Java 如何从地图中的列表中删除特定对象?

Java 如何从地图中的列表中删除特定对象?,java,Java,我有一个类似这样的映射:HashMap。 我想删除列表中的一些项目,我想删除的特定项目存储在另一个列表中。如何以最有效的方式删除映射中与此其他列表中的项目匹配的列表项目 List已经提供了一个方法:List.removeAll(集合) 当然,在这种情况下,使用LinkedList可能更可取,因为对于ArrayListHashMap=//您的映射,删除元素是一个O(1)操作,而不是O(n)操作 HashMap<Person, List<Items>> map = // Yo

我有一个类似这样的映射:
HashMap

我想删除列表中的一些项目,我想删除的特定项目存储在另一个列表中。如何以最有效的方式删除映射中与此其他列表中的项目匹配的列表项目

List已经提供了一个方法:
List.removeAll(集合)

当然,在这种情况下,使用
LinkedList
可能更可取,因为对于
ArrayList

HashMap=//您的映射,删除元素是一个O(1)操作,而不是O(n)操作
HashMap<Person, List<Items>> map = // Your map
for(Person p:map.keySet()) {
    map.get(p).removeAll(removeList);
}
for(Person p:map.keySet()){ map.get(p.removeAll(removeList); }

这应该行得通。

这里有一个完整的例子来演示您的情况。请注意,
hashcode和equals是在
类上实现的。这一点很重要,因为
removeAll
方法将使用它来确定提供的项目列表是否等于拥有的项目列表

Item.java

public class Item {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Item(String name) {
        super();
        this.name = name;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Item other = (Item) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }


}
public class Person {

    private String name;


    public Person(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
Person.java

public class Item {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Item(String name) {
        super();
        this.name = name;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Item other = (Item) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }


}
public class Person {

    private String name;


    public Person(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
应用程序

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


    public class StackTest {
        public static void main(String[] args) {
            Person person = new Person("Logan");
            Person person2 = new Person("Jean");
            Person person3 = new Person("Gambit");
            Person person4 = new Person("Storm");

            Item item1 = new Item("Claws");
            Item item2 = new Item("Jacket");
            Item item3 = new Item("Cards");
            Item item4 = new Item("Cape");

            List<Item> items = new ArrayList<Item>();
            items.add(item1);
            items.add(item2);
            items.add(item3);
            items.add(item4);

            List<Item> loganItems = new ArrayList<Item>(items);
            List<Item> jeanItems = new ArrayList<Item>(items);
            List<Item> gambitItems = new ArrayList<Item>(items);
            List<Item> stormItems = new ArrayList<Item>(items);

            Map<Person, List<Item>> people = new HashMap<Person,List<Item>>();
            people.put(person, loganItems);
            people.put(person2, jeanItems);
            people.put(person3, gambitItems);
            people.put(person4, stormItems);

            printMap(people);

            List<Item> removeItems = new ArrayList<Item>();
            Item rItem1 = new Item("Cards");
            Item rItem2 = new Item("Jacket");
            removeItems.add(rItem1);
            removeItems.add(rItem2);

            removeItem(people, person, removeItems);

            printMap(people);

        }

        public static void removeItem(Map<Person,List<Item>> map, Person p, List<Item> items){
            map.get(p).removeAll(items);
        }

        public static void printMap(Map<Person, List<Item>> map){
            for(Entry<Person,List<Item>> entry:map.entrySet()){
                System.out.println(entry.getKey().getName() + " items:");
                for(Item item: entry.getValue()){
                    System.out.println(item.getName());
                }
            }
        }
    }
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.Map.Entry;
公共类堆栈测试{
公共静态void main(字符串[]args){
人员=新人员(“Logan”);
人员2=新人员(“Jean”);
人员3=新人员(“策略”);
人员4=新人员(“风暴”);
项目1=新项目(“爪”);
项目2=新项目(“护套”);
项目3=新项目(“卡片”);
项目4=新项目(“Cape”);
列表项=新建ArrayList();
增加(第1项);
增加(第2项);
增加(第3项);
增加(第4项);
列表日志项=新的ArrayList(项);
List jeanItems=新阵列列表(项目);
列表gambitItems=新的ArrayList(项目);
List stormItems=新阵列列表(项目);
Map people=newhashmap();
人。放(人,loganItems);
人。放(人2,珍妮);
人。放(人3,赌博);
人物。放置(人物4,风暴物品);
印刷地图(人);
List removitems=new ArrayList();
项目1=新项目(“卡片”);
项目2=新项目(“护套”);
删除项目。添加(rItem1);
删除项目。添加(m2);
removeItem(人、人、removeItems);
印刷地图(人);
}
公共静态void removietem(地图地图、人员p、列表项){
map.get(p.removeAll(项目);
}
公共静态无效打印地图(地图地图){
for(条目:map.entrySet()){
System.out.println(entry.getKey().getName()+“items:”);
对于(项:entry.getValue()){
System.out.println(item.getName());
}
}
}
}

迭代地图中的所有键,然后使用列表中的removeAll方法。非常感谢您的全面回答!在equals和hashcode方面有点困难,但现在工作正常:)