Source Code for BAEL-2534 : Determine if all elements are the same in a Java list

This commit is contained in:
caroline
2019-02-01 21:34:08 +01:00
parent 73f2a12c4e
commit 555409b78e
3 changed files with 78 additions and 29 deletions

View File

@@ -1,5 +1,7 @@
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;
@@ -9,21 +11,32 @@ public class VerifyAllEqualListElements {
public boolean verifyAllEqualUsingALoop(List<String> list) {
for (String s : list) {
if(!s.equals(list.get(0)))
if (!s.equals(list.get(0)))
return false;
}
return true;
}
public boolean verifyAllEqualUsingHashSet(List<String> list) {
return new HashSet<String>(list).size() <= 1;
}
public boolean verifyAllEqualUsingFrequency(List<String> list) {
return list.isEmpty() || Collections.frequency(list, list.get(0)) == list.size();
}
public boolean verifyAllEqualUsingStream(List<String> list) {
return list.stream().distinct().limit(2).count() <= 1;
return list.stream()
.distinct()
.limit(2)
.count() <= 1;
}
public boolean verifyAllEqualAnotherUsingStream(List<String> list) {
return list.isEmpty() || list.stream().allMatch(list.get(0)::equals);
return list.isEmpty() || list.stream()
.allMatch(list.get(0)::equals);
}
public boolean verifyAllEqualUsingGuava(List<String> list) {
return Iterables.all(list, new Predicate<String>() {
public boolean apply(String s) {

View File

@@ -2,6 +2,7 @@ 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;
@@ -10,30 +11,17 @@ public class VerifyAllEqualListElementsUnitTest {
private static List<String> distinctList = new ArrayList<>();
static {
distinctList.add(new String("Jack"));
distinctList.add(new String("James"));
distinctList.add(new String("Sam"));
}
private static List<String> 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<String> emptyList = new ArrayList<>();
private static List<String> allEqualList = new ArrayList<>();
static {
allEqualList.add(new String("Jack"));
allEqualList.add(new String("Jack"));
allEqualList.add(new String("Jack"));
allEqualList.add(new String("Jack"));
distinctList = Arrays.asList("Jack", "James", "Sam");
notAllEqualList = Arrays.asList("Jack", "James", "Sam", "James");
emptyList = Arrays.asList();
allEqualList = Arrays.asList("Jack", "Jack", "Jack", "Jack");
}
private static VerifyAllEqualListElements verifyAllEqualListElements = new VerifyAllEqualListElements();
@@ -66,6 +54,62 @@ public class VerifyAllEqualListElementsUnitTest {
assertTrue(allEqual);
}
@Test
public void verifyAllEqualUsingHashSet_whenUsingDistinctList_thenReturnFalse() {
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingHashSet(distinctList);
assertFalse(allEqual);
}
@Test
public void verifyAllEqualUsingHashSet_whenNotAllEqualList_thenReturnFalse() {
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingHashSet(notAllEqualList);
assertFalse(allEqual);
}
@Test
public void verifyAllEqualUsingHashSet_whenEmptyList_thenReturnTrue() {
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingHashSet(emptyList);
assertTrue(allEqual);
}
@Test
public void verifyAllEqualUsingHashSet_whenAllEqualList_thenReturnTrue() {
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingHashSet(allEqualList);
assertTrue(allEqual);
}
@Test
public void verifyAllEqualUsingFrequency_whenUsingDistinctList_thenReturnFalse() {
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingFrequency(distinctList);
assertFalse(allEqual);
}
@Test
public void verifyAllEqualUsingFrequency_whenNotAllEqualList_thenReturnFalse() {
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingFrequency(notAllEqualList);
assertFalse(allEqual);
}
@Test
public void verifyAllEqualUsingFrequency_whenEmptyList_thenReturnTrue() {
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingFrequency(emptyList);
assertTrue(allEqual);
}
@Test
public void verifyAllEqualUsingFrequency_whenAllEqualList_thenReturnTrue() {
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingFrequency(allEqualList);
assertTrue(allEqual);
}
@Test
public void verifyAllEqualUsingStream_whenUsingDistinctList_thenReturnFalse() {
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(distinctList);
@@ -122,7 +166,6 @@ public class VerifyAllEqualListElementsUnitTest {
assertTrue(allEqual);
}
@Test
public void verifyAllEqualUsingGuava_whenUsingDistinctList_thenReturnFalse() {
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(distinctList);