Merge pull request #14003 from sam-gardner/BAEL-6334-check-list-element-in-other-list

BAEL-6334 Add example code for checking if a list element is in anoth…
This commit is contained in:
Vini 2023-05-18 13:51:45 +02:00 committed by GitHub
commit 9bb795bfd3
3 changed files with 130 additions and 40 deletions

View File

@ -1,41 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-list-5</artifactId>
<name>core-java-collections-list-5</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.36</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<properties>
<jmh.version>1.21</jmh.version>
<commons-lang.version>2.2</commons-lang.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
</properties>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-list-5</artifactId>
<name>core-java-collections-list-5</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.36</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<jmh.version>1.21</jmh.version>
<commons-lang.version>2.2</commons-lang.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
</properties>
</project>

View File

@ -0,0 +1,11 @@
package com.baeldung.list;
import lombok.Data;
@Data
public class Country {
private final String name;
private final String capital;
}

View File

@ -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<String> listOfLetters = Arrays.asList("a", "b", "c", "d");
final private List<String> listOfLettersWithOverlap = Arrays.asList("d", "e", "f", "g");
final private List<String> listOfCities = Arrays.asList("London", "Berlin", "Paris", "Brussels");
@Test
public void givenValuesToCompare_whenUsingCollectionsDisjoint_thenDetectElementsInTwoLists() {
boolean shouldBeTrue = !Collections.disjoint(listOfLetters, listOfLettersWithOverlap);
assertTrue(shouldBeTrue);
boolean shouldBeFalse = !Collections.disjoint(listOfLetters, listOfCities);
assertFalse(shouldBeFalse);
}
@Test
public void givenValuesToCompare_whenUsingStreams_thenDetectElementsInTwoLists() {
boolean shouldBeTrue = listOfLetters.stream()
.anyMatch(listOfLettersWithOverlap::contains);
assertTrue(shouldBeTrue);
boolean shouldBeFalse = listOfLetters.stream()
.anyMatch(listOfCities::contains);
assertFalse(shouldBeFalse);
}
@Test
public void givenValuesToCompare_whenUsingApacheCollectionUtils_thenDetectElementsInTwoLists() {
boolean shouldBeTrue = CollectionUtils.containsAny(listOfLetters, listOfLettersWithOverlap);
assertTrue(shouldBeTrue);
boolean shouldBeFalse = CollectionUtils.containsAny(listOfLetters, listOfCities);
assertFalse(shouldBeFalse);
}
@Test
public void givenPropertiesInObjectsToCompare_whenUsingStreams_thenDetectElementsInTwoLists() {
Country france = new Country("France", "Paris");
Country belgium = new Country("Belgium", "Brussels");
Country spain = new Country("Spain", "Madrid");
List<Country> franceAndBelgium = Arrays.asList(france, belgium);
List<Country> belgiumAndSpain = Arrays.asList(belgium, spain);
boolean shouldBeTrue = franceAndBelgium.stream()
.map(Country::getCapital)
.anyMatch(belgiumAndSpain.stream()
.map(Country::getCapital)
.collect(toSet())::contains);
assertTrue(shouldBeTrue);
}
}