Solved

Convert list to map

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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

724 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