-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMergingCustomObjects.java
More file actions
28 lines (22 loc) · 1.02 KB
/
MergingCustomObjects.java
File metadata and controls
28 lines (22 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package StreamExamples;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
import StudentClass.Student;
public class MergingCustomObjects
{
public static void main(String[] args)
{
Stream<Student> stream1 = Stream.of(new Student(1,"Ram"),new Student(2,"Arjun"),new Student(3,"Kavi"),new Student(4,"Sanju"),new Student(5,"Ajay"));
Stream<Student> stream2 = Stream.of(new Student(6,"Raguram"),new Student(7,"Arun"),new Student(8,"KavinKumar"),new Student(9,"Sam"),new Student(10,"Akash"));
Stream.concat(stream1, stream2)
.filter(distinctByKey(Student::getName)).forEach(e->System.out.print(e+" "));
}
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor)
{
Map<Object, Boolean> map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
}