简体   繁体   中英

Removing elements from a key/value subview of Map?

How can I get a subview of Map then cross elements off it, Gave it a whirl in vain. Here's my snippet of code :

HashMap<String,Integer> myLinkedHashMap = new LinkedHashMap<String, Integer>(1,1,true);         
myLinkedHashMap.put("a", 2);
myLinkedHashMap.put("b", 3);
myLinkedHashMap.put("c", 4);
Set keysView =  myLinkedHashMap.keySet();
keysView.remove("a");  // worked without a hitch     
Collection valuesView =  myLinkedHashMap.values();
valuesView.remove(4);  // like the last one

Here's what my question comes down to :

Set<Entry<String,Integer>> associationsView = myLinkedHashMap.entrySet();
associationsView.remove("Nothing doing,for set does not know about key/value thing");   

Lending a hand would be far appreciated, thanks.

What are you trying to do? Would something like this work?

final Iterator<Entry<String,Integer>> iter = myLinkedHashMap.entrySet().iterator();
while(iter.hasNext()) {
  final Entry<String,Integer> entry = iter.next();
  if(entry.getKey().equals("something")) {
    iter.remove();
  }
}

It might also be worth taking a look at the Guava Maps class, this has many ways if filtering maps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM