In Java 8 new package added java.util.stream. This package consists of classes, interfaces and enum to allows functional-style operations on the elements and stream process the elements from source in sequence, we can say streams are wrappers for collections, arrays and Input/Output. So, by using streams we can easily accessing the elements from collections and apply all operations on it. All Operations performed on a stream does not modify the source for it. It supports aggregate operations like filter, map, limit, reduce, find, match, and so on. Remember that, stream is not a data structure that stores elements.
Here are the methods to create a Stream
Create Stream Using Collections and use filter operation
package com.javatutsworld.stream;
import java.util.ArrayList;
import java.util.List;
public class Java8StreamFilter {
public static void main(String a[]) {
List<AccountDetails> acttList = new ArrayList<>();
acttList.add(new AccountDetails("Saaim", "00266767", 2000));
acttList.add(new AccountDetails("Sudhir", "00266788", 30000));
acttList.add(new AccountDetails("Gaurav", "00266798", 90000));
acttList.add(new AccountDetails("Ram", "00266799", 50000));
// find the name whose account balance matched the condition
acttList.stream().filter(acct -> acct.getAmount() > 3000)
.forEach(acct -> System.out.println("Name: " + acct.getName()));
}
}
Here is domain class
package com.javatutsworld.stream;
public class AccountDetails {
private String name;
private String accountNumber;
private Integer amount;
public AccountDetails(String name, String accountNumber, Integer amount) {
super();
this.name = name;
this.accountNumber = accountNumber;
this.amount = amount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
}
Output:
Name: Sudhir
Name: Gaurav
Name: Ram
Using the Stream Method and use filter operation
package com.javatutsworld.stream;
import java.util.stream.Stream;
public class Java8StreamFilter {
public static void main(String a[]) {
Stream<AccountDetails> stream = Stream.of(
new AccountDetails("Saaim", "00266767", 2000),
new AccountDetails("Sudhir", "00266788", 30000),
new AccountDetails("Gaurav", "00266798", 90000),
new AccountDetails("Ram", "00266799", 50000)).filter(
acct -> acct.getAmount() > 3000);
stream.forEach(acct -> System.out.println("Name: " + acct.getName()));
}
}
Output:
Name: Sudhir
Name: Gaurav
Name: Ram
Using Stream.builder(): and use filter operation
package com.javatutsworld.stream;
import java.util.stream.Stream;
public class Java8StreamFilter {
public static void main(String a[]) {
Stream.Builder<AccountDetails> acctStreamBuilder = Stream.builder();
acctStreamBuilder.accept(new AccountDetails("Saaim", "00266767", 2000));
acctStreamBuilder.accept(new AccountDetails("Sudhir", "00266788", 30000));
acctStreamBuilder.accept(new AccountDetails("Gaurav", "00266798", 90000));
acctStreamBuilder.accept(new AccountDetails("Ram", "00266799", 50000));
Stream<AccountDetails> acctDetailsStream = acctStreamBuilder.build();
// find the name whose account balance matched the condition
acctDetailsStream.filter(acct -> acct.getAmount() > 3000).forEach(
acct -> System.out.println("Name: " + acct.getName()));
}
}
Output:
Name: Sudhir
Name: Gaurav
Name: Ram
Example of forEach operation
forEach is used to iterate each element of the stream.
package com.javatutsworld.stream;
import java.util.ArrayList;
import java.util.List;
public class Java8Stream {
public static void main(String a[]) {
List<AccountDetails> acttList = new ArrayList<>();
acttList.add(new AccountDetails("Saaim", "00266767", 2000));
acttList.add(new AccountDetails("Sudhir", "00266788", 30000));
acttList.add(new AccountDetails("Gaurav", "00266798", 90000));
acttList.add(new AccountDetails("Ram", "00266799", 50000));
acttList.stream().forEach(acct -> System.out.println("Name: " + acct.getName()));
}
}
Output:
Name: Saaim
Name: Sudhir
Name: Gaurav
Name: Ram
Example of map operation
The map method is used to maps each elements to its corresponding result.
Here is code which use the map method to generate the list of names and cube numbers of corresponding elements:
package com.javatutsworld.stream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Java8Stream {
public static void main(String a[]) {
Stream<AccountDetails> stream = Stream.of(
new AccountDetails("Saaim", "00266767", 2000),
new AccountDetails("Sudhir", "00266788", 30000),
new AccountDetails("Gaurav", "00266798", 90000),
new AccountDetails("Ram", "00266799", 50000));
List<String> sName= (List) stream.map(acct->acct.getName()).collect(Collectors.toList());
System.out.println(sName);
List<Integer> numbers = Arrays.asList(3, 2, 7, 5);
//get list of cube
List<Integer> squaresList = numbers.stream().map( i -> i*i*i).collect(Collectors.toList());
System.out.println(squaresList);
}
}
Output:
[Saaim, Sudhir, Gaurav, Ram]
[27, 8, 343, 125]
Example of limit operation
limit method is used to reduce the size of the stream.
Here is the code which shows the limit operation with stream.
package com.javatutsworld.stream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Java8Stream {
public static void main(String a[]) {
Stream<AccountDetails> stream = Stream.of(
new AccountDetails("Saaim", "00266767", 2000),
new AccountDetails("Sudhir", "00266788", 30000),
new AccountDetails("Gaurav", "00266798", 90000),
new AccountDetails("Ram", "00266799", 50000));
List<AccountDetails> listAccountDetails=stream.limit(3).collect(Collectors.toList());
for(AccountDetails acctName: listAccountDetails){
System.out.println(acctName.getName());
}
}
}
Output:
Saaim
Sudhir
Gaurav
Example of sorted operation
Sorted method is used to sort the stream.
Here is the code which shows how to sort amount in ascending order.
package com.javatutsworld.stream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Java8Stream {
public static void main(String a[]) {
Stream<AccountDetails> stream = Stream.of(
new AccountDetails("Saaim", "00266767", 2000),
new AccountDetails("Sudhir", "00266788", 30000),
new AccountDetails("Gaurav", "00266798", 90000),
new AccountDetails("Ram", "00266799", 50000));
List<AccountDetails> listAccountDetails=stream.sorted(Comparator.comparingInt(AccountDetails::getAmount)).collect(Collectors.toList());
for(AccountDetails acctName: listAccountDetails){
System.out.println(acctName.getAmount());
}
}
}
Output:
2000
30000
50000
90000
Example of Statistics operation
Statistics is used to calculate all statistical information when stream processing is being done such as count, min, max, sum, and average. In Java 8, DoubleSummaryStatistics for double data type, IntSummaryStatistics for integer data type and LongSummaryStatistics for long data type.
Here is the code which shows how to use Statistics operation
package com.javatutsworld.stream;
import java.util.IntSummaryStatistics;
import java.util.stream.Stream;
public class Java8Stream {
public static void main(String a[]) {
Stream<AccountDetails> stream = Stream.of(
new AccountDetails("Saaim", "00266767", 2000),
new AccountDetails("Sudhir", "00266788", 30000),
new AccountDetails("Gaurav", "00266798", 90000),
new AccountDetails("Ram", "00266799", 50000));
IntSummaryStatistics intSummaryStats=stream.mapToInt(AccountDetails::getAmount).summaryStatistics();
System.out.println("Max:"+intSummaryStats.getMax()+", Min:"+intSummaryStats.getMin());
System.out.println("Count:"+intSummaryStats.getCount()+", Sum:"+intSummaryStats.getSum());
System.out.println("Average:"+intSummaryStats.getAverage());
}
}
Output:
Max:90000, Min:2000
Count:4, Sum:172000
Average:43000.0