• Post Reply Bookmark Topic Watch Topic
  • New Topic

Conversion In Collections  RSS feed

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you sample give code to convert ArrayList to HashMap and HashMap to ArrayList.
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajendra,

As you know ArrayList contains only value. While HashMap contains Key and Value.

Arraylist ("Value") or HashMap ("Key" , "Value") pair.

SO you can not convert it to directly ArrayList to HashMap and HashMap to ArrayList.
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

Well you could split it into 2 arraylists, 1 with the keys and the other holding the values, but why you would want to do this I have no idea.

Sean
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can convert a HashMap into an ArrayList: I still don't see why you'd want anything like this though. If it is because you want to maintain the insertion order for the Map, use LinkedHashMap instead.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
convert a HashMap into an ArrayList:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!