Re: Collection
"Garg" <sendtogarg@gmail.com> wrote:
| in ArrayList, Vector we can get the value by providing posting number
In any List.
| Like:
|
| String str = (String) arrList.get(4);
This cast should not be necessary. Also, "arrList" is not a good name for a
variable.
| but arrayList can only store one object.
You mean "only one object per slot", but that's no problem because that object
can hold any number of references.
| HashMap, TreeMap can store two maps (key and Object).
No, they cannot store two "maps", they store two objects in each slot, a key
and a value, and the value type need not be Object.
| Can i [sic] get the value of the key and Object stored at the 4th place.
|
| is there any way to achieve this.
Map doesn't just store a pair, it stores a pair /indexed by the key/. It
sounds like this is more than you need.
Declare a class to hold the pair of values, say, Pair <T, U>, and put
instances of that class in your list. That's how you'd do it with a Map,
where the "pair" class is Map.Entry, only with the custom solution you
dispense with the unnecessary machinery.
As Matt Humphrey pointed out, unless you specify a particular Map
implementation you lose any guarantee of order. If you do specify a
particular Map implementation, you are mandating machinery that you do not
actually need.
List< Pair <T, U>> is the way to go.
--
Lew