Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Conversion of list contains Employee object

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Employee{
String empId;
String empName;
String deptId;
}

How to convert from the list<Employee> to map<String,List<Employee> here key is department name and value is employee list?

Please guide me for convertion from the list to map in java.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

rithvik sukhavasi wrote:How to convert from the list<Employee> to map<String,List<Employee> here key is department name and value is employee list?

Please guide me for convertion from the list to map in java.


You are not converting the List into a Map - you are adding a List to a Map. For that you need to use the Map.put method.
 
Marshal
Posts: 69856
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…and welcome to the Ranch
 
rithvik sukhavasi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here my question is Employee list is created like

Now I have to convert this list into map such that key is the department and value is empList.
In a map the key is like "hr" and value is list of emp1,emp3.

Could you please help me to solve this problem
 
Campbell Ritchie
Marshal
Posts: 69856
278
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added code tags, which you should always use, to your last post, and doesn't it look better

Why are you using a linked list rather than an array list?
I think you would have to iterate your list and put a new list into the Map. It would be a bit like the counting example in the Java Tutorials. There is a new technique there, for Java8 users only.
 
This tiny ad will self destruct in five seconds.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic