简体   繁体   English

检查列表是否包含相同的对象

[英]Check if lists contain same objects

I have two filled lists. 我有两个填充列表。 The first list contains for example: 第一个列表包含例如:

"Shoppinglist-fruit", "Shoppinglist-drinks", "Shoppinglist-dinner"

The second list contains: 第二个列表包含:

"Shoppinglist-drinks"

Now i wanna print all items in the first list, except if there's a same object in the second list with the same name (Shoppinglist-drinks). 现在,我要打印第一个列表中的所有项目,除非第二个列表中有相同名称的对象(Shoppinglist-drinks)。 Looking like: "Shoppinglist-fruit", "Shoppinglist-dinner" 看起来像: "Shoppinglist-fruit", "Shoppinglist-dinner"

So how can i check if the name of the object inside the second list is also in one of the objects of the first list. 因此,如何检查第二个列表内的对象名称是否也在第一个列表的对象中。 Eventually i want to end up with a string containing all the names of the shoppinglists that are in the first list and not in the second one. 最终我想以一个字符串结尾,该字符串包含第一个列表中而不是第二个列表中购物清单的所有名称。 I started with some code below but i haven't been able to finish it. 我从下面的一些代码开始,但是我无法完成它。

I have the two lists, one called listShoppinglists, this is a list filled with different shopping lists. 我有两个列表,一个叫做listShoppinglists,这是一个充满不同购物列表的列表。 And the second list filled with somebody's shoppinglists. 第二个列表中包含某人的购物清单。 So i wanna check if the name of the shoppinglists are equal. 所以我想检查购物清单的名称是否相等。 If done that by doing so. 如果这样做的话。

public String getAllShoppingLists(List listShoppinglists, Customer customer, List shoppinglists) { 
String namesOfTheShoppingListNames = ""
for (Shoppinglist shoppinglist : listShoppinglists) {
       for (int i = 0; i < customer.shoppinglists.size(); i++) {
           if (customer.shoppinglists.get(i).getName().equals(shoppinglist.getName())) { 
    // Some action here    
           } 
       }    
    } 
return namesOfTheShoppingListNames;
}

You can try this: 您可以尝试以下方法:

    List<ShoopingList> firstShoppingListNames = new ArrayList<>();
    firstShoppingListNames.add(new ShoppingList("fruit"));
    firstShoppingListNames.add(new ShoppingList("dinner"));
    firstShoppingListNames.add(new ShoppingList("juice"));
    List<ShoppingList> secondShoppingListNames = new ArrayList<>();
    secondShoppingListNames.add(new ShoppingList("fruit"));


    List<ShoppingList> distinct = firstShoppingListNames.stream().
            filter( list -> secondShoppingListNames.stream().
            noneMatch(o -> o.getName().equals(list.getName()))).
            collect(Collectors.toList());

    distinct.forEach(o -> System.out.print(o.getName()));

In this case you are using stream to achieve what you want. 在这种情况下,您将使用流来实现所需的功能。 You filter first list to obtain those elements, which are not present in other list. 您过滤第一个列表以获得那些元素,这些元素在其他列表中不存在。

Additionaly if you want to obtain only names of those lists you can use map: 另外,如果您只想获取这些列表的名称,则可以使用map:

    List<String> distinct = firstShoppingListNames.stream().
             filter( list -> secondShoppingListNames.stream().
             noneMatch(o -> o.getName().equals(list.getName()))).
             map(ShoppingList::getName).collect(Collectors.toList());

Use Collections.removeAll() method to do this. 使用Collections.removeAll()方法执行此操作。 Quoted from JavaDoc:- 引用JavaDoc:-

Removes all of this collection's elements that are also contained in the specified collection (optional operation). 删除指定集合中也包含的所有此集合元素(可选操作)。 After this call returns, this collection will contain no elements in common with the specified collection 此调用返回后,此集合将不包含与指定集合相同的元素

.List<String> list1=new ArrayList<String>();
        list1.add("Shoppinglist-fruit");list1.add("Shoppinglist-drinks");list1.add("Shoppinglist-dinner");
        List<String> list2=new ArrayList<String>();
        list2.add("Shoppinglist-drinks");
        list1.removeAll(list2);
        System.out.println(list1);

//Output:- [Shoppinglist-fruit, Shoppinglist-dinner]

In case, lists contains a custom objects, override equals and hashcode methods in that custom object. 如果列表包含一个自定义对象,则在该自定义对象中覆盖等于和哈希码方法

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

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