简体   繁体   English

从使用 List 作为值的 HashMap 中删除 object

[英]Remove object from a HashMap that use List as values

private static final Map<TeamType, List<Player>> LIST = new ConcurrentHashMap<>();

How can i remove a Player object from the LIST?如何从列表中删除播放器 object? My code right now is:我现在的代码是:

for (List<Player> team : LIST.values())
{
    if (team.contains(ObjectIWantToRemove))
    {
        team.remove(ObjectIWantToRemove);
        return;
    }
}

But i want to improve using just a single line.但我想只用一行来改进。

Are you looking to:您是否希望:

LIST.values().forEach(team -> team.remove(ObjectIWantToRemove));

Edit编辑

The question is a little unclear, for that I will put this solution, so if you want to remove ObjectIWantToRemove from the first element which contain it, then you can use stream like this:这个问题有点不清楚,因为我会提出这个解决方案,所以如果你想从包含它的第一个元素中删除ObjectIWantToRemove ,那么你可以像这样使用 stream :

LIST.values().stream()
        .filter(team -> team.contains(ObjectIWantToRemove))
        .findFirst()
        .ifPresent(team -> team.remove(ObjectIWantToRemove));

One thing you could do is this:你可以做的一件事是:

for (List<Player> team : LIST.values()) {
     if (team.remove(ObjectIWantToRemove))
     {
         return;
     }
}

This will avoid the call to contains before removing the element.这将避免在删除元素之前调用contains

If you want to do it in one line, you can do it like that:如果你想在一行中完成,你可以这样做:

LIST.values().forEach(team -> team.remove(ObjectIWantToRemove));

That will remove the player from all teams it belongs to, whereas the solution above only remove it from the first one.这会将玩家从它所属的所有团队中删除,而上面的解决方案仅将其从第一个团队中删除。

If you are looking for a solution that removes it only from the first, there already is an answer.如果您正在寻找一种仅从第一个中删除它的解决方案,那么已经有了答案。

Try this.尝试这个。

LIST.values().stream().filter(team -> team.remove(ObjectIWantToRemove)).findFirst();

List.remove(Object) returns true if the team contained the ObjectIWantToRemove .如果团队包含ObjectIWantToRemove ,则 List.remove List.remove(Object)返回 true。 This expression selects only the first team that contain ObjectIWantToRemove .此表达式仅选择包含ObjectIWantToRemove的第一个团队。

This uses Integers and Strings to demonstrate but could also be applied to your classes.这使用整数和字符串来演示,但也可以应用于您的类。

LIST.put("A", new ArrayList<>(List.of(1,2,3,4,2,3,1)));
LIST.put("B", new ArrayList<>(List.of(1,2, 9, 10,2)));
LIST.put("C", new ArrayList<>(List.of(1,2,5,2, 9, 1)));
LIST.put("D", new ArrayList<>(List.of(1,3,2,4,2)));


Integer ObjectToRemove = 2;

System.out.println("Before remove");
LIST.entrySet().forEach(System.out::println);


LIST.forEach((k,v)->v.removeIf(r->r.equals(ObjectToRemove)));

System.out.println("After remove");
LIST.entrySet().forEach(System.out::println);

Prints印刷

Before remove
A=[1, 2, 3, 4, 2, 3, 1]
B=[1, 2, 9, 10, 2]
C=[1, 2, 5, 2, 9, 1]
D=[1, 3, 2, 4, 2]
After remove
A=[1, 3, 4, 3, 1]
B=[1, 9, 10]
C=[1, 5, 9, 1]
D=[1, 3, 4]

Even though this removes all Objects, I would assume (perhaps wrongly) that a player would not be listed twice for the same team.尽管这会删除所有对象,但我会假设(可能是错误的)一个球员不会为同一支球队列出两次。 If you just want to remove the first one encountered, then use this construct.如果您只想删除遇到的第一个,请使用此构造。

LIST.forEach((k,v)->v.remove(ObjectToRemove));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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