Monday, June 26, 2017

Here is a code sample to use the iterator in a for loop to remove the entry.

    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();
      }
    }

No comments:

Post a Comment