简体   繁体   中英

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). Looking like: "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. 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:

    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. Quoted from 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.

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