I need the last input from HashMap to be deleted then, add a new input instead.I heard you can do that using LinkedHashMap, but how exactly? The instructions didn't mention that I should use LinkedHashMap but apparently, it is impossible to remove the last item from a HashMap without it.
Or if you have any alternative solutions that would remove the last item so I could add the other input, do tell me what I should add to the code.
Here is what I am trying to do:
package studentlist;
import java.util.LinkedHashMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class StudentList {
public static void main(String[] args) {
Map<String, String> students = new HashMap<>();
Scanner s = new Scanner(System.in);
for(int i=1; i<= 3; i++){
System.out.print("Enter student number " + i + ": ");
String es = s.nextLine();
System.out.print("Enter student first name " + i + ": ");
String en = s.nextLine();
students.put(es, en);
}
for (Map.Entry mp : students.entrySet()) {
System.out.println(mp.getKey() + " " + mp.getValue());
}
//The 3rd input should be removed before this:
System.out.print("Enter your student number: ");
String sn = s.nextLine();
System.out.print("Enter your first name: ");
String fn = s.nextLine();
students.put(sn, fn);
for (Map.Entry mp : students.entrySet()) {
System.out.println(mp.getKey() + " " + mp.getValue());
}
}
}