Skip to content

Commit 6bdf463

Browse files
authored
Merge pull request eugenp#6263 from cscib/master
BAEL-2534 - Determine if all elements are the same in a Java list
2 parents 69e29b3 + 6faaedd commit 6bdf463

2 files changed

Lines changed: 227 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.allequalelements;
2+
3+
import java.util.Collections;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import org.apache.commons.collections4.IterableUtils;
7+
import com.google.common.base.Predicate;
8+
import com.google.common.collect.Iterables;
9+
10+
public class VerifyAllEqualListElements {
11+
12+
public boolean verifyAllEqualUsingALoop(List<String> list) {
13+
for (String s : list) {
14+
if (!s.equals(list.get(0)))
15+
return false;
16+
}
17+
return true;
18+
}
19+
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+
28+
public boolean verifyAllEqualUsingStream(List<String> list) {
29+
return list.stream()
30+
.distinct()
31+
.count() <= 1;
32+
}
33+
34+
public boolean verifyAllEqualAnotherUsingStream(List<String> list) {
35+
return list.isEmpty() || list.stream()
36+
.allMatch(list.get(0)::equals);
37+
}
38+
39+
public boolean verifyAllEqualUsingGuava(List<String> list) {
40+
return Iterables.all(list, new Predicate<String>() {
41+
public boolean apply(String s) {
42+
return s.equals(list.get(0));
43+
}
44+
});
45+
}
46+
47+
public boolean verifyAllEqualUsingApacheCommon(List<String> list) {
48+
return IterableUtils.matchesAll(list, new org.apache.commons.collections4.Predicate<String>() {
49+
public boolean evaluate(String s) {
50+
return s.equals(list.get(0));
51+
}
52+
});
53+
}
54+
55+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package com.baeldung.allequalelements;
2+
3+
import org.junit.Test;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
10+
public class VerifyAllEqualListElementsUnitTest {
11+
12+
private static List<String> notAllEqualList = new ArrayList<>();
13+
14+
private static List<String> emptyList = new ArrayList<>();
15+
16+
private static List<String> allEqualList = new ArrayList<>();
17+
18+
static {
19+
notAllEqualList = Arrays.asList("Jack", "James", "Sam", "James");
20+
emptyList = Arrays.asList();
21+
allEqualList = Arrays.asList("Jack", "Jack", "Jack", "Jack");
22+
}
23+
24+
private static VerifyAllEqualListElements verifyAllEqualListElements = new VerifyAllEqualListElements();
25+
26+
@Test
27+
public void givenNotAllEqualList_whenUsingALoop_thenReturnFalse() {
28+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingALoop(notAllEqualList);
29+
30+
assertFalse(allEqual);
31+
}
32+
33+
@Test
34+
public void givenEmptyList_whenUsingALoop_thenReturnTrue() {
35+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingALoop(emptyList);
36+
37+
assertTrue(allEqual);
38+
}
39+
40+
@Test
41+
public void givenAllEqualList_whenUsingALoop_thenReturnTrue() {
42+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingALoop(allEqualList);
43+
44+
assertTrue(allEqual);
45+
}
46+
47+
@Test
48+
public void givenNotAllEqualList_whenUsingHashSet_thenReturnFalse() {
49+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingHashSet(notAllEqualList);
50+
51+
assertFalse(allEqual);
52+
}
53+
54+
@Test
55+
public void givenEmptyList_whenUsingHashSet_thenReturnTrue() {
56+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingHashSet(emptyList);
57+
58+
assertTrue(allEqual);
59+
}
60+
61+
@Test
62+
public void givenAllEqualList_whenUsingHashSet_thenReturnTrue() {
63+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingHashSet(allEqualList);
64+
65+
assertTrue(allEqual);
66+
}
67+
68+
@Test
69+
public void givenNotAllEqualList_whenUsingFrequency_thenReturnFalse() {
70+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingFrequency(notAllEqualList);
71+
72+
assertFalse(allEqual);
73+
}
74+
75+
@Test
76+
public void givenEmptyList_whenUsingFrequency_thenReturnTrue() {
77+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingFrequency(emptyList);
78+
79+
assertTrue(allEqual);
80+
}
81+
82+
@Test
83+
public void givenAllEqualList_whenUsingFrequency_thenReturnTrue() {
84+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingFrequency(allEqualList);
85+
86+
assertTrue(allEqual);
87+
}
88+
89+
@Test
90+
public void givenNotAllEqualList_whenUsingStream_thenReturnFalse() {
91+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(notAllEqualList);
92+
93+
assertFalse(allEqual);
94+
}
95+
96+
@Test
97+
public void givenEmptyList_whenUsingStream_thenReturnTrue() {
98+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(emptyList);
99+
100+
assertTrue(allEqual);
101+
}
102+
103+
@Test
104+
public void givenAllEqualList_whenUsingStream_thenReturnTrue() {
105+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingStream(allEqualList);
106+
107+
assertTrue(allEqual);
108+
}
109+
110+
@Test
111+
public void givenNotAllEqualList_whenUsingAnotherStream_thenReturnFalse() {
112+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualAnotherUsingStream(notAllEqualList);
113+
114+
assertFalse(allEqual);
115+
}
116+
117+
@Test
118+
public void givenEmptyList_whenUsingAnotherStream_thenReturnTrue() {
119+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualAnotherUsingStream(emptyList);
120+
121+
assertTrue(allEqual);
122+
}
123+
124+
@Test
125+
public void givenAllEqualList_whenUsingAnotherStream_thenReturnTrue() {
126+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualAnotherUsingStream(allEqualList);
127+
128+
assertTrue(allEqual);
129+
}
130+
131+
@Test
132+
public void givenNotAllEqualList_whenUsingGuava_thenReturnFalse() {
133+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(notAllEqualList);
134+
135+
assertFalse(allEqual);
136+
}
137+
138+
@Test
139+
public void givenEmptyList_whenUsingGuava_thenReturnTrue() {
140+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(emptyList);
141+
142+
assertTrue(allEqual);
143+
}
144+
145+
@Test
146+
public void givenAllEqualList_whenUsingGuava_thenReturnTrue() {
147+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingGuava(allEqualList);
148+
149+
assertTrue(allEqual);
150+
}
151+
152+
@Test
153+
public void givenNotAllEqualList_whenUsingApacheCommon_thenReturnFalse() {
154+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingApacheCommon(notAllEqualList);
155+
156+
assertFalse(allEqual);
157+
}
158+
159+
@Test
160+
public void givenEmptyList_whenUsingApacheCommon_thenReturnTrue() {
161+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingApacheCommon(emptyList);
162+
163+
assertTrue(allEqual);
164+
}
165+
166+
@Test
167+
public void givenAllEqualList_whenUsingApacheCommon_thenReturnTrue() {
168+
boolean allEqual = verifyAllEqualListElements.verifyAllEqualUsingApacheCommon(allEqualList);
169+
170+
assertTrue(allEqual);
171+
}
172+
}

0 commit comments

Comments
 (0)