Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
Java 8 Features
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Syed Javed Ali
Java 8 Features
Commits
bf143ed1
Commit
bf143ed1
authored
Feb 10, 2021
by
Syed Javed Ali
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added code for sorting
parent
1d0e68fa
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
20 deletions
+32
-20
ClientApp.java
src/main/java/com/nisum/example/client/ClientApp.java
+31
-19
Person.java
src/main/java/com/nisum/example/model/Person.java
+1
-1
No files found.
src/main/java/com/nisum/example/client/ClientApp.java
View file @
bf143ed1
...
...
@@ -10,37 +10,49 @@ import java.util.stream.Collectors;
*/
public
class
ClientApp
{
public
static
void
main
(
String
[]
args
)
{
Person
p1
=
new
Person
(
1201
,
"HARI"
,
8700
,
"HYD"
,
"DEV"
,
"40582678416578"
,
Person
p1
=
new
Person
(
1201
,
"HARI"
,
8700
,
"HYD"
,
"DEV"
,
20200725122530L
,
Arrays
.
asList
(
"7722448815"
,
"9966778844"
,
"9944668877"
));
Person
p2
=
new
Person
(
1202
,
"SHAM"
,
5500
,
"PUNE"
,
"QA"
,
"69382678416578"
,
Arrays
.
asList
(
"8855447788"
,
"7547871478"
,
"0147248784"
));
20201025013020L
,
Arrays
.
asList
(
"8855447788"
,
"7547871478"
,
"0147248784"
));
Person
p3
=
new
Person
(
1203
,
"SAI"
,
7500
,
"BANGLORE"
,
"DEV"
,
"14782678413689"
,
Arrays
.
asList
(
"7755447788"
,
"6767714785"
,
"01452487845"
));
20200825032515L
,
Arrays
.
asList
(
"7755447788"
,
"6767714785"
,
"01452487845"
));
Person
p4
=
new
Person
(
1204
,
"KIRAN"
,
9500
,
"CHENNAI"
,
"DEV"
,
"89782678413689"
,
Arrays
.
asList
(
"8855447788"
,
"7667714785"
,
"01252487845"
));
20201125052010L
,
Arrays
.
asList
(
"8855447788"
,
"7667714785"
,
"01252487845"
));
Person
p5
=
new
Person
(
1203
,
"KHAN"
,
4500
,
"CHENNAI"
,
"PROD"
,
"60582678413689"
,
Arrays
.
asList
(
"9955447788"
,
"1467714785"
,
"02352487845"
));
20200625072045L
,
Arrays
.
asList
(
"9955447788"
,
"1467714785"
,
"02352487845"
));
List
<
Person
>
person
s
=
Arrays
.
asList
(
p1
,
p2
,
p3
,
p4
,
p5
);
List
<
Person
>
person
List
=
Arrays
.
asList
(
p1
,
p2
,
p3
,
p4
,
p5
);
// find the person id with highest salary in a given department
Optional
<
Person
>
optionalPerson
=
persons
.
stream
().
filter
(
p
->
p
.
getDepartment
().
equals
(
"DEV"
))
.
collect
(
Collectors
.
maxBy
(
Comparator
.
comparingInt
(
f
->
f
.
getSalary
())));
System
.
out
.
println
(
optionalPerson
.
isPresent
()?
"Person ID with highest salary :"
+
optionalPerson
.
get
().
getId
():
"Record with highest salary not exist"
);
// print the no of employees at each location.
Map
<
String
,
Long
>
locationCount
=
persons
.
stream
().
collect
(
Collectors
.
groupingBy
(
Person:
:
getLocation
,
Collectors
.
counting
()));
for
(
String
s:
locationCount
.
keySet
()){
System
.
out
.
println
(
s
+
"-"
+
locationCount
.
get
(
s
));
}
Optional
<
Person
>
highestPaidPersonWrapper
=
personList
.
stream
()
.
filter
(
p
->
p
.
getDepartment
().
equals
(
"DEV"
)
).
collect
(
Collectors
.
maxBy
(
Comparator
.
comparingInt
(
f
->
f
.
getSalary
())));
System
.
out
.
println
(
"Highest Salary Person ID: "
+
highestPaidPersonWrapper
.
get
().
getId
());
//print the no of employees at each location.
Map
<
String
,
Long
>
personCountByLocation
=
personList
.
stream
()
.
collect
(
Collectors
.
groupingBy
(
Person:
:
getLocation
,
Collectors
.
counting
()));
Set
<
Map
.
Entry
<
String
,
Long
>>
entrySet
=
personCountByLocation
.
entrySet
();
for
(
Map
.
Entry
<
String
,
Long
>
entry:
entrySet
){
System
.
out
.
println
(
entry
.
getKey
()+
" "
+
entry
.
getValue
());
}
//verify the two persons provided the same phone number
//sort the person list based on timestamp.
//sort descending
List
<
Person
>
sortBasedOnTimestamp
=
personList
.
stream
()
.
sorted
(
Comparator
.
comparing
(
Person:
:
getTimestamp
).
reversed
()).
collect
(
Collectors
.
toList
());
sortBasedOnTimestamp
.
forEach
(
System
.
out
::
println
);
}
}
src/main/java/com/nisum/example/model/Person.java
View file @
bf143ed1
...
...
@@ -19,6 +19,6 @@ public class Person {
private
int
salary
;
private
String
location
;
private
String
department
;
private
Stri
ng
timestamp
;
private
lo
ng
timestamp
;
private
List
<
String
>
phones
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment