• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap from a Collection  RSS feed

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is there any way we can get a hashmap from any other different object of the collection say Arraylist/vector??

Regards
 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean with "getting an HashMap" ?
 
Stan Verswijver
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a hashamp is a collection of [key,value] entries.

It is the only collection that has this kind of entries. Others like arrays and lists have single entries like [value].

the only way to convert a collection different than a hashmap is to iterate that collection and put all values in a hashmap with entry [value, null] where your key for the hashmap is equals to the value of your original collection entry.
i think this is what you wanted to know...
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the only way to convert a collection different than a hashmap is to iterate that collection and put all values in a hashmap with entry [value, null] where your key for the hashmap is equals to the value of your original collection entry.
i think this is what you wanted to know...
Thats no the only way. A random access List<T> is semantically equivalent to a Map<Integer, T>.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Kumar, I think the answer to this would depend on what sort of items are in the Collection and how they are related. Can you give a small example of a Collection that you start with, and the Map that you might expect to get from it? There is no single general answer to your question, but there are several strategies that might make sense for certain types of data.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was a question my friend had asked me...i had an idea about the answer but wanted to make sure with you all...

Thanks for the info...
[ March 02, 2007: Message edited by: A Kumar ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!