FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
StreamsAPI-Tutorials/StreamExamples/DistinctByKeys.java at main · CodeJamm/StreamsAPI-Tutorials · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
CodeJamm
/
StreamsAPI-Tutorials
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
5
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
StreamsAPI-Tutorials
/
StreamExamples
/
DistinctByKeys.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
34 lines (31 loc) · 1.11 KB
Breadcrumbs
StreamsAPI-Tutorials
/
StreamExamples
/
DistinctByKeys.java
Copy path
File metadata and controls
34 lines (31 loc) · 1.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
29
30
31
32
33
34
package
StreamExamples
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
List
;
import
java
.
util
.
Map
;
import
java
.
util
.
concurrent
.
ConcurrentHashMap
;
import
java
.
util
.
function
.
Function
;
import
java
.
util
.
function
.
Predicate
;
import
java
.
util
.
stream
.
Collectors
;
import
java
.
util
.
stream
.
Stream
;
import
StudentClass
.
Student
;
public
class
DistinctByKeys
{
public
static
void
main
(
String
[]
args
)
{
Stream
.
of
(
new
Student
(
1
,
"Ram"
),
new
Student
(
2
,
"Arjun"
),
new
Student
(
3
,
"Kavi"
),
new
Student
(
4
,
"Sanju"
),
new
Student
(
5
,
"Ajay"
))
.
filter
(
distinctByKeys
(
Student
::
getID
,
Student
::
getName
))
.
collect
(
Collectors
.
toList
()).
forEach
(
System
.
out
::
println
);
System
.
out
.
println
();
}
private
static
<
T
>
Predicate
<
T
>
distinctByKeys
(
Function
<?
super
T
, ?>...
keyExtractors
)
{
final
Map
<
List
<?>,
Boolean
>
seen
=
new
ConcurrentHashMap
<>();
return
t
->
{
final
List
<?>
keys
=
Arrays
.
stream
(
keyExtractors
)
.
map
(
ke
->
ke
.
apply
(
t
))
.
collect
(
Collectors
.
toList
());
return
seen
.
putIfAbsent
(
keys
,
Boolean
.
TRUE
) ==
null
;
};
}
}
Back
|
FazBrowse Home
|
New Git URL