1
private Set<Entry<Personne, List<Precision>>> mapCorrelationEtat;

    for (Map.Entry<Personne, List<Precision>> entry : this.mapCorrelationEtat) {
        Personne key = entry.getKey();
        System.out.println(key.getIdPersonne());
    }

in this linkedhashmap I have some values, I want to remove some values from an another List

keyList: [1,2,5];

How can I do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mercer
  • 9,736
  • 30
  • 105
  • 170
  • Is it a list of `keys` or `values`? – Daniel May 03 '16 at 13:45
  • @Daniel a list of `keys` – Mercer May 03 '16 at 13:46
  • Do you want to remove entire Entry (key,value pair) or only value for Entry for selected key (you could set that value to some neutral data or null)? – Pshemo May 03 '16 at 13:48
  • @Pshemo want to remove entire Entry (key,value pair) – Mercer May 03 '16 at 13:49
  • Your question looks like duplicate of [How to remove a key from HashMap while iterating over it?](http://stackoverflow.com/questions/6092642/how-to-remove-a-key-from-hashmap-while-iterating-over-it). Is there any reason I shouldn't vote it as duplicate? – Pshemo May 03 '16 at 14:16
  • "i want to remove some values from an another List" is confusing. It may suggest that you want to edit list and remove some values from it. It can also be understood as you want to remove from map some elements which also exist in list. To get rid of this confusion try to add to your question simple example of input and expected result. – Pshemo May 03 '16 at 14:49

1 Answers1

2

You can simply call removeAll on keySet:

map.keySet().removeAll(keyList);
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • It looks like keys in map are instances of `Personne` class, but list contains only persons ID. – Pshemo May 03 '16 at 13:50
  • 1
    @Pshemo That seems correct from the code. Yet it is not what the OP describes :-/ – Jean Logeart May 03 '16 at 13:52
  • @Pshemo overriden `Personne::toString()`?? – Jordi Castilla May 03 '16 at 14:00
  • @Mercer What is your Map then? Maps which implement `java.util.Map` interface like `LinkedHashMap` must have this method https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#keySet-- – Pshemo May 03 '16 at 14:03
  • @JeanLogeart I suspect that OP is looking for something more like `map.keySet().removeIf(person -> listOfIDToRemove.contains(person.getIdPersonne()));` – Pshemo May 03 '16 at 14:45