Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
Java8Practies
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
Uma Vani Ravuri
Java8Practies
Commits
5aab7f96
Commit
5aab7f96
authored
Sep 23, 2021
by
Uma Vani Ravuri
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Replace TestEmployee.java
parent
db7753dd
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
86 additions
and
59 deletions
+86
-59
TestEmployee.java
...8PractiesProgram/src/com/java8/practies/TestEmployee.java
+86
-59
No files found.
Java8PractiesProgram/src/com/java8/practies/TestEmployee.java
View file @
5aab7f96
package
com
.
java8
.
practies
;
package
com
.
java8
.
practies
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Comparator
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.function.Function
;
import
java.util.stream.Collector
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
import
java.util.stream.Stream
;
public
class
TestEmployee
{
public
class
TestEmployee
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
ArrayList
<
Employee
>
list
=
new
ArrayList
<
Employee
>();
list
.
add
(
new
Employee
(
"1"
,
"Uma"
,
"Vani"
,
"IT"
));
list
.
add
(
new
Employee
(
"2"
,
"Sujit"
,
"Krishnan"
,
"CSE"
));
list
.
add
(
new
Employee
(
"3"
,
"Brayan"
,
"Bra"
,
"IT"
));
list
.
add
(
new
Employee
(
"4"
,
"Allice"
,
"Ali"
,
"CSE"
));
list
.
add
(
new
Employee
(
"5"
,
"Sco te"
,
"Scote"
,
"IT"
));
list
.
add
(
new
Employee
(
"6"
,
"Rajesh"
,
"Krishnan"
,
"ECE"
));
list
.
add
(
new
Employee
(
"7"
,
"Ramana"
,
"Raju"
,
"IT"
));
list
.
add
(
new
Employee
(
"8"
,
"Raju"
,
"Krishnan"
,
"EEE"
));
list
.
add
(
new
Employee
(
"9"
,
"Rishi"
,
"Vani"
,
"IT"
));
list
.
add
(
new
Employee
(
"10"
,
"Dattu"
,
"Krishnan"
,
"EEE"
));
//1)list of employees, get the first employee from the list and return his fullName
list
.
stream
().
limit
(
1
)
.
forEach
(
x
->
System
.
out
.
println
(
"First employee Full Name is::"
+
x
.
firstName
+
x
.
lastName
));
//given a list of employees. Can you create a map with the count of employees each department has ?
//with key as department name and count of employees as value.
Map
<
String
,
Long
>
mapDepartment
=
list
.
stream
()
.
collect
(
Collectors
.
groupingBy
(
Employee:
:
getDepartmentNames
,
Collectors
.
counting
()));
mapDepartment
.
forEach
((
k
,
v
)
->
System
.
out
.
println
((
k
+
":"
+
v
)));
//Given a map with the department name as key and value as list of employees belonging to that department.
//when a search string is given, need to list out the employees whose firstname or lastname is matching(match should be case insensitive)
Map
<
String
,
List
<
Employee
>>
mapEmployee
=
list
.
stream
()
.
collect
(
Collectors
.
groupingBy
(
Employee:
:
getDepartmentNames
,
Collectors
.
toList
()));
mapEmployee
.
forEach
((
k
,
v
)
->
System
.
out
.
println
((
k
+
":"
+
v
)));
//Consider a list of employees, and if a department name is given as argument, list out the employees which doesn't belong to that department.
List
<
String
>
depList
=
new
ArrayList
<
String
>();
depList
.
add
(
"IT"
);
List
<
Employee
>
filterEmp
=
list
.
stream
().
filter
(
x
->
!
x
.
getDepartmentNames
().
equals
(
"IT"
))
depList
.
add
(
"CSE"
);
.
collect
(
Collectors
.
toList
());
depList
.
add
(
"ECE"
);
filterEmp
.
forEach
(
x
->
System
.
out
.
println
(
" Employee list whose Department is not IT"
+
x
));
List
<
String
>
depList2
=
new
ArrayList
<
String
>();
depList2
.
add
(
"ECE"
);
depList2
.
add
(
"EEE"
);
depList2
.
add
(
"IT"
);
List
<
Employee
>
listEmp
=
new
ArrayList
<
Employee
>();
listEmp
.
add
(
new
Employee
(
"1"
,
"Uma"
,
"vani"
,
depList
));
listEmp
.
add
(
new
Employee
(
"2"
,
"Sujit"
,
"Krishnan"
,
depList2
));
listEmp
.
add
(
new
Employee
(
"3"
,
"Brayan"
,
"Braya"
,
depList
));
listEmp
.
add
(
new
Employee
(
"4"
,
"Allice"
,
"Ali"
,
depList2
));
listEmp
.
add
(
new
Employee
(
"5"
,
"Sco te"
,
""
,
depList2
));
listEmp
.
add
(
new
Employee
(
"6"
,
"Rajesh"
,
"Krishnan"
,
depList
));
listEmp
.
add
(
new
Employee
(
"7"
,
"Ramana"
,
"Raju"
,
depList2
));
listEmp
.
add
(
new
Employee
(
"8"
,
"Raju"
,
"Krishnan"
,
depList
));
listEmp
.
add
(
new
Employee
(
"9"
,
"Rishi"
,
"Vani"
,
depList
));
listEmp
.
add
(
new
Employee
(
"10"
,
"Dattu"
,
""
,
depList2
));
//list of employees, get the first employee from the list and return his fullName.
// 6)Consider a list of employees, sort the employees by their firstName and
listEmp
.
stream
().
map
(
emp
->
{
// return the sorted list of employees.
Optional
.
ofNullable
(
emp
)
List
<
Employee
>
sortedEmployee
=
list
.
stream
().
sorted
(
new
EmployeeComparator
()).
collect
(
Collectors
.
toList
());
.
ifPresent
(
x
->
System
.
out
.
println
(
"First Employee Details :"
+
x
.
getFirstName
()
+
x
.
getLastName
()));
sortedEmployee
.
forEach
(
x
->
System
.
out
.
println
(
"Sorted Employee based on firstName"
+
x
));
return
emp
;
}).
findFirst
();
//Consider a list of employees, sort the employees by their firstName and
listEmp
.
stream
().
sorted
(
Comparator
.
comparing
(
Employee:
:
getFirstName
)).
collect
(
Collectors
.
toList
()).
forEach
(
System
.
out
::
println
);
//Consider a list of employees, return the employee whose empId is highest
//Consider a list of employees, return the employee whose empId is highest
Employee
e
=
list
.
stream
().
max
((
e1
,
e2
)
->
e1
.
getId
().
compareTo
(
e2
.
getId
())).
get
();
Optional
<
Employee
>
e
=
listEmp
.
stream
().
collect
(
Collectors
.
maxBy
(
Comparator
.
comparing
(
Employee:
:
getId
)));
System
.
out
.
println
(
"Maximum Id Employee details"
+
e
);
if
(
e
.
isPresent
())
{
System
.
out
.
print
(
"Higest Employee Id"
+
e
.
get
());
}
//Consider a list of employees, concat the fullName of all the employees with pipe (|) and return the concatenated string.
//Consider a list of employees, concat the fullName of all the employees with pipe (|) and return the concatenated string.
String
joinedString
=
list
.
stream
().
map
(
x
->
x
.
firstName
+
x
.
lastName
).
collect
(
Collectors
.
joining
(
"|"
));
System
.
out
.
println
(
"Maximum Id Employee details-----------"
+
joinedString
);
String
joinedString
=
listEmp
.
stream
().
map
(
x
->
x
.
firstName
+
x
.
lastName
).
collect
(
Collectors
.
joining
(
"|"
));
System
.
out
.
println
(
"FullNames Of all employees concat with | symbol is :-----------"
+
joinedString
);
//Consider a list of 10 employees, get the 8th employee and print his full name and department name.
//Consider a list of 10 employees, get the 8th employee and print his full name and department name.
list
.
stream
().
sorted
((
e1
,
e2
)
->
e1
.
getId
().
compareTo
(
e2
.
getId
())).
skip
(
8
).
limit
(
1
)
listEmp
.
stream
().
sorted
(
Comparator
.
comparing
(
Employee:
:
getId
)).
skip
(
8
).
limit
(
1
).
forEach
(
System
.
out
::
println
);
.
forEach
(
System
.
out
::
println
);
//consider list of employees and list of employee ids. return the list of employees matching with the given list of employee ids.
//consider list of employees and list of employee ids. return the list of employees matching with the given list of employee ids.
List
<
Employee
>
checkForMatches
=
new
ArrayList
<>();
List
<
Employee
>
checkForMatches
=
new
ArrayList
<>();
checkForMatches
.
add
(
new
Employee
(
"2"
,
"siri"
,
"raja"
,
"CSE"
));
checkForMatches
.
add
(
new
Employee
(
"2"
,
"siri"
,
"raja"
,
depList
));
checkForMatches
.
add
(
new
Employee
(
"7"
,
"roja"
,
"rani"
,
"CSE"
));
checkForMatches
.
add
(
new
Employee
(
"7"
,
"roja"
,
"rani"
,
depList2
));
checkForMatches
.
add
(
new
Employee
(
"11"
,
"roja"
,
"rani"
,
"CSE"
));
checkForMatches
.
add
(
new
Employee
(
"11"
,
"roja"
,
"rani"
,
depList
));
List
<
Employee
>
filteredList
=
list
.
stream
()
listEmp
.
stream
().
filter
(
employee
->
checkForMatches
.
stream
().
anyMatch
(
dept
->
employee
.
getId
().
equals
(
dept
.
getId
())))
.
filter
(
employee
->
checkForMatches
.
stream
().
anyMatch
(
dept
->
employee
.
getId
().
equals
(
dept
.
getId
())))
.
collect
(
Collectors
.
toList
()).
forEach
(
System
.
out
::
println
);
.
collect
(
Collectors
.
toList
());
filteredList
.
forEach
(
x
->
System
.
out
.
println
(
"Employee matches"
+
x
));
//Store ids are of 4 digit strings.if length of given storeId is less than 4 digits,you need to prefix with zeros and return a 4 digit storeId.
//Store ids are of 4 digit strings.if length of given storeId is less than 4 digits,you need to prefix with zeros and return a 4 digit storeId.
list
.
stream
().
forEach
(
x
->
System
.
out
.
println
(
"4 Digits employee Id::"
+
String
.
format
(
"%04d"
,
Integer
.
parseInt
(
x
.
id
))));
listEmp
.
stream
()
.
map
(
employee
->
{
Optional
.
ofNullable
(
employee
)
.
map
(
Employee:
:
getId
)
.
map
(
id
->
String
.
format
(
"%04d"
,
Integer
.
parseInt
(
id
)))
.
ifPresent
(
a
->
employee
.
setId
(
a
));
return
employee
.
getId
();
}).
collect
(
Collectors
.
toList
()).
forEach
(
System
.
out
::
println
);
//Consider a list of employees, and if a department name is given as argument, list out the employees which doesn't belong to that department.
listEmp
.
stream
().
filter
(
x
->
!
x
.
getDepartmentNames
().
stream
().
equals
(
"EEE"
))
.
collect
(
Collectors
.
toList
()).
forEach
(
System
.
out
::
println
);
listEmp
.
stream
()
.
flatMap
(
e
->
e
.
getDepartmentNames
().
stream
())
.
filter
(
d
->
!
"EEE"
.
equals
(
d
.
getDepartmentNames
()))
.
collect
(
Collectors
.
toList
()).
forEach
(
System
.
out
::
println
);
//given a list of employees. Can you create a map with the count of employees each department has ?
//with key as department name and count of employees as value.
Stream
<
String
>
distinctDept
=
listEmp
.
stream
()
.
map
(
Employee:
:
getDepartmentNames
)
.
flatMap
(
x
->
x
.
stream
());
Map
<
String
,
Long
>
mapDepartment
=
distinctDept
.
collect
(
Collectors
.
groupingBy
(
Function
.
identity
(),
Collectors
.
counting
()));
mapDepartment
.
forEach
((
k
,
v
)
->
System
.
out
.
println
((
k
+
":"
+
v
)));
}
}
}
}
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