Source Code for BAEL-2534 : Determine if all elements are the same in a Java list
This commit is contained in:
parent
73f2a12c4e
commit
555409b78e
@ -36,12 +36,6 @@
|
|||||||
<version>${lombok.version}</version>
|
<version>${lombok.version}</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.junit.jupiter</groupId>
|
|
||||||
<artifactId>junit-jupiter-api</artifactId>
|
|
||||||
<version>${junit-jupiter.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
@ -50,6 +44,5 @@
|
|||||||
<avaitility.version>1.7.0</avaitility.version>
|
<avaitility.version>1.7.0</avaitility.version>
|
||||||
<assertj.version>3.11.1</assertj.version>
|
<assertj.version>3.11.1</assertj.version>
|
||||||
<lombok.version>1.16.12</lombok.version>
|
<lombok.version>1.16.12</lombok.version>
|
||||||
<junit-jupiter.version>5.0.0-M4</junit-jupiter.version>
|
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.baeldung.allequalelements;
|
package com.baeldung.allequalelements;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.apache.commons.collections4.IterableUtils;
|
import org.apache.commons.collections4.IterableUtils;
|
||||||
import com.google.common.base.Predicate;
|
import com.google.common.base.Predicate;
|
||||||
@ -9,21 +11,32 @@ public class VerifyAllEqualListElements {
|
|||||||
|
|
||||||
public boolean verifyAllEqualUsingALoop(List<String> list) {
|
public boolean verifyAllEqualUsingALoop(List<String> list) {
|
||||||
for (String s : list) {
|
for (String s : list) {
|
||||||
if(!s.equals(list.get(0)))
|
if (!s.equals(list.get(0)))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
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) {
|
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) {
|
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) {
|
public boolean verifyAllEqualUsingGuava(List<String> list) {
|
||||||
return Iterables.all(list, new Predicate<String>() {
|
return Iterables.all(list, new Predicate<String>() {
|
||||||
public boolean apply(String s) {
|
public boolean apply(String s) {
|
||||||
|
@ -2,6 +2,7 @@ package com.baeldung.allequalelements;
|
|||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
@ -10,30 +11,17 @@ public class VerifyAllEqualListElementsUnitTest {
|
|||||||
|
|
||||||
private static List<String> distinctList = new ArrayList<>();
|
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<>();
|
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> emptyList = new ArrayList<>();
|
||||||
|
|
||||||
private static List<String> allEqualList = new ArrayList<>();
|
private static List<String> allEqualList = new ArrayList<>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
allEqualList.add(new String("Jack"));
|
distinctList = Arrays.asList("Jack", "James", "Sam");
|
||||||
allEqualList.add(new String("Jack"));
|
notAllEqualList = Arrays.asList("Jack", "James", "Sam", "James");
|
||||||
allEqualList.add(new String("Jack"));
|
emptyList = Arrays.asList();
|
||||||
allEqualList.add(new String("Jack"));
|
allEqualList = Arrays.asList("Jack", "Jack", "Jack", "Jack");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static VerifyAllEqualListElements verifyAllEqualListElements = new VerifyAllEqualListElements();
|
private static VerifyAllEqualListElements verifyAllEqualListElements = new VerifyAllEqualListElements();
|
||||||
@ -66,6 +54,62 @@ public class VerifyAllEqualListElementsUnitTest {
|
|||||||
assertTrue(allEqual);
|
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
|
@Test
|
||||||
public void verifyAllEqualUsingStream_whenUsingDistinctList_thenReturnFalse() {
|
public void verifyAllEqualUsingStream_whenUsingDistinctList_thenReturnFalse() {
|
||||||
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(distinctList);
|
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(distinctList);
|
||||||
@ -122,7 +166,6 @@ public class VerifyAllEqualListElementsUnitTest {
|
|||||||
assertTrue(allEqual);
|
assertTrue(allEqual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyAllEqualUsingGuava_whenUsingDistinctList_thenReturnFalse() {
|
public void verifyAllEqualUsingGuava_whenUsingDistinctList_thenReturnFalse() {
|
||||||
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(distinctList);
|
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(distinctList);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user