由于许多实现原因(使用带有非常有限库的Java ME 1.4),我无法访问HashMap或任何类型的Map接口。与此相结合,我必须使用Hashtable,它在我使用的库中不会继承任何东西。
是的,我绝对没办法绕过我正在使用的实现和库。
所以我有两个Hashtables。我需要创建一个新的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);
}
答案 0 :(得分: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;
}
}
感谢您让我不得不考虑这些家伙。我写了这个,它对我有用。