Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
Java8StreamAPI
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
Ashwini P
Java8StreamAPI
Commits
8c881a7d
Commit
8c881a7d
authored
Dec 03, 2024
by
Ashwini P
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Java8 Stream Changes
parents
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
237 additions
and
0 deletions
+237
-0
.gitignore
.gitignore
+39
-0
pom.xml
pom.xml
+18
-0
Employee.java
src/main/java/org/example/Employee.java
+83
-0
Main.java
src/main/java/org/example/Main.java
+97
-0
No files found.
.gitignore
0 → 100644
View file @
8c881a7d
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
pom.xml
0 → 100644
View file @
8c881a7d
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
org.example
</groupId>
<artifactId>
Java8Demo
</artifactId>
<version>
1.0-SNAPSHOT
</version>
<properties>
<maven.compiler.source>
8
</maven.compiler.source>
<maven.compiler.target>
8
</maven.compiler.target>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
src/main/java/org/example/Employee.java
0 → 100644
View file @
8c881a7d
package
org
.
example
;
import
java.util.List
;
public
class
Employee
{
private
int
id
;
private
String
name
;
private
String
department
;
private
int
age
;
private
double
salary
;
public
List
<
String
>
skills
;
public
Employee
(
int
id
,
String
name
,
String
department
,
int
age
,
double
salary
,
List
<
String
>
skills
)
{
super
();
this
.
id
=
id
;
this
.
name
=
name
;
this
.
department
=
department
;
this
.
age
=
age
;
this
.
salary
=
salary
;
this
.
skills
=
skills
;
}
@Override
public
String
toString
()
{
return
"Employee{"
+
"id="
+
id
+
", name='"
+
name
+
'\''
+
", department='"
+
department
+
'\''
+
", age="
+
age
+
", salary="
+
salary
+
'}'
;
}
public
int
getId
()
{
return
id
;
}
public
void
setId
(
int
id
)
{
this
.
id
=
id
;
}
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
String
getDepartment
()
{
return
department
;
}
public
void
setDepartment
(
String
department
)
{
this
.
department
=
department
;
}
public
int
getAge
()
{
return
age
;
}
public
void
setAge
(
int
age
)
{
this
.
age
=
age
;
}
public
double
getSalary
()
{
return
salary
;
}
public
void
setSalary
(
double
salary
)
{
this
.
salary
=
salary
;
}
public
List
<
String
>
getSkills
()
{
return
skills
;
}
public
void
setSkills
(
List
<
String
>
skills
)
{
this
.
skills
=
skills
;
}
}
src/main/java/org/example/Main.java
0 → 100644
View file @
8c881a7d
package
org
.
example
;
import
java.util.*
;
import
java.util.stream.Collectors
;
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Hello world!"
);
List
<
Employee
>
employees
=
new
ArrayList
<
Employee
>();
employees
.
add
(
new
Employee
(
101
,
"Ashwini"
,
"IT"
,
25
,
20000.0
,
Arrays
.
asList
(
"java"
,
"python"
)));
employees
.
add
(
new
Employee
(
102
,
"Amulu"
,
"CSE"
,
30
,
200000.0
,
Arrays
.
asList
(
".net"
,
"python"
)));
employees
.
add
(
new
Employee
(
101
,
"Anu"
,
"IT"
,
35
,
35000.0
,
Arrays
.
asList
(
"J2EE"
,
"C++"
,
"Kafka"
)));
employees
.
add
(
new
Employee
(
101
,
"Kamala"
,
"IT"
,
28
,
25000.0
,
Arrays
.
asList
(
"microservices"
,
"python"
)));
employees
.
add
(
new
Employee
(
101
,
"Anji"
,
"CSE"
,
32
,
40000.0
,
Arrays
.
asList
(
"java"
,
"spring"
)));
/*
* 1.Filtering and Collecting: Write a Streams API expression to filter out
* employees with a salary greater than 50,000 from empList and collect them
* into a new list.
*/
List
<
Employee
>
empList
=
employees
.
stream
().
filter
(
e
->
e
.
getSalary
()>
50000
).
collect
(
Collectors
.
toList
());
System
.
out
.
println
(
empList
);
/*
* 2.Mapping: Transform empList to a list of employee names using the Streams API.
*/
List
<
String
>
collect
=
employees
.
stream
().
map
(
e
->
e
.
getName
()).
collect
(
Collectors
.
toList
());
System
.
out
.
println
(
collect
);
/*
* 3.Sorting: Sort empList by employee age in ascending order using the Streams
* API.
*/
List
<
String
>
empAge
=
employees
.
stream
().
sorted
(
Comparator
.
comparingInt
(
Employee:
:
getAge
)).
map
(
Employee:
:
getName
).
collect
(
Collectors
.
toList
());
System
.
out
.
println
(
"Employee age in Ascending order:"
+
empAge
);
/*
*4. Aggregation (Reduction): Use the Streams API to find the total salary of all
* employees in empList.
*/
DoubleSummaryStatistics
collect2
=
employees
.
stream
().
collect
(
Collectors
.
summarizingDouble
(
Employee:
:
getSalary
));
System
.
out
.
println
(
"Total salary of the employees:"
+
collect2
.
getSum
());
/*
*5. Finding Maximum/Minimum: Write a Streams API expression to find the employee
* with the highest salary in empList. Group By:
*/
Optional
<
Employee
>
max
=
employees
.
stream
().
max
(
Comparator
.
comparingDouble
(
Employee:
:
getSalary
));
System
.
out
.
println
(
"Highest salary Employee name: "
+
max
.
get
().
getName
());
/*
* 6.Group By: Group employees by department using the Streams API.
*/
Map
<
String
,
List
<
Employee
>>
collect3
=
employees
.
stream
().
collect
(
Collectors
.
groupingBy
(
Employee:
:
getDepartment
));
collect3
.
forEach
((
dept1
,
empList1
)->
{
System
.
out
.
println
(
"department:"
+
dept1
);
System
.
out
.
println
(
empList1
);
});
/*
* Custom Collector: Create a custom collector to partition employees into two
* groups based on their salary (greater or less than 50,000) using the Streams
* API.
*
*/
Map
<
Boolean
,
List
<
Employee
>>
collect4
=
employees
.
stream
().
collect
(
Collectors
.
partitioningBy
(
e
->
e
.
getSalary
()>
50000
));
List
<
Employee
>
salaryBelow10000Employees
=
collect4
.
get
(
false
);
System
.
out
.
println
(
"Employees list salary less than 50000 "
+
salaryBelow10000Employees
);
List
<
Employee
>
salaryGreaterthan10000Employees
=
collect4
.
get
(
true
);
System
.
out
.
println
(
"employees list salary is greater than 50000 "
+
salaryGreaterthan10000Employees
);
/*
* Parallel Streams: Convert empList into a parallel stream and demonstrate
* calculating the count of employees.
*/
long
count
=
employees
.
parallelStream
().
count
();
System
.
out
.
println
(
"count of employees from the list"
+
count
);
/* FlatMap:
Assuming empList contains a list of employees, each having a list of skills, write a Streams API expression to flatten all skills into a single list.
*/
List
<
String
>
allSkills
=
employees
.
stream
().
flatMap
(
employee
->
employee
.
skills
.
stream
()).
collect
(
Collectors
.
toList
());
System
.
out
.
println
(
"flatten Skills:"
+
allSkills
);
}
}
\ No newline at end of file
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