Previously we have discussed about
Here we learn about Aggregate Operations In Java, but we must also know What is Collections Framework In Java ? and What is Collection Interface In Java ?
Aggregate Operations
From JDK 8 and later, the preferred method of iterating over a collection is to obtain a stream and perform aggregate operations on it. Aggregate operations are often used with lambda expressions to make programming more expressive, using less lines of code.
The Aggregate Operations are listed below:
Aggregate Operation
|
Description
|
Stream<E> stream()
|
Obtain a source of objects
|
Stream<T> filter(Predicate<? super T> predicate)
|
Filter objects that match a
Predicate object |
<R> Stream<R> map(Function<? super T,? extends R> mapper)
|
Map objects to another value as specified by a
Function object |
void forEach(Consumer<? super T> action)
|
Perform an action as specified by a
Consumer object |
Pipelines and Streams
A pipeline is a sequence of aggregate operations.The following sequentially iterates through a collection:
list.stream()
.filter(e -> e.getGender() == Person.Sex.MALE)
.forEach(e -> System.out.println(e.getName()));
Likewise, the parallel stream is used if the collection is large enough and your computer has enough cores:
list.parallelStream()
.filter(e -> e.getGender() == Person.Sex.FEMALE)
.forEach(e -> System.out.println(e.getName()));
The Collections framework has always provided a "bulk operations" as part of its API. These include methods that operate on entire collections, such as containsAll, addAll, removeAll, etc.Do not confuse those methods with the aggregate operations that were introduced in JDK 8. The key difference between the new aggregate operations and the existing bulk operations (containsAll, addAll, etc.) is that the old versions are all mutative, meaning that they all modify the underlying collection. In contrast, the new aggregate operations do not modify the underlying collection.
Aggregate Operations example :
import java.util.ArrayList;
import java.util.stream.Collectors;
class Person {
public enum Sex {
MALE, FEMALE
}
String name;
Sex gender;
int salary;
String emailAddress;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Sex getGender() {
return gender;
}
public void setGender(Sex gender) {
this.gender = gender;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
public class AggregateOperationsExample {
public static void main(String[] args) {
ArrayList<Person> list = new ArrayList<Person>();
Person p = new Person();
p.setName("John");
p.setSalary(1000);
p.setGender(Person.Sex.MALE);
list.add(p);
Person p1 = new Person();
p1.setName("Saara");
p1.setSalary(500);
p1.setGender(Person.Sex.FEMALE);
list.add(p1);
Person p2 = new Person();
p2.setName("Peter");
p2.setSalary(800);
p2.setGender(Person.Sex.MALE);
list.add(p2);
System.out.println("Accessing Collection Using for-each :");
for(Person per : list){
System.out.println(per.getName());
}
System.out.println("\nAccessing all elements using bulk data operations :");
list.stream()
.forEach(e ->System.out.println(e.getName()+"\t"+e.getGender()+"\t"+e.getSalary()));
System.out.println("\nAccessing Collection using stram() with filter() (Only Male Members) :");
list.stream()
.filter(e -> e.getGender() == Person.Sex.MALE)
.forEach(e -> System.out.println(e.getName()));
System.out.println("\nAccessing Collection using parallelStram() with filter() (Only Female Members):");
list.parallelStream()
.filter(e -> e.getGender() == Person.Sex.FEMALE)
.forEach(e -> System.out.println(e.getName()));
System.out.println("\nSum of Salaries of All Employees :");
int total = list.stream()
.collect(Collectors.summingInt(e-> e.getSalary()));
System.out.println(total);
System.out.println("\nAverage Salary of Employees : ");
double ave = list.stream()
.filter(pe -> pe.getGender() == Person.Sex.MALE)
.mapToInt(e -> e.getSalary())
.average()
.getAsDouble();
System.out.println(ave);
}
}
Program Output:
Accessing Collection Using for-each :
John
Saara
Peter
Accessing all elements using bulk data operations :
John MALE 1000
Saara FEMALE 500
Peter MALE 800
Accessing Collection using stram() with filter() (Only Male Members) :
John
Peter
Accessing Collection using parallelStram() with filter() (Only Female Members):
Saara
Sum of Salaries of All Employees :
2300
Average Salary of Employees :
900.0
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Java I/O Tutorial

No comments:
Post a Comment