From 956660497a08fda46057c0218d33e928a939bc50 Mon Sep 17 00:00:00 2001 From: Sam Gardner Date: Fri, 12 May 2023 11:58:08 +0100 Subject: [PATCH] BAEL-6334 Add example code for checking if a list element is in another list --- .../core-java-collections-list-5/pom.xml | 13 ++++ .../main/java/com/baeldung/list/Country.java | 11 +++ .../ListContainsElementFromOtherListTest.java | 68 +++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 core-java-modules/core-java-collections-list-5/src/main/java/com/baeldung/list/Country.java create mode 100644 core-java-modules/core-java-collections-list-5/src/test/java/com/baeldung/java/list/ListContainsElementFromOtherListTest.java diff --git a/core-java-modules/core-java-collections-list-5/pom.xml b/core-java-modules/core-java-collections-list-5/pom.xml index b51dda4091..3c734b8922 100644 --- a/core-java-modules/core-java-collections-list-5/pom.xml +++ b/core-java-modules/core-java-collections-list-5/pom.xml @@ -24,6 +24,19 @@ commons-lang3 ${commons-lang3.version} + + + org.apache.commons + commons-collections4 + 4.4 + + + + org.projectlombok + lombok + 1.18.26 + provided + diff --git a/core-java-modules/core-java-collections-list-5/src/main/java/com/baeldung/list/Country.java b/core-java-modules/core-java-collections-list-5/src/main/java/com/baeldung/list/Country.java new file mode 100644 index 0000000000..354aeca2d3 --- /dev/null +++ b/core-java-modules/core-java-collections-list-5/src/main/java/com/baeldung/list/Country.java @@ -0,0 +1,11 @@ +package com.baeldung.list; + +import lombok.Data; + +@Data +public class Country { + + private final String name; + private final String capital; + +} diff --git a/core-java-modules/core-java-collections-list-5/src/test/java/com/baeldung/java/list/ListContainsElementFromOtherListTest.java b/core-java-modules/core-java-collections-list-5/src/test/java/com/baeldung/java/list/ListContainsElementFromOtherListTest.java new file mode 100644 index 0000000000..e69cc733ee --- /dev/null +++ b/core-java-modules/core-java-collections-list-5/src/test/java/com/baeldung/java/list/ListContainsElementFromOtherListTest.java @@ -0,0 +1,68 @@ +package com.baeldung.java.list; + +import static java.util.stream.Collectors.toSet; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.apache.commons.collections4.CollectionUtils; +import org.junit.Test; + +import com.baeldung.list.Country; + +public class ListContainsElementFromOtherListTest { + + final private List listOfLetters = Arrays.asList("a", "b", "c", "d"); + final private List listOfLettersWithOverlap = Arrays.asList("d", "e", "f", "g"); + final private List listOfCities = Arrays.asList("London", "Berlin", "Paris", "Brussels"); + + @Test + public void givenWholeValuesToCompare_whenUsingCollectionsDisjoint_thenDetectElementsInTwoLists() { + boolean shouldBeTrue = !Collections.disjoint(listOfLetters, listOfLettersWithOverlap); + assertTrue(shouldBeTrue); + + boolean shouldBeFalse = !Collections.disjoint(listOfLetters, listOfCities); + assertFalse(shouldBeFalse); + } + + @Test + public void givenWholeValuesToCompare_whenUsingStreams_thenDetectElementsInTwoLists() { + boolean shouldBeTrue = listOfLetters.stream() + .anyMatch(listOfLettersWithOverlap::contains); + assertTrue(shouldBeTrue); + + boolean shouldBeFalse = listOfLetters.stream() + .anyMatch(listOfCities::contains); + assertFalse(shouldBeFalse); + } + + @Test + public void givenWholeValuesToCompare_whenUsingApacheCollectionUtils_thenDetectElementsInTwoLists() { + boolean shouldBeTrue = CollectionUtils.containsAny(listOfLetters, listOfLettersWithOverlap); + assertTrue(shouldBeTrue); + + boolean shouldBeFalse = CollectionUtils.containsAny(listOfLetters, listOfCities); + assertFalse(shouldBeFalse); + } + + @Test + public void givenWholePorpertiesInObjectsToCompare_whenUsingStreams_thenDetectElementsInTwoLists() { + Country france = new Country("France", "Paris"); + Country belgium = new Country("Belgium", "Brussels"); + Country spain = new Country("Spain", "Madrid"); + List franceAndBelgium = Arrays.asList(france, belgium); + List belgiumAndSpain = Arrays.asList(belgium, spain); + + boolean shouldBeTrue = franceAndBelgium.stream() + .map(Country::getCapital) + .anyMatch(belgiumAndSpain.stream() + .map(Country::getCapital) + .collect(toSet())::contains); + + assertTrue(shouldBeTrue); + } + +}