• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

How to convert Map<String,String> to ArrayList<Employee>  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is it possible to convert Map<String,String> to ArrayList<Employee>?

Employee is a class which has name and address fields of type String.
 
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming you want to use the map keys as name and map values as address: yes, it is possible - by writing the necessary code. I don't know of any JDK class or method that would do the work in this case.

What have you tried? Do you have some doubts?

 
Pradeep allada
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no name and address are the fields belongs to Employee like employee name and employee address

I have tried like this
I don't know whether it is correct or not

and to convert them to List



 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're getting compile-time error, because the m.values() is a collection of Strings. That ArrayList constructor needs a collection of Employee's.

Java doesn't know how to convert a String to an Employee. You need to do that. You probably also need to code a loop that will go through all the entries in the map.

It might help if you write down all the individual operations you need to do on a piece of paper. And then start to thinking about converting these steps to actual code.
 
You showed up just in time for the waffles! And this tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!