Ashish Mohan
Ashish Mohan

Reputation: 724

Convert List<Object[]> into HashMap<Integer,List<String>> what can be a optimal way?

I have List<Object[]> say li with items like

li.add(new Object[]{1,"Orange"}) 
li.add(new Object[]{1,"Apple"}) 
li.add(new Object[]{1,"Orange"}) 
li.add(new Object[]{2,"Guava"}) 
li.add(new Object[]{3,"Apple"}) 
li.add(new Object[]{3,"Grapes"}) 

i.e. one Integer can be multiple times.Now I have to convert this List into a HashMap HashMap<Integer,List<String>> types what can be a optimal way?

Upvotes: 0

Views: 1751

Answers (4)

Carlos Robles
Carlos Robles

Reputation: 10947

I would use an SparseArray<ArrayList<String>> that has better performance. Actually probably your IDE (eclipse does) will recommend you to change your HashMap<Integer,List<String>> to SparseArray

     SparseArray<ArrayList<String>> list;

      for (Object[] objects : yourlist) {

            Integer number=((Integer) objects[0]);
            String value=((String) objects[1]);
            if (list.get(number) == null) {
                 ArrayList<String> temp= new  ArrayList<String>(); 
                 temp.put(value);
                 list.append(number, temp);
            }
            else list.get(number).put(value);

       }

Upvotes: 0

Niraj Chapla
Niraj Chapla

Reputation: 2179

If you want to avoid duplicate value in the value list in the map user HashSet in place of List

With HashSet

HashMap<Integer, HashSet<String>> map =new HashMap<Integer, HashSet<String>>();
for (Object[] objects : lst) {
HashSet<String> valueSet;
if(map.containsKey((Integer)objects[0])){
valueSet = map.get((Integer)objects[0]);
}
else{
   valueSet = new HashSet<String>();
   map.put((Integer)objects[0],valueSet);
}
valueSet.add((String)objects[1]);
}
System.out.println(map);

Using List

HashMap<Integer, List<String>> map =new HashMap<Integer, List<String>>();
for (Object[] objects : lst) {
List<String> valueList;
if(map.containsKey((Integer)objects[0])){
valueList = map.get((Integer)objects[0]);
}
else{
valueList = new ArrayList<String>();
map.put((Integer)objects[0],valueList);
}
valueList.add((String)objects[1]);
}
System.out.println(map);

Upvotes: 1

Masudul
Masudul

Reputation: 21971

Try,

    Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();

    for (Object[] objects : li) {
        if (map.containsKey(objects[0])) {
            List<String> list = map.get(objects[0]);
            list.add((String) objects[1]);
            map.put((Integer) objects[0], list);
        } else {
            List<String> list = new ArrayList<>();
            list.add((String) objects[1]);
            map.put((Integer) objects[0], list);
        }
    }
    System.out.println(map);

Upvotes: 0

Rahul
Rahul

Reputation: 45070

Here is a sequence of steps which can help you achieve it.

  1. Iterate over the List.
  2. For each element, check if the Object[0] is already present in the map.
    a. If present, then get the List<String> for that key and add the Object[1] to it.
    b. If not, then create a new List<String>, add the Object[1] to it and add an entry to the map with Object[0] as the key and the newly created List with the Object[1] added to it as the value.
  3. Continue this till the end of the list.

Note: You need to cast your Object[0] to Integer and Object[1] as String wherever necessary.

Upvotes: 3

Related Questions