0

I was told that I should remove the last or third entry of the mapping, but I can't find any solution to these on how to get the specific entry and remove it.

Here is the code:

Scanner reader = new Scanner(System.in);
        
    String input1, input2;
    Map<String, String> students = new HashMap<>();
        
        for(int i = 1; i <= 3; i++){
            System.out.print("Enter student number " + i + ": ");
            input1 = reader.next();
            
            System.out.print("Enter first name " + i + ": ");
            input2 = reader.next();
            
            students.put(input1, input2);
        }
        
        System.out.println("Student List:");
        
        for(Map.Entry entry: students.entrySet()){
            System.out.println(entry.getKey() + " - " + entry.getValue());
        }

I tried using the students.remove(students.get(3), students.get(3)); but still it doesn't work. Is there a possible solution to this? Please help.

  • HashMaps don't have order -- what does "last or third entry" mean? – nanofarad Dec 04 '20 at 05:32
  • Use LinkedHashMap instead of HashMap – Sri Dec 04 '20 at 05:36
  • You could use a `LinkedHashMap`, this preserves the insertion order. A regular `HashMap` doesn't. A better solution would likely be not to use a map at all. Instead create `Student` objects and store in a `List`, then use `list.remove(2)` to remove the third element. – Magnilex Dec 04 '20 at 05:37
  • @nanofarad, what I mean is the last user input. As u can I see I am asking for the user to enter which student id becomes the key and the value goes for the first name of the student. – Official Jappy Dec 04 '20 at 05:45

3 Answers3

0

Please use LinkedHashMap.It extends HashMap and maintains a linked list of the entries in the map, in the order in which they were inserted.

Map<String, String> students = new LinkedHashMap <>();
Sri
  • 437
  • 1
  • 4
  • 13
0

From iterating over and removing from a map example:

Map<String, String> map = new HashMap<String, String>() {
  {
    put("test", "test123");
    put("test2", "test456");
  }
};

for(Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); it.hasNext(); ) {
    Map.Entry<String, String> entry = it.next();
    if(entry.getKey().equals("test")) {
        it.remove();
    }
}

You can do something like


int size = map.entrySet();
int i = 0;
for(Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); it.hasNext(); i++) {

    Map.Entry<String, String> entry = it.next();
    if(i == size - 1) { // last
//     if(i == 2) { // 3rd
        it.remove();
    }
}

However note that the above operation can be O(n) for hashmap. LinkedHashMap as other have pointed out can have a O(1) per deletion time

Sean L
  • 151
  • 2
  • 12
0

I think that in your case it might be well enough to remove the last entry directly instead of looping through the map trying to find it. Use LinkedHashMap to preserve order when printing out the entries:

Scanner reader = new Scanner(System.in);
        
String input1, input2;
LinkedHashMap<String, String> students = new LinkedHashMap<>();
        
String last = null;
        
for (int i = 1; i <= 3; i++) {
    System.out.print("Enter student number " + i + ": ");
    input1 = reader.next();
            
    System.out.print("Enter first name " + i + ": ");
    input2 = reader.next();
            
    students.put(input1, input2);
            
    if (i == 3) {
        last = input1;
    }
}
        
students.remove(last);
        
System.out.println("Student List:");
        
for (Map.Entry entry: students.entrySet())
    System.out.println(entry.getKey() + " - " + entry.getValue());
}
Newerth
  • 449
  • 2
  • 12