【问题标题】:Combining Hashtables without unifying interface组合Hashtables而不统一接口
【发布时间】:2013-07-01 22:24:56
【问题描述】:

由于许多实现原因(使用带有 非常 有限库的 Java ME 1.4),我无法访问 HashMap 或任何类型的 Map 接口。结合这一点,我必须使用 Hashtable,它在我使用的库中不会继承任何东西。

是的,我绝对无法绕过我正在使用的实现和库。

所以我有两个哈希表。我需要创建一个新的 Hashtable 实例来访问和更改两个“支持”实例。由于 Hashtable 不继承任何东西,有什么办法可以做到这一点?我已经尝试了一种基本的组合策略,它只通过一组表,但存在一些严重的问题。具体来说,put(key, object) 很困难,因为无法判断它被支持到哪个地图。

对执行此操作的策略有任何建议还是我被卡住了?

public class Scope {

    private final Hashtable publicVars;
    private final Hashtable publicMethods;
    private final Hashtable publicReturning;
    private final Hashtable privateVars;
    private final Hashtable privateMethods;

    public Scope() {
        publicMethods = new Hashtable();
        publicReturning = new Hashtable(0);
        publicVars = new Hashtable();
        privateVars = new Hashtable();
        privateMethods = new Hashtable();
    }

    public Scope(Scope scope) {
        publicVars = scope.publicVars;
        publicMethods = scope.publicMethods;
        publicReturning = scope.publicReturning;
        privateVars = new Hashtable();
        privateMethods = new Hashtable();

        // Here's my problem - I need changes made to publicVars to also affect scope.privateVars (and the same to methods)
        publicVars.putAll(scope.privateVars);
        publicMethods.putAll(scope.privateMethods);
    }

【问题讨论】:

  • 好吧,如果没有办法告诉Mapput 进入,那么就没有办法告诉。 需要为此制定策略。或者使统一的HashTable 不可变。
  • 你为什么要制作一个更新两个哈希表的哈希表?为什么不在两个地方使用一个 Hashtable?
  • 你想用这个“组合哈希表”解决什么问题?
  • 我需要一个哈希表来“推送”更新“上游”到它的“父”哈希表。这有任何意义吗?我可能正在接近这个错误。
  • 我添加了一个示例,希望能阐明我想要做什么。

标签: java hashmap hashtable


【解决方案1】:
private static final class MapGroup {

    private final List maps = new ArrayList();

    public MapGroup(Hashtable start) {
        maps.add(start);
    }

    public MapGroup(MapGroup group) {
        for (int x = 0; x < group.maps.size(); x++) {
            maps.add(group.maps.get(x));
        }
    }

    public void add(Hashtable h) {
        maps.add(h);
    }

    public Enumeration keys() {
        return new Enumeration() {
            private final Enumeration[] enumerations;
            private int i;

            {
                enumerations = new Enumeration[maps.size()];
                for (int x = 0; x < maps.size(); x++) {
                    enumerations[x] = ((Hashtable) maps.get(x)).keys();
                }
            }

            public boolean hasMoreElements() {
                return enumerations[i].hasMoreElements()
                        || (++i < enumerations.length && enumerations[i].hasMoreElements());
            }

            public Object nextElement() {
                // needed to increment i
                return hasMoreElements() ? enumerations[i].nextElement() : null;
            }
        };
    }

    public Enumeration elements() {
        return new Enumeration() {
            private final Enumeration[] enumerations;
            private int i;

            {
                enumerations = new Enumeration[maps.size()];
                for (int x = 0; x < maps.size(); x++) {
                    enumerations[x] = ((Hashtable) maps.get(x)).elements();
                }
            }

            public boolean hasMoreElements() {
                return enumerations[i].hasMoreElements()
                        || (++i < enumerations.length && enumerations[i].hasMoreElements());
            }

            public Object nextElement() {
                // needed to increment i
                return hasMoreElements() ? enumerations[i].nextElement() : null;
            }
        };
    }

    public boolean contains(Object value) {
        for (int x = 0; x < maps.size(); x++) {
            if (((Hashtable) maps.get(x)).contains(value)) {
                return true;
            }
        }
        return false;
    }

    public boolean containsKey(Object key) {
        for (int x = 0; x < maps.size(); x++) {
            if (((Hashtable) maps.get(x)).containsKey(key)) {
                return true;
            }
        }
        return false;
    }

    public Object get(Object key) {
        for (int x = 0; x < maps.size(); x++) {
            if (((Hashtable) maps.get(x)).containsKey(key)) {
                return ((Hashtable) maps.get(x)).get(key);
            }
        }
        return null;
    }

    public Object put(Object key, Object value) {
        for (int x = 0; x < maps.size(); x++) {
            if (((Hashtable) maps.get(x)).containsKey(key)) {
                return ((Hashtable) maps.get(x)).put(key, value);
            }
        }
        return ((Hashtable) maps.get(maps.size() - 1)).put(key, value);
    }

    public Object remove(Object key) {
        // Nothing is ever removed - don't worry
        return null;
    }

    public void clear() {
    }

    public int size() {
        int s = 0;
        for (int x = 0; x < maps.size(); x++) {
            s += ((Hashtable) maps.get(x)).size();
        }
        return s;
    }

    public boolean isEmpty() {
        return size() == 0;
    }
}

感谢您让我不得不考虑这些人。我写了这个,它对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-10
    • 2012-01-30
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    相关资源
    最近更新 更多