?
Solved

Convert list to map

Posted on 2014-04-03
5
Medium Priority
?
357 Views
Last Modified: 2014-04-16
Hi,
  I need a generic method to convert from list to map. I can do it by passing the specific object like below, but I need a generic method to convert from list to map.

public static HashMap<String, String> convertListToMap(List<Test> listArray){
      
      
            
            HashMap<String, String> map = new HashMap<String, String>();
            Iterator iter = listArray.iterator();
            while (iter.hasNext()){
                  Test tmpProp = iter.next();
                  map.put(tmpProp .id(), tmpProp.getName());
          }
            return map;
      }
}

In the above code it is specific to class Temp, but I want a generic method which converts lists to map.

Thank you
0
Comment
Question by:Sthokala
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39975461
but I want a generic method which converts lists to map.
How can you have that without some way of specifying how the keys are derived?
0
 

Author Comment

by:Sthokala
ID: 39975468
I have get methods are same in all (getID and getName)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39975890
I can't think of any way to make it more generic really
0
 
LVL 9

Expert Comment

by:sanjooz
ID: 39976663
If you have getID and getName methods in all your classes that you will be iterating over, then you could define an interface with these methods in it. Your classes will then implement this interface.

When you can change your method from concrete class Test to the interface like

convertListToMap(List<InterfaceName> listArray)

That would server your purpose.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 1500 total points
ID: 39978323
What sanjooz means is this:

With this interface

public interface MyObject {
     String getId();
     String getName();
}

Open in new window

you can change your code like this:

public static HashMap<String, String> convertListToMap(List<MyObject> listArray){
            HashMap<String, String> map = new HashMap();
            for (MyObject myObject : listArray) {
                  map.put(myObject .id(), myObject.getName());
            }
            return map;
      }
}

Open in new window

This way you can use your method for all classes that implement MyObject

Like this:

public class Temp implements MyObject {

      public String getId() { ... }
      public String getName() { ... }
}

Open in new window

or

public class AntoherOne implements MyObject {

      public String getId() { ... }
      public String getName() { ... }
}

Open in new window


This is valid code:
List<Temp> myList = new ArrayList();
myList.add( new Temp(...) );
myList.add( new Temp(...) );
myList.add( new Temp(...) );

map = convertListToMap(myList);

Open in new window


But this also:

List<AnotherOne> myList2 = new ArrayList();
myList2.add( new AnotherOne(...) );
myList2.add( new AnotherOne(...) );
myList2.add( new AnotherOne(...) );

map = convertListToMap(myList2);

Open in new window

0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question