Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert List to a Map

 
vidu mayur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
There is a LinkedList with elements which have a key and a value. For example.,
List ls contains
1=one
2=two
3=three
I want this to be mapped as
HashMap hm = {1=one, 2=two, 3=three}
How do I do that?
Thanks in advance
Vidu
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use StringTokenizer to parse the elements of the
LinkedList, use "=" as delimiter.
for each element,
first token : key
2nd token : value
put the (key, value) into the map
 
vidu mayur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot! That worked perfectly!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic