Solved

Convert list to map

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strCount chalenge 3 50
mapShare challenge 13 68
for i loop in grovy 1 32
Python Assistance 7 35
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now