Solved

Convert list to map

Posted on 2014-04-03
5
339 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
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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 86
java 8 lambda expresssions exception handling 3 91
servlet example 11 40
difference between sorce folder and folder in eclipise 3 16
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

832 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