Skip to content

Commit 555409b

Browse files
author
caroline
committed
Source Code for BAEL-2534 : Determine if all elements are the same in a Java list
1 parent 73f2a12 commit 555409b

3 files changed

Lines changed: 78 additions & 29 deletions

File tree

‎core-java-collections-list/pom.xml‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@
3636
<version>${lombok.version}</version>
3737
<scope>provided</scope>
3838
</dependency>
39-
<dependency>
40-
<groupId>org.junit.jupiter</groupId>
41-
<artifactId>junit-jupiter-api</artifactId>
42-
<version>${junit-jupiter.version}</version>
43-
<scope>test</scope>
44-
</dependency>
4539
</dependencies>
4640

4741
<properties>
@@ -50,6 +44,5 @@
5044
<avaitility.version>1.7.0</avaitility.version>
5145
<assertj.version>3.11.1</assertj.version>
5246
<lombok.version>1.16.12</lombok.version>
53-
<junit-jupiter.version>5.0.0-M4</junit-jupiter.version>
5447
</properties>
5548
</project>

‎core-java-collections-list/src/main/java/com/baeldung/allequalelements/VerifyAllEqualListElements.java‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.baeldung.allequalelements;
22

3+
import java.util.Collections;
4+
import java.util.HashSet;
35
import java.util.List;
46
import org.apache.commons.collections4.IterableUtils;
57
import com.google.common.base.Predicate;
@@ -9,21 +11,32 @@ public class VerifyAllEqualListElements {
911

1012
public boolean verifyAllEqualUsingALoop(List<String> list) {
1113
for (String s : list) {
12-
if(!s.equals(list.get(0)))
14+
if (!s.equals(list.get(0)))
1315
return false;
1416
}
1517
return true;
1618
}
1719

20+
public boolean verifyAllEqualUsingHashSet(List<String> list) {
21+
return new HashSet<String>(list).size() <= 1;
22+
}
23+
24+
public boolean verifyAllEqualUsingFrequency(List<String> list) {
25+
return list.isEmpty() || Collections.frequency(list, list.get(0)) == list.size();
26+
}
27+
1828
public boolean verifyAllEqualUsingStream(List<String> list) {
19-
return list.stream().distinct().limit(2).count() <= 1;
29+
return list.stream()
30+
.distinct()
31+
.limit(2)
32+
.count() <= 1;
2033
}
2134

2235
public boolean verifyAllEqualAnotherUsingStream(List<String> list) {
23-
return list.isEmpty() || list.stream().allMatch(list.get(0)::equals);
36+
return list.isEmpty() || list.stream()
37+
.allMatch(list.get(0)::equals);
2438
}
2539

26-
2740
public boolean verifyAllEqualUsingGuava(List<String> list) {
2841
return Iterables.all(list, new Predicate<String>() {
2942
public boolean apply(String s) {

‎core-java-collections-list/src/test/java/com/baeldung/allequalelements/VerifyAllEqualListElementsUnitTest.java‎

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Test;
44
import java.util.ArrayList;
5+
import java.util.Arrays;
56
import java.util.List;
67
import static org.junit.Assert.assertFalse;
78
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -10,30 +11,17 @@ public class VerifyAllEqualListElementsUnitTest {
1011

1112
private static List<String> distinctList = new ArrayList<>();
1213

13-
static {
14-
distinctList.add(new String("Jack"));
15-
distinctList.add(new String("James"));
16-
distinctList.add(new String("Sam"));
17-
}
18-
1914
private static List<String> notAllEqualList = new ArrayList<>();
2015

21-
static {
22-
notAllEqualList.add(new String("Jack"));
23-
notAllEqualList.add(new String("James"));
24-
notAllEqualList.add(new String("Sam"));
25-
notAllEqualList.add(new String("James"));
26-
}
27-
2816
private static List<String> emptyList = new ArrayList<>();
2917

3018
private static List<String> allEqualList = new ArrayList<>();
3119

3220
static {
33-
allEqualList.add(new String("Jack"));
34-
allEqualList.add(new String("Jack"));
35-
allEqualList.add(new String("Jack"));
36-
allEqualList.add(new String("Jack"));
21+
distinctList = Arrays.asList("Jack", "James", "Sam");
22+
notAllEqualList = Arrays.asList("Jack", "James", "Sam", "James");
23+
emptyList = Arrays.asList();
24+
allEqualList = Arrays.asList("Jack", "Jack", "Jack", "Jack");
3725
}
3826

3927
private static VerifyAllEqualListElements verifyAllEqualListElements = new VerifyAllEqualListElements();
@@ -66,6 +54,62 @@ public void verifyAllEqualUsingALoop_whenAllEqualList_thenReturnTrue() {
6654
assertTrue(allEqual);
6755
}
6856

57+
@Test
58+
public void verifyAllEqualUsingHashSet_whenUsingDistinctList_thenReturnFalse() {
59+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingHashSet(distinctList);
60+
61+
assertFalse(allEqual);
62+
}
63+
64+
@Test
65+
public void verifyAllEqualUsingHashSet_whenNotAllEqualList_thenReturnFalse() {
66+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingHashSet(notAllEqualList);
67+
68+
assertFalse(allEqual);
69+
}
70+
71+
@Test
72+
public void verifyAllEqualUsingHashSet_whenEmptyList_thenReturnTrue() {
73+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingHashSet(emptyList);
74+
75+
assertTrue(allEqual);
76+
}
77+
78+
@Test
79+
public void verifyAllEqualUsingHashSet_whenAllEqualList_thenReturnTrue() {
80+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingHashSet(allEqualList);
81+
82+
assertTrue(allEqual);
83+
}
84+
85+
@Test
86+
public void verifyAllEqualUsingFrequency_whenUsingDistinctList_thenReturnFalse() {
87+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingFrequency(distinctList);
88+
89+
assertFalse(allEqual);
90+
}
91+
92+
@Test
93+
public void verifyAllEqualUsingFrequency_whenNotAllEqualList_thenReturnFalse() {
94+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingFrequency(notAllEqualList);
95+
96+
assertFalse(allEqual);
97+
}
98+
99+
@Test
100+
public void verifyAllEqualUsingFrequency_whenEmptyList_thenReturnTrue() {
101+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingFrequency(emptyList);
102+
103+
assertTrue(allEqual);
104+
}
105+
106+
@Test
107+
public void verifyAllEqualUsingFrequency_whenAllEqualList_thenReturnTrue() {
108+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingFrequency(allEqualList);
109+
110+
assertTrue(allEqual);
111+
}
112+
69113
@Test
70114
public void verifyAllEqualUsingStream_whenUsingDistinctList_thenReturnFalse() {
71115
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(distinctList);
@@ -122,7 +166,6 @@ public void verifyAllEqualAnotherUsingStream_whenAllEqualList_thenReturnTrue() {
122166
assertTrue(allEqual);
123167
}
124168

125-
126169
@Test
127170
public void verifyAllEqualUsingGuava_whenUsingDistinctList_thenReturnFalse() {
128171
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(distinctList);

0 commit comments

Comments
 (0)