Solved

Convert list to map

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 72
hibernate example using maven 12 77
Need Help! Getting a syntax error and don't understand why 3 38
What browser will run Java? 7 124
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

756 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