Merge pull request #6263 from cscib/master

BAEL-2534 - Determine if all elements are the same in a Java list
This commit is contained in:
Tom Hombergs 2019-02-14 20:05:34 +01:00 committed by GitHub
commit 6bdf463da9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 227 additions and 0 deletions

View File

@ -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<String> list) {
for (String s : list) {
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()
.count() <= 1;
}
public boolean verifyAllEqualAnotherUsingStream(List<String> list) {
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) {
return s.equals(list.get(0));
}
});
}
public boolean verifyAllEqualUsingApacheCommon(List<String> list) {
return IterableUtils.matchesAll(list, new org.apache.commons.collections4.Predicate<String>() {
public boolean evaluate(String s) {
return s.equals(list.get(0));
}
});
}
}

View File

@ -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<String> notAllEqualList = new ArrayList<>();
private static List<String> emptyList = new ArrayList<>();
private static List<String> 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);
}
}