Skip to content

Commit eeb89df

Browse files
authored
Merge pull request eugenp#2447 from earth001/master
BAEL-1079
2 parents da3273a + 7ec1db1 commit eeb89df

8 files changed

Lines changed: 260 additions & 8 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,11 @@
186186
<artifactId>rome</artifactId>
187187
<version>${rome.version}</version>
188188
</dependency>
189-
<dependency>
190-
<groupId>io.specto</groupId>
191-
<artifactId>hoverfly-java</artifactId>
192-
<version>0.8.0</version>
193-
</dependency>
189+
<dependency>
190+
<groupId>io.specto</groupId>
191+
<artifactId>hoverfly-java</artifactId>
192+
<version>0.8.0</version>
193+
</dependency>
194194
<dependency>
195195
<groupId>org.apache.httpcomponents</groupId>
196196
<artifactId>httpclient</artifactId>
@@ -385,7 +385,7 @@
385385
<dependency>
386386
<groupId>one.util</groupId>
387387
<artifactId>streamex</artifactId>
388-
<version>0.6.5</version>
388+
<version>${streamex.version}</version>
389389
</dependency>
390390
<dependency>
391391
<groupId>org.jooq</groupId>
@@ -472,11 +472,16 @@
472472
<artifactId>noexception</artifactId>
473473
<version>1.1.0</version>
474474
</dependency>
475-
<dependency>
475+
<dependency>
476476
<groupId>org.eclipse.collections</groupId>
477477
<artifactId>eclipse-collections</artifactId>
478478
<version>${eclipse-collections.version}</version>
479479
</dependency>
480+
<dependency>
481+
<groupId>io.vavr</groupId>
482+
<artifactId>vavr</artifactId>
483+
<version>${vavr.version}</version>
484+
</dependency>
480485
</dependencies>
481486
<properties>
482487
<multiverse.version>0.7.0</multiverse.version>
@@ -519,6 +524,8 @@
519524
<bytebuddy.version>1.7.1</bytebuddy.version>
520525
<pcollections.version>2.1.2</pcollections.version>
521526
<rome.version>1.0</rome.version>
522-
<eclipse-collections.version>8.2.0</eclipse-collections.version>
527+
<eclipse-collections.version>8.2.0</eclipse-collections.version>
528+
<streamex.version>0.6.5</streamex.version>
529+
<vavr.version>0.9.0</vavr.version>
523530
</properties>
524531
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.distinct;
2+
3+
import java.util.Map;
4+
import java.util.concurrent.ConcurrentHashMap;
5+
import java.util.function.Function;
6+
import java.util.function.Predicate;
7+
8+
public class DistinctWithJavaFunction {
9+
10+
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
11+
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
12+
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
13+
}
14+
15+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.distinct;
2+
3+
public class Person {
4+
int age;
5+
String name;
6+
String email;
7+
8+
public Person(int age, String name, String email) {
9+
super();
10+
this.age = age;
11+
this.name = name;
12+
this.email = email;
13+
}
14+
15+
public int getAge() {
16+
return age;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public String getEmail() {
24+
return email;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
StringBuilder builder = new StringBuilder();
30+
builder.append("Person [age=");
31+
builder.append(age);
32+
builder.append(", name=");
33+
builder.append(name);
34+
builder.append(", email=");
35+
builder.append(email);
36+
builder.append("]");
37+
return builder.toString();
38+
}
39+
40+
@Override
41+
public int hashCode() {
42+
final int prime = 31;
43+
int result = 1;
44+
result = prime * result + ((email == null) ? 0 : email.hashCode());
45+
return result;
46+
}
47+
48+
@Override
49+
public boolean equals(Object obj) {
50+
if (this == obj)
51+
return true;
52+
if (obj == null)
53+
return false;
54+
if (getClass() != obj.getClass())
55+
return false;
56+
Person other = (Person) obj;
57+
if (email == null) {
58+
if (other.email != null)
59+
return false;
60+
} else if (!email.equals(other.email))
61+
return false;
62+
return true;
63+
}
64+
65+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.distinct;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.util.List;
6+
7+
import org.eclipse.collections.impl.block.factory.HashingStrategies;
8+
import org.eclipse.collections.impl.utility.ListIterate;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
public class DistinctWithEclipseCollectionsUnitTest {
13+
List<Person> personList;
14+
15+
@Before
16+
public void init() {
17+
personList = PersonDataGenerator.getPersonListWithFakeValues();
18+
}
19+
20+
@Test
21+
public void whenFilterListByName_thenSizeShouldBe4() {
22+
List<Person> personListFiltered = ListIterate.distinct(personList, HashingStrategies.fromFunction(Person::getName));
23+
assertTrue(personListFiltered.size() == 4);
24+
}
25+
26+
@Test
27+
public void whenFilterListByAge_thenSizeShouldBe2() {
28+
List<Person> personListFiltered = ListIterate.distinct(personList, HashingStrategies.fromIntFunction(Person::getAge));
29+
assertTrue(personListFiltered.size() == 2);
30+
}
31+
32+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.distinct;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static com.baeldung.distinct.DistinctWithJavaFunction.distinctByKey;
5+
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
public class DistinctWithJavaFunctionUnitTest {
13+
List<Person> personList;
14+
15+
@Before
16+
public void init() {
17+
personList = PersonDataGenerator.getPersonListWithFakeValues();
18+
}
19+
20+
@Test
21+
public void whenFilterListByName_thenSizeShouldBe4() {
22+
List<Person> personListFiltered = personList.stream()
23+
.filter(distinctByKey(p -> p.getName()))
24+
.collect(Collectors.toList());
25+
assertTrue(personListFiltered.size() == 4);
26+
}
27+
28+
@Test
29+
public void whenFilterListByAge_thenSizeShouldBe2() {
30+
List<Person> personListFiltered = personList.stream()
31+
.filter(distinctByKey(p -> p.getAge()))
32+
.collect(Collectors.toList());
33+
assertTrue(personListFiltered.size() == 2);
34+
}
35+
36+
@Test
37+
public void whenFilterListWithDefaultDistinct_thenSizeShouldBe5() {
38+
List<Person> personListFiltered = personList.stream()
39+
.distinct()
40+
.collect(Collectors.toList());
41+
assertTrue(personListFiltered.size() == 5);
42+
}
43+
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.distinct;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.util.List;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import one.util.streamex.StreamEx;
11+
12+
public class DistinctWithStreamexUnitTest {
13+
List<Person> personList;
14+
15+
@Before
16+
public void init() {
17+
personList = PersonDataGenerator.getPersonListWithFakeValues();
18+
}
19+
20+
@Test
21+
public void whenFilterListByName_thenSizeShouldBe4() {
22+
List<Person> personListFiltered = StreamEx.of(personList)
23+
.distinct(Person::getName)
24+
.toList();
25+
assertTrue(personListFiltered.size() == 4);
26+
}
27+
28+
@Test
29+
public void whenFilterListByAge_thenSizeShouldBe2() {
30+
List<Person> personListFiltered = StreamEx.of(personList)
31+
.distinct(Person::getAge)
32+
.toList();
33+
assertTrue(personListFiltered.size() == 2);
34+
}
35+
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.distinct;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.util.List;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
public class DistinctWithVavrUnitTest {
11+
List<Person> personList;
12+
13+
@Before
14+
public void init() {
15+
personList = PersonDataGenerator.getPersonListWithFakeValues();
16+
}
17+
18+
@Test
19+
public void whenFilterListByName_thenSizeShouldBe4() {
20+
List<Person> personListFiltered = io.vavr.collection.List.ofAll(personList)
21+
.distinctBy(Person::getName)
22+
.toJavaList();
23+
assertTrue(personListFiltered.size() == 4);
24+
}
25+
26+
@Test
27+
public void whenFilterListByAge_thenSizeShouldBe2() {
28+
List<Person> personListFiltered = io.vavr.collection.List.ofAll(personList)
29+
.distinctBy(Person::getAge)
30+
.toJavaList();
31+
assertTrue(personListFiltered.size() == 2);
32+
}
33+
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.distinct;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class PersonDataGenerator {
7+
8+
public static List<Person> getPersonListWithFakeValues() {
9+
// @formatter:off
10+
return Arrays.asList(
11+
new Person(20, "Jhon", "jhon@test.com"),
12+
new Person(20, "Jhon", "jhon1@test.com"),
13+
new Person(20, "Jhon", "jhon2@test.com"),
14+
new Person(21, "Tom", "Tom@test.com"),
15+
new Person(21, "Mark", "Mark@test.com"),
16+
new Person(20, "Julia", "jhon@test.com"));
17+
// @formatter:on
18+
}
19+
}

0 commit comments

Comments
 (0)