For each student , there is a name , subject list along with marks . i need to take input for student name and his marks and then return the top 3 students based on marks. I tried using Hashmap and a list to get the marks. After taking the marks of 1st student in list i am clearing everything from the list and taking the input again. But this is changing the data for the first student also . I am not sure what i am doing wrong here?
HashMap<String,ArrayList<Integer>> map=new HashMap<>();
ArrayList<Integer> marks=new ArrayList<>();
//1st student detail
marks.add(12);
marks.add(10);
marks.add(15);
map.put("Rakesh",marks);
System.out.println(map);
//Reset the marks List
marks.clear();
//2nd student detail
marks.add(9);
marks.add(8);
marks.add(15);
map.put("Ashwin",marks);
System.out.println(map);
//Reset the marks List
marks.clear()
//3rd student detail
marks.add(7);
marks.add(11);
marks.add(8);
map.put("Rahul",marks);
System.out.println(map);```
CodePudding user response:
You are adding multiple entries to the HashMap
with the same object reference. Instead of the HashMap
making a copy of what you put
into it, it stores only the reference. All your keys point to the same marks
list, so only the list entries after the last clear
show up.
Rakesh ----> [7, 11, 8]
Ashwin -/ /
Rahul --/
Instead, create new ArrayList
s for each key, so each key can reference a unique ArrayList
object.
CodePudding user response:
You only create one List
, and you keep clearing it.
Get rid of clear()
, and create a new list for each student:
...
//1st student detail
marks.add(12);
marks.add(10);
marks.add(15);
map.put("Rakesh",marks);
System.out.println(map);
// Create a new marks list:
marks = new ArrayList<>();
// 2nd student detail
marks.add(9);
marks.add(8);
...
CodePudding user response:
Take a look at how Object reference works in Java. If you replace marks.clear() with marks = new ArrayList<>() then you will get your desired result.
CodePudding user response:
You're repeatedly adding to and clearing the same list.
I would do it like this.
Map<String,List<Integer>> map=new HashMap<>();
map.computeIfAbsent("Rakesh", v->new ArrayList<>()).addAll(List.of(12,10,15));
map.computeIfAbsent("Ashwin", v->new ArrayList<>()).addAll(List.of(9,8,15));
map.computeIfAbsent("Rahul", v->new ArrayList<>()).addAll(List.of(7,11,8));
- computeIfAbsent will create and return an
ArrayList
or return the existing one. Then you can just add the values to the returned list. - Later on you can update the
list
using the same construct and it will either add the value/values to an existing person, or create a new map entry and add it there.
map.entrySet().foreach(System.out::println);
prints
Rahul=[7, 11, 8]
Ashwin=[9, 8, 15]
Rakesh=[12, 10, 15]
If you want to preserve the order entries were added to the Map use a LinkedHashMap. If you want the map to be sorted based on the Key (person) use a TreeMap.
CodePudding user response:
You are saving the reference to the arraylist
marks in the map
, so all map
values are pointing to the same arraylist
. Create a different arraylist
for every student.
You can use something like this:
HashMap<String,ArrayList<Integer>> map=new HashMap<>();
ArrayList<Integer> marks=new ArrayList<>();
//1st student detail
marks.add(12);
marks.add(10);
marks.add(15);
map.put("Rakesh", new ArrayList<Integer>(marks));
System.out.println(map);
//Reset the marks List
marks.clear();
//2nd student detail
marks.add(9);
marks.add(8);
marks.add(15);
map.put("Ashwin",new ArrayList<Integer>(marks));
System.out.println(map);
//Reset the marks List
marks.clear()
//3rd student detail
marks.add(7);
marks.add(11);
marks.add(8);
map.put("Rahul",new ArrayList<Integer>(marks));
System.out.println(map);
CodePudding user response:
try this. Create List and inject each element with Arrays.asList.
HashMap<String, List<Integer>> map=new HashMap<>();
List<Integer> marks;
//1st student detail
marks = Arrays.asList(12,10,15);
map.put("Rakesh",marks);
System.out.println(map);
//2nd student detail
marks= Arrays.asList(9,8,15);
map.put("Ashwin",marks);
System.out.println(map);
//3rd student detail
marks= Arrays.asList(7,11,8);
map.put("Rahul",marks);
System.out.println(map);