Reputation: 143
Class Test
is my separate class which has two field of String
and Float
type, I am using this class with List Collections which will be finally populated as values into HashMap
.
But when I try to populate the Map with a key and the List objects (values) into the map, Java does not seem to accept it as it is not valid syntax:
ArrayList <Test> list = new ArrayList <Test> ();
Map<Integer, ArrayList <Test>> mp = new HashMap<Integer, ArrayList <Test>>();
list.add(new Telephone ( 0.9 , "A"));
list.add(new Telephone(5.1 , "A"));
mp.put(0,list.get(0)); // this Does Not work :(, it should work
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method put(Integer, ArrayList<Telephone>) in the type
Map<Integer,ArrayList<Telephone>> is not applicable for the arguments
(int, Telephone) at Main.main(Main.java:64)
Upvotes: 0
Views: 223
Reputation: 13066
Going by your requirement you told in comments and question above I think you need following syntax for Map declaration:
Map<Integer,Test> mp = new HashMap<Integer,Test>();
EDIT
OK here is the Edit:
ArrayList<Test> list = new ArrayList<Test>();
Map<Integer,ArrayList<Test>> mp = new HashMap<Integer,ArrayList<Test>>();
list.add(new Test(0.1,"A"));
list.add(new Test(0.2,"B"));
mp.put(1,list);
and if you again want to put more Test objects at key 1 then do as follows:
List<Test> value = mp.get(1);
value.add(0.3,"c");
value.add(0.5,"E");
mp.put(1,value);
Upvotes: 2
Reputation: 3274
Your map accepts an Integer as key and ArrayList of Test as value.But instead of arrayList of Test object, you are trying to put Telephone object. Your IDE is clearly stating that.
Map<Integer,ArrayList<Telephone>> is not applicable for the arguments
(int, Telephone) at Main.main(Main.java:64)
Upvotes: 2
Reputation: 34900
You map can accept only List
objects as values, while you are trying to put there simple Telephone
object.
Upvotes: 1