Ordered index in Collections
Hello,
I try to set up a data structure which would have the following features:
- Key / value for direct access via get (key)
- The structure must be ordered (and order does not change with time):
key1 -> value1
key2 -> value2
Key3 -> vlaeur3
- Access via index also get (i)
I work in a small enthronement and have access to:
- Interfaces: Collection, List, Map, Set
- Classes: AbstractCollection, AbstractList, AbstractMap, AbstractSet, ArrayList, Dictionary, HashMap, HashSet, Hashtable, Vector
well on any class does not meet all these requirements, it is necessary that I implement my own structure by extending / implementing something exists: In your opinion what is the best way?
Re: Ordered index in Collections
Hello,
See if you can use LinkedHashMap which allows you to create map preserving the order of addition of components. Then you can very well use entrySet (). toArray () if it is not too penalizing in access time. I think you can go with the following and then create a new table to access your program , this will be easy and faster to access.
Re: Ordered index in Collections
Hello,
This requires that you work with two hashtables. The first contains your data, the second is a table that associates an index (order came from your element) the value of the corresponding key in the first table. I know if I am not very clear? A small example to understand:
Code:
Hashtb tb1 = new HashTable();
Hashtb tb2 = new HashTable();
for (int i = 0 ; I <NumElements; i + +){
tb1.could(key, value);
Table2.could(new Integer(i), key);
}
String value1 = tb1.get( Table2.get(new Integer(0)));
I think it answers your problem?
Re: Ordered index in Collections
Hello,
Why not simply use two's List? one for keys and another for values to get (Key) can be translated.
Code:
try {
return lsval.get(listCles.indexOf(Key);
} catch(ArrayOutOfBoundsException e) {
return null;
}
Re: Ordered index in Collections
Hey,
This is my updated code.
Code:
Public class enchasht {
private Hashtable ori;
private Hashtable ordmp;
Public enchasht() {
Original = new Hashtable();
ordmp = new Hashtable();
}
Public void could(Object key, Object val) {
ordmp.could(new Integer(ori.size()), Key);
ori.could(key, val);
}
Public Object get(Object key) {
return ori.get(key);
}
Public Object get(int i) {
Object key = ordmp.get(new Integer(i));
Object val = null;
try {
val = ori.get(key);
} catch(NullPointerException npex) {}
return val;
}
Public void clear() {
ordmp.clear();
ori.clear();
}
Public Object getFirstElement() {
return this.get(0);
}
Public GetLastElement Object() {
return this.get(ori.size()-1);
}
}
Re: Ordered index in Collections
Hello,
The key is not stored twice in memory (there is not no new () or clone ()). Do not forget when passing java parameters to a function is by reference not by value (the notion of pointer and memory address). Therefore the value of the object key is not duplicated . The access is in the worst case (ie retrieve the last element) with a linear list, it is consistent with a HashTable. If you got a lot of data, uses a HashTable otherwise the list. You're probably right about the fact that a HashTable is more space in memory than a list, because the "new Integer (i). I hope to have you provide some clarification. Otherwise, your class is very well