diff --git a/core-java-collections-list/pom.xml b/core-java-collections-list/pom.xml index ee99e470d0..103508b948 100644 --- a/core-java-collections-list/pom.xml +++ b/core-java-collections-list/pom.xml @@ -36,6 +36,12 @@ ${lombok.version} provided + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter.version} + test + @@ -44,5 +50,6 @@ 1.7.0 3.11.1 1.16.12 + 5.0.0-M4 diff --git a/core-java-collections-list/src/main/java/com/baeldung/allequalelements/VerifyAllEqualListElements.java b/core-java-collections-list/src/main/java/com/baeldung/allequalelements/VerifyAllEqualListElements.java new file mode 100644 index 0000000000..033245f239 --- /dev/null +++ b/core-java-collections-list/src/main/java/com/baeldung/allequalelements/VerifyAllEqualListElements.java @@ -0,0 +1,43 @@ +package com.baeldung.allequalelements; + +import java.util.List; +import org.apache.commons.collections4.IterableUtils; +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; + +public class VerifyAllEqualListElements { + + public boolean verifyAllEqualUsingALoop(List list) { + for (String s : list) { + if(!s.equals(list.get(0))) + return false; + } + return true; + } + + public boolean verifyAllEqualUsingStream(List list) { + return list.stream().distinct().limit(2).count() <= 1; + } + + public boolean verifyAllEqualAnotherUsingStream(List list) { + return list.isEmpty() || list.stream().allMatch(list.get(0)::equals); + } + + + public boolean verifyAllEqualUsingGuava(List list) { + return Iterables.all(list, new Predicate() { + public boolean apply(String s) { + return s.equals(list.get(0)); + } + }); + } + + public boolean verifyAllEqualUsingApacheCommon(List list) { + return IterableUtils.matchesAll(list, new org.apache.commons.collections4.Predicate() { + public boolean evaluate(String s) { + return s.equals(list.get(0)); + } + }); + } + +} \ No newline at end of file diff --git a/core-java-collections-list/src/test/java/com/baeldung/allequalelements/VerifyAllEqualListElementsUnitTest.java b/core-java-collections-list/src/test/java/com/baeldung/allequalelements/VerifyAllEqualListElementsUnitTest.java new file mode 100644 index 0000000000..6ebb724580 --- /dev/null +++ b/core-java-collections-list/src/test/java/com/baeldung/allequalelements/VerifyAllEqualListElementsUnitTest.java @@ -0,0 +1,181 @@ +package com.baeldung.allequalelements; + +import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class VerifyAllEqualListElementsUnitTest { + + private static List distinctList = new ArrayList<>(); + + static { + distinctList.add(new String("Jack")); + distinctList.add(new String("James")); + distinctList.add(new String("Sam")); + } + + private static List notAllEqualList = new ArrayList<>(); + + static { + notAllEqualList.add(new String("Jack")); + notAllEqualList.add(new String("James")); + notAllEqualList.add(new String("Sam")); + notAllEqualList.add(new String("James")); + } + + private static List emptyList = new ArrayList<>(); + + private static List allEqualList = new ArrayList<>(); + + static { + allEqualList.add(new String("Jack")); + allEqualList.add(new String("Jack")); + allEqualList.add(new String("Jack")); + allEqualList.add(new String("Jack")); + } + + private static VerifyAllEqualListElements verifyAllEqualListElements = new VerifyAllEqualListElements(); + + @Test + public void verifyAllEqualUsingALoop_whenUsingDistinctList_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingALoop(distinctList); + + assertFalse(allEqual); + } + + @Test + public void verifyAllEqualUsingALoop_whenNotAllEqualList_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingALoop(notAllEqualList); + + assertFalse(allEqual); + } + + @Test + public void verifyAllEqualUsingALoop_whenEmptyList_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingALoop(emptyList); + + assertTrue(allEqual); + } + + @Test + public void verifyAllEqualUsingALoop_whenAllEqualList_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingALoop(allEqualList); + + assertTrue(allEqual); + } + + @Test + public void verifyAllEqualUsingStream_whenUsingDistinctList_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(distinctList); + + assertFalse(allEqual); + } + + @Test + public void verifyAllEqualUsingStream_whenNotAllEqualList_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(notAllEqualList); + + assertFalse(allEqual); + } + + @Test + public void verifyAllEqualUsingStream_whenEmptyList_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(emptyList); + + assertTrue(allEqual); + } + + @Test + public void verifyAllEqualUsingStream_whenAllEqualList_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(allEqualList); + + assertTrue(allEqual); + } + + @Test + public void verifyAllEqualAnotherUsingStream_whenUsingDistinctList_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualAnotherUsingStream(distinctList); + + assertFalse(allEqual); + } + + @Test + public void verifyAllEqualAnotherUsingStream_whenNotAllEqualList_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualAnotherUsingStream(notAllEqualList); + + assertFalse(allEqual); + } + + @Test + public void verifyAllEqualAnotherUsingStream_whenEmptyList_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualAnotherUsingStream(emptyList); + + assertTrue(allEqual); + } + + @Test + public void verifyAllEqualAnotherUsingStream_whenAllEqualList_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualAnotherUsingStream(allEqualList); + + assertTrue(allEqual); + } + + + @Test + public void verifyAllEqualUsingGuava_whenUsingDistinctList_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(distinctList); + + assertFalse(allEqual); + } + + @Test + public void verifyAllEqualUsingGuava_whenNotAllEqualList_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(notAllEqualList); + + assertFalse(allEqual); + } + + @Test + public void verifyAllEqualUsingGuava_whenEmptyList_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(emptyList); + + assertTrue(allEqual); + } + + @Test + public void verifyAllEqualUsingGuava_whenAllEqualList_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(allEqualList); + + assertTrue(allEqual); + } + + @Test + public void verifyAllEqualUsingApacheCommon_whenUsingDistinctList_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingApacheCommon(distinctList); + + assertFalse(allEqual); + } + + @Test + public void verifyAllEqualUsingApacheCommon_whenNotAllEqualList_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingApacheCommon(notAllEqualList); + + assertFalse(allEqual); + } + + @Test + public void verifyAllEqualUsingApacheCommon_whenEmptyList_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingApacheCommon(emptyList); + + assertTrue(allEqual); + } + + @Test + public void verifyAllEqualUsingApacheCommon_whenAllEqualList_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingApacheCommon(allEqualList); + + assertTrue(allEqual); + } +}