Java Map update during iteration

Description

Java Map update during iteration

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Main {

  public static final void main(String[] args) {
    Map<String,String> m = new HashMap<>();
    m.put("CSS", "1");
    m.put("HTML", "2");

    System.out.println(m);/*from w w  w.j  a  v a  2  s  .  c  o  m*/
    
    Iterator<Map.Entry<String,String>>it = m.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry<String,String> entry = it.next();
      if (entry.getKey() == "HTML") {
        entry.setValue("new Value");
      }
    }
    System.out.println(m);
    
  }
}



PreviousNext

Related