简体   繁体   中英

How to remove a value from HashMap with list of values in Java?

I'm trying to learn about HashMaps and was hoping to get some help with removing values (in this case, places). I have 3 HashMaps (perName, perCategory, perPosition), 2 of them have lists of places as values and all have strings/positions as keys. Hence there can be different places with the same name or category. I'd like to remove a certain place from the value list in each HashMap based on if that place is "marked" or not.

Every added place is stored in all HashMaps, and the marked places are stored in a separate ArrayList. I'm trying to loop through the ArrayList and remove each of its places/values from every HashMap. I do not want to remove the key since there could be other, unmarked places with the same key. Is this possible? At the moment the places don't seem to disappear when I try to remove them.

I tried different methods, the one with the iterator (that might be unnecessary) seems to remove the entire key, while the one with the for loop doesn't remove anything at all. Let me know if I haven't provided enough information to solve this. Thank you.

    private Map<Position, Place> perPosition = new HashMap<>();
    private Map<String, List<Place>> perCategory = new HashMap<>();
    private Map<String, List<Place>> perName = new HashMap<>()

     //method 1
    Iterator<Place> it = markedPlace.iterator();
                 while (it.hasNext()) {

                 Place p = it.next();
                 perPosition.remove(p.getPosition());
                 perName.remove(p.getName());
                 perCategory.remove(p.getCategory());
                 p.setMarked(false);
                 p.setVisible(false);
                 it.remove();
            }

  //method 2
                for (Place p : markedPlace) {
                    for (Map.Entry<String, List<Place>> entry : perName.entrySet()) {
                        if (entry.getValue().equals(p)) {
                            perName.remove(entry.getValue());
                        }
                    }

                    for (Map.Entry<String, List<Place>> entry : perCategory.entrySet()) {
                        if (entry.getValue().equals(p)) {
                            perCategory.remove(entry.getValue());
                        }
                    }

                    for (Map.Entry<Position, Place> entry : perPosition.entrySet()) {
                        if (entry.getValue().equals(p)) {
                            perName.remove(entry.getKey());
                        }

                        }
                   markedPlace.clear();

entry.getValue() is of type List<Place> and p is Type Place . So for the perName and perCategory loops, the equals will never be true. You need to use .contains(foo) instead to check if an object is in the list.

You are then trying to purge the lists you want to keep. You want to remove the entry from the list, not the map.

if (entry.getValue().contains(p)) {
  entry.getValue().remove(p);
}

And then List supports remove collection , so you can simply do

for (Map.Entry<String, List<Place>> entry : perName.entrySet()) {
    entry.getValue().remove(markedPlace);
}

Also, in the Place class, make sure you override equals and hashcode, or else it will only match if both Place objects are the exact same instantiated object.

Java's Map remove method takes a key to remove. In two of those cases you are passing the value not the key.

https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#remove-java.lang.Object-

Two HashMap given below-

hmap2 - >{Competitor=[aaa, bbb, 000, 111], Contractor=[ccc, ddd, 222, 333]}
hmap 1 ->{Competitor=[aaa, bbb], Contractor=[ccc, ddd]}

Below is the code to remove hmap1 from hmap2 - > 

for (Map.Entry<String, List<String>> entry : hmap2.entrySet()) {
            for (Map.Entry<String, List<String>> tobeDeleted : hmap1.entrySet()) {
                List<String> li = tobeDeleted.getValue();
                for (String li1 : li) {
                    entry.getValue().remove(li1);
                }
            }

        }
        System.out.println(hmap2);




Output - >{Competitor=[000, 111], Contractor=[222, 333]}

Happy Learning :)

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