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..936e89893c --- /dev/null +++ b/core-java-collections-list/src/main/java/com/baeldung/allequalelements/VerifyAllEqualListElements.java @@ -0,0 +1,55 @@ +package com.baeldung.allequalelements; + +import java.util.Collections; +import java.util.HashSet; +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 verifyAllEqualUsingHashSet(List list) { + return new HashSet(list).size() <= 1; + } + + public boolean verifyAllEqualUsingFrequency(List list) { + return list.isEmpty() || Collections.frequency(list, list.get(0)) == list.size(); + } + + public boolean verifyAllEqualUsingStream(List list) { + return list.stream() + .distinct() + .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..698725591c --- /dev/null +++ b/core-java-collections-list/src/test/java/com/baeldung/allequalelements/VerifyAllEqualListElementsUnitTest.java @@ -0,0 +1,172 @@ +package com.baeldung.allequalelements; + +import org.junit.Test; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class VerifyAllEqualListElementsUnitTest { + + private static List notAllEqualList = new ArrayList<>(); + + private static List emptyList = new ArrayList<>(); + + private static List allEqualList = new ArrayList<>(); + + static { + notAllEqualList = Arrays.asList("Jack", "James", "Sam", "James"); + emptyList = Arrays.asList(); + allEqualList = Arrays.asList("Jack", "Jack", "Jack", "Jack"); + } + + private static VerifyAllEqualListElements verifyAllEqualListElements = new VerifyAllEqualListElements(); + + @Test + public void givenNotAllEqualList_whenUsingALoop_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingALoop(notAllEqualList); + + assertFalse(allEqual); + } + + @Test + public void givenEmptyList_whenUsingALoop_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingALoop(emptyList); + + assertTrue(allEqual); + } + + @Test + public void givenAllEqualList_whenUsingALoop_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingALoop(allEqualList); + + assertTrue(allEqual); + } + + @Test + public void givenNotAllEqualList_whenUsingHashSet_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingHashSet(notAllEqualList); + + assertFalse(allEqual); + } + + @Test + public void givenEmptyList_whenUsingHashSet_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingHashSet(emptyList); + + assertTrue(allEqual); + } + + @Test + public void givenAllEqualList_whenUsingHashSet_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingHashSet(allEqualList); + + assertTrue(allEqual); + } + + @Test + public void givenNotAllEqualList_whenUsingFrequency_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingFrequency(notAllEqualList); + + assertFalse(allEqual); + } + + @Test + public void givenEmptyList_whenUsingFrequency_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingFrequency(emptyList); + + assertTrue(allEqual); + } + + @Test + public void givenAllEqualList_whenUsingFrequency_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingFrequency(allEqualList); + + assertTrue(allEqual); + } + + @Test + public void givenNotAllEqualList_whenUsingStream_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(notAllEqualList); + + assertFalse(allEqual); + } + + @Test + public void givenEmptyList_whenUsingStream_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(emptyList); + + assertTrue(allEqual); + } + + @Test + public void givenAllEqualList_whenUsingStream_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(allEqualList); + + assertTrue(allEqual); + } + + @Test + public void givenNotAllEqualList_whenUsingAnotherStream_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualAnotherUsingStream(notAllEqualList); + + assertFalse(allEqual); + } + + @Test + public void givenEmptyList_whenUsingAnotherStream_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualAnotherUsingStream(emptyList); + + assertTrue(allEqual); + } + + @Test + public void givenAllEqualList_whenUsingAnotherStream_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualAnotherUsingStream(allEqualList); + + assertTrue(allEqual); + } + + @Test + public void givenNotAllEqualList_whenUsingGuava_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(notAllEqualList); + + assertFalse(allEqual); + } + + @Test + public void givenEmptyList_whenUsingGuava_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(emptyList); + + assertTrue(allEqual); + } + + @Test + public void givenAllEqualList_whenUsingGuava_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(allEqualList); + + assertTrue(allEqual); + } + + @Test + public void givenNotAllEqualList_whenUsingApacheCommon_thenReturnFalse() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingApacheCommon(notAllEqualList); + + assertFalse(allEqual); + } + + @Test + public void givenEmptyList_whenUsingApacheCommon_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingApacheCommon(emptyList); + + assertTrue(allEqual); + } + + @Test + public void givenAllEqualList_whenUsingApacheCommon_thenReturnTrue() { + boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingApacheCommon(allEqualList); + + assertTrue(allEqual); + } +}