I have the following Hashmap:
Map<String,String> studentGrades = new HashMap<>();
studentGrades.put("Tom", "A+");
studentGrades.put("Jack", "B+");
Iterator<Map.Entry<String,String>> iterator = studentGrades.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String,String> studentEntry = iterator.next();
System.out.println(studentEntry.getKey() + " :: " + studentEntry.getValue());
iterator.remove();
}
I thought the iterator.remove();
meant that something would be removed from the HashMap
, for example iterator.remove("Tom");
, then when the iteration happens this is removed from the HashMap.
The program compiles and runs correctly when there iterator.remove();
but when it is iterator.remove("Tom");
an error is found. The compiler says
required: no arguments and found: java.lang.String reason: actual and formal arguments list differ in length.
Any reason why this is happening or have I got iterator.remove();
completely wrong?