-3

This is my code.I am inserting a map into a list.but when I am directly adding a map into table.It is showing error.

import java.util.*;
class mapIn{
   public static void main(String... a){
List<Map<Integer, String>> mapList = new ArrayList<Map<Integer,String>>();
mapList.add(new HashMap<Integer,String>().put(1,"Ram"));
mapList.add(new HashMap<Integer,String>().put(2,"Shyam"));
mapList.add(new HashMap<Integer,String>().put(3,"Shyam"));
for(Map m:mapList){
        // for(Map.Entry e:m.entrySet()){
        //  System.out.println(e.getKey()+" "+e.getValue());
        // }
       Set set=m.entrySet();//Converting to Set so that we can traverse  
    Iterator itr=set.iterator();  
            while(itr.hasNext()){  
        //Converting to Map.Entry so that we can get key and value separately  
        Map.Entry entry=(Map.Entry)itr.next();  
        System.out.println(entry.getKey()+" "+entry.getValue());  
    } 
    }
}
}
Anurag Mishra
  • 181
  • 1
  • 4
  • 1
    what's the error? could you please format your code? Please read our [ask] page for hints on how to improve this question – blurfus Feb 04 '17 at 03:16
  • Returns: the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.) This is put returns value description. This should not compile at all. –  Feb 04 '17 at 03:18

3 Answers3

1

aibreania's answer is the better way to do it, but if you want to keep it in one line you can use:

mapList.add(new HashMap<Integer,String>(){{ put(1,"Ram"); }});
mapList.add(new HashMap<Integer,String>(){{ put(2,"Shyam"); }});
mapList.add(new HashMap<Integer,String>(){{ put(3,"Shyam"); }});
Nic
  • 253
  • 1
  • 3
  • 9
  • I see no reason why it wouldn't, it obviously needs the other code around it as the OP has – Nic Feb 04 '17 at 03:24
  • you learn something new everyday. I'd never encounter this syntax to put elements into a HashMap – blurfus Feb 04 '17 at 03:30
  • 2
    It's compact, but does have problems: as discussed [here](http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization) – Nic Feb 04 '17 at 03:35
0

Please do the steps of initializing a hashMap and putting (key, value) into the map separately. I rewrote the first part of your code:

List<Map<Integer, String>> mapList = new ArrayList<>();
for(int i = 0; i < 3; i++) mapList.add(new HashMap<Integer, String>());
mapList.get(0).put(1, "Ram");
mapList.get(1).put(2, "Shyam");
mapList.get(2).put(3, "Shyam");

I don't know what your code is for, but using an ArrayList to store 3 different hashMap is not really efficient. If you can provide more info, we can furtherly improve the code. Hope it helps. :D

aibreania
  • 1
  • 1
  • this does something different than what the OPs question is doing. – blurfus Feb 04 '17 at 03:23
  • It is just a miniature.actually, I am undergraduate and I am using this for implementation of a weighted graph where I can insert weight and node into a map and store it in a list. – Anurag Mishra Feb 04 '17 at 04:25
0

The put method in HashMap returns String and not the type (Map<Integer, String>) of the underlying objects in your list. That's why you get that error.

You can do it the following way too:

mapList.add(Collections.singletonMap(1, "Ram"));
mapList.add(Collections.singletonMap(2, "Shyam"));
mapList.add(Collections.singletonMap(3, "Shyam"));
VHS
  • 9,534
  • 3
  • 19
  • 43