A stream is an Object, type of data
Stream and file streams are different
You can use Stream once, cannot be using again after consumption
import java.util.Arrays ;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
public class Demo{
public static void main(String[] args)
List<Integer> nums = Arrays.asList(4,5,7,8,9);
Stream<Integer> data =nums.stream() ;
Stream<Integer> sortedData= data.sorted();
sortedData.forEach(n->System.out.println(n));
}
other example can be seen in
import java.util.Arrays ;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
public class Demo{
public static void main(String[] args)
List<Integer> nums = Arrays.asList(4,5,7,8,9);
Stream<Integer> data =nums.stream() ;
Stream<Integer> sortedData= data.sorted();
sortedData.forEach(n->System.out.println(n));
}
More Examples and Use cases
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Streams{
public static void main(String[] args) {
System.out.println("this is streams");
final User[] arrayOfUsers = {
new User(1, "Jeff Bezos", 100000.0),
new User(2, "Bill Gates", 200000.0),
new User(3, "Mark Zuckerberg", 300000.0)
};
//for each
List<Integer> nums = Arrays.asList(4,5,7,8,9);
Stream<Integer> dataStream1 =nums.stream() ;
Stream<Integer> sortedData= dataStream1.sorted();
sortedData.forEach(n->System.out.print("| "+n+" | "));
System.out.println("printing each value");
Stream<Integer> dataStream2= nums.stream();
dataStream2.forEach(n->System.out.print(n));
//filter - criteria based result
List<User> userList= Arrays.asList(arrayOfUsers);
Stream<User> userListStream=userList.stream();
List<User> userFilteredListStream=userListStream.filter(string-> !string.getName().isEmpty())
.collect(Collectors.toList());
System.out.println(userFilteredListStream);
List<Integer> intList1= Arrays.asList(2,4,3,8,9,10);
System.out.println("even and double");
intList1.stream()
.filter(n -> n%2==0)
.map(n-> n*2)
.forEach(n-> System.out.print(n+" "));
//Count
Long userFilteredCountListStream=userList.stream().filter(string-> string.getName().length()>13).count();
System.out.println(userFilteredCountListStream);
//Map - map each element corresponding to result
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
List<Integer> squaredNumbers=numbers.stream().map(num->num*num).distinct().collect(Collectors.toList());
System.out.print("squaredNumbers" +squaredNumbers+"\n");
//limit- limit the number of list
List<Integer> list1= Arrays.asList(1,2,3,4,5,6,7, 8,9,10,11,12,13);
System.out.println("limiting result");
list1.stream().limit(10).forEach(System.out::print);
}
}
Collectors
Collectors are used to combine the result of processing on the elements of a stream. Collectors can be used to return a list or a string.like in the example we have used, I am putting new usernames so that you can run and test it
List<User> userList= Arrays.asList("Elon Musk", "Bill Gates", "Mark");
Stream<User> userListStream=userList.stream();
List<User> userFilteredListStream=userListStream.filter(string-> !string.getName().isEmpty())
.collect(Collectors.toList());
System.out.println(userFilteredListStream);
Mathematical and Statistical operations
List numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
IntSummaryStatistics stats = integers.stream().mapToInt((x) -> x).summaryStatistics();
System.out.println("Highest number in List : " + stats.getMax());
System.out.println("Lowest number in List : " + stats.getMin());
System.out.println("Sum of all numbers : " + stats.getSum());
System.out.println("Average of all numbers : " + stats.getAverage());
Map Filter Reduce
meaning of Predicate
Predicate is the object that is accepted by the filter, it can be illustrated by example
List<lnteger> nums = Arrays.asList(
4,5,7,3,2);
Predicate<lnteger> p = new Predicate<lnteger>() {
public boolean test(Integer n) {
return n%2==0;
}
};
//can be written as
//predicate p =n->n%2=0;
int result = nums.stream( )
. fitter(p)
.map(n -> n*2)
. reduce( 0, (c,e) -> c+e);
System.out.println(result);
the function is a functional interface having a method apply
Reduce
in reduce c is carry and element is e , so we keep adding two element to get the result
Parallel Stream
- Gives functionality of multiple filter
Reference:
A Guide to Java Streams in Java 8: In-Depth Tutorial With Examples (stackify.com)