Skip to content

Commit 73f2a12

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

3 files changed

Lines changed: 231 additions & 0 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
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>
3945
</dependencies>
4046

4147
<properties>
@@ -44,5 +50,6 @@
4450
<avaitility.version>1.7.0</avaitility.version>
4551
<assertj.version>3.11.1</assertj.version>
4652
<lombok.version>1.16.12</lombok.version>
53+
<junit-jupiter.version>5.0.0-M4</junit-jupiter.version>
4754
</properties>
4855
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.allequalelements;
2+
3+
import java.util.List;
4+
import org.apache.commons.collections4.IterableUtils;
5+
import com.google.common.base.Predicate;
6+
import com.google.common.collect.Iterables;
7+
8+
public class VerifyAllEqualListElements {
9+
10+
public boolean verifyAllEqualUsingALoop(List<String> list) {
11+
for (String s : list) {
12+
if(!s.equals(list.get(0)))
13+
return false;
14+
}
15+
return true;
16+
}
17+
18+
public boolean verifyAllEqualUsingStream(List<String> list) {
19+
return list.stream().distinct().limit(2).count() <= 1;
20+
}
21+
22+
public boolean verifyAllEqualAnotherUsingStream(List<String> list) {
23+
return list.isEmpty() || list.stream().allMatch(list.get(0)::equals);
24+
}
25+
26+
27+
public boolean verifyAllEqualUsingGuava(List<String> list) {
28+
return Iterables.all(list, new Predicate<String>() {
29+
public boolean apply(String s) {
30+
return s.equals(list.get(0));
31+
}
32+
});
33+
}
34+
35+
public boolean verifyAllEqualUsingApacheCommon(List<String> list) {
36+
return IterableUtils.matchesAll(list, new org.apache.commons.collections4.Predicate<String>() {
37+
public boolean evaluate(String s) {
38+
return s.equals(list.get(0));
39+
}
40+
});
41+
}
42+
43+
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package com.baeldung.allequalelements;
2+
3+
import org.junit.Test;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import static org.junit.Assert.assertFalse;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
9+
public class VerifyAllEqualListElementsUnitTest {
10+
11+
private static List<String> distinctList = new ArrayList<>();
12+
13+
static {
14+
distinctList.add(new String("Jack"));
15+
distinctList.add(new String("James"));
16+
distinctList.add(new String("Sam"));
17+
}
18+
19+
private static List<String> notAllEqualList = new ArrayList<>();
20+
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+
28+
private static List<String> emptyList = new ArrayList<>();
29+
30+
private static List<String> allEqualList = new ArrayList<>();
31+
32+
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"));
37+
}
38+
39+
private static VerifyAllEqualListElements verifyAllEqualListElements = new VerifyAllEqualListElements();
40+
41+
@Test
42+
public void verifyAllEqualUsingALoop_whenUsingDistinctList_thenReturnFalse() {
43+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingALoop(distinctList);
44+
45+
assertFalse(allEqual);
46+
}
47+
48+
@Test
49+
public void verifyAllEqualUsingALoop_whenNotAllEqualList_thenReturnFalse() {
50+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingALoop(notAllEqualList);
51+
52+
assertFalse(allEqual);
53+
}
54+
55+
@Test
56+
public void verifyAllEqualUsingALoop_whenEmptyList_thenReturnTrue() {
57+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingALoop(emptyList);
58+
59+
assertTrue(allEqual);
60+
}
61+
62+
@Test
63+
public void verifyAllEqualUsingALoop_whenAllEqualList_thenReturnTrue() {
64+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingALoop(allEqualList);
65+
66+
assertTrue(allEqual);
67+
}
68+
69+
@Test
70+
public void verifyAllEqualUsingStream_whenUsingDistinctList_thenReturnFalse() {
71+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(distinctList);
72+
73+
assertFalse(allEqual);
74+
}
75+
76+
@Test
77+
public void verifyAllEqualUsingStream_whenNotAllEqualList_thenReturnFalse() {
78+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(notAllEqualList);
79+
80+
assertFalse(allEqual);
81+
}
82+
83+
@Test
84+
public void verifyAllEqualUsingStream_whenEmptyList_thenReturnTrue() {
85+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(emptyList);
86+
87+
assertTrue(allEqual);
88+
}
89+
90+
@Test
91+
public void verifyAllEqualUsingStream_whenAllEqualList_thenReturnTrue() {
92+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(allEqualList);
93+
94+
assertTrue(allEqual);
95+
}
96+
97+
@Test
98+
public void verifyAllEqualAnotherUsingStream_whenUsingDistinctList_thenReturnFalse() {
99+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualAnotherUsingStream(distinctList);
100+
101+
assertFalse(allEqual);
102+
}
103+
104+
@Test
105+
public void verifyAllEqualAnotherUsingStream_whenNotAllEqualList_thenReturnFalse() {
106+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualAnotherUsingStream(notAllEqualList);
107+
108+
assertFalse(allEqual);
109+
}
110+
111+
@Test
112+
public void verifyAllEqualAnotherUsingStream_whenEmptyList_thenReturnTrue() {
113+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualAnotherUsingStream(emptyList);
114+
115+
assertTrue(allEqual);
116+
}
117+
118+
@Test
119+
public void verifyAllEqualAnotherUsingStream_whenAllEqualList_thenReturnTrue() {
120+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualAnotherUsingStream(allEqualList);
121+
122+
assertTrue(allEqual);
123+
}
124+
125+
126+
@Test
127+
public void verifyAllEqualUsingGuava_whenUsingDistinctList_thenReturnFalse() {
128+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(distinctList);
129+
130+
assertFalse(allEqual);
131+
}
132+
133+
@Test
134+
public void verifyAllEqualUsingGuava_whenNotAllEqualList_thenReturnFalse() {
135+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(notAllEqualList);
136+
137+
assertFalse(allEqual);
138+
}
139+
140+
@Test
141+
public void verifyAllEqualUsingGuava_whenEmptyList_thenReturnTrue() {
142+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(emptyList);
143+
144+
assertTrue(allEqual);
145+
}
146+
147+
@Test
148+
public void verifyAllEqualUsingGuava_whenAllEqualList_thenReturnTrue() {
149+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(allEqualList);
150+
151+
assertTrue(allEqual);
152+
}
153+
154+
@Test
155+
public void verifyAllEqualUsingApacheCommon_whenUsingDistinctList_thenReturnFalse() {
156+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingApacheCommon(distinctList);
157+
158+
assertFalse(allEqual);
159+
}
160+
161+
@Test
162+
public void verifyAllEqualUsingApacheCommon_whenNotAllEqualList_thenReturnFalse() {
163+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingApacheCommon(notAllEqualList);
164+
165+
assertFalse(allEqual);
166+
}
167+
168+
@Test
169+
public void verifyAllEqualUsingApacheCommon_whenEmptyList_thenReturnTrue() {
170+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingApacheCommon(emptyList);
171+
172+
assertTrue(allEqual);
173+
}
174+
175+
@Test
176+
public void verifyAllEqualUsingApacheCommon_whenAllEqualList_thenReturnTrue() {
177+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingApacheCommon(allEqualList);
178+
179+
assertTrue(allEqual);
180+
}
181+
}

0 commit comments

Comments
 (0)