0

由于很多实现原因(使用 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);
    }
4

1 回答 1

0
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;
    }
}

谢谢你让我不得不考虑这些家伙。我写了这个,它对我有用。

于 2013-07-01T23:13:30.193 回答