Posts

Stateless vs Statefull Operations

  map and filter takes elements from the stream and it provides the resultant stream with zero or more elements. There operation doesn't need to cache the state or elements. Once the operation done on any element it doesn't need to cache its state for performing operation next element. So, these map and filter are stateless operations. While reduce, sort and distinct all such operation required to cache the state of elements because to perform such operation it needs to have the previous state of the operation. So, these reduce, distinct and sort operations are state full operations.

Stream complex examples like Queries

import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; class Client { private final String name; private final String country; public Client(String n, String c) { this.name = n; this.country = c; } public String getName() { return this.name; } public String getCountry() { return this.country; } public String toString() { return "Client:" + this.name + " in " + this.country; } } class Balance { private final Client client; private final int year; private final int amount; public Balance(Client client, int year, int amount) { this.client = client; this.year = year; this.amount = amount; } public Client getClient() { return this.client; } public int getYear() { return this.year; } public int getAmount() { return this.amount; } public String toString() { return "{" + this.client + ", " + "...

mapToInt() mapToLong() and mapToDouble() example

import java.util.Arrays; import java.util.List; import java.util.OptionalDouble; public class MapToIntLongDoubleFlight { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1,2,3,4,5,6); int total = numbers .stream() .mapToInt(num->num) .sum(); System.out.println("Total: "+total); OptionalInt min = numbers .stream() . mapToInt (num->num) . min (); System.out.println("Smallest Value: "+min.getAsInt()); OptionalInt max = numbers .stream() . mapToInt (num->num) . max (); System.out.println("Largest Value: "+max.getAsInt()); OptionalDouble average =  numbers .stream() .mapToDouble(num->num) .average(); System.out.println("Average: "+average.getAsDouble()); long totalLong = numbers .stream() .mapToLong(num->num*10000) .sum(); System.out.println("Total Long: "+totalLong); } } Outpu...

reduce() to find min and max

import java.util.Arrays; import java.util.List; import java.util.Optional; public class MaxMinReduceFlight { public static void main(String[] args) { List<Integer> nums = Arrays.asList(11,2,3,4,5,6,7); int max = nums.stream().reduce(0,Integer::max); Optional<Integer> resultMin = nums.stream().reduce(Integer::min); System.out.println("max="+max); System.out.println("min="+resultMin.get()); } } Output: max=11 min=2 Explanation: Integer::max: This method reference will be used to find max value. Integer::min:  This method reference will be used to find min value. for finding minimum value we have taken Optional to get the result because if we will take  initial value 0 then it will give us 0 as a resulted minimum value.

reduce() example

import java.util.Arrays; import java.util.List; public class ReduceFlight { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7); int sum = nums.stream() . reduce ( 0, (n1,n2)->n1+n2 ) ; System.out.println("Sum="+sum);  } } Output: Sum=28 Explanation: reduce is used to convert all values of stream into a single element. It means it is used to reduce stream. reduce ( 0, (n1,n2)->n1+n2 ): it takes two arguments 1st argument is an initial value. 2nd argument is BinaryOperator<T>. It takes two elements in argument and return new element. int sum = numbers.stream().reduce(0, Integer::sum); you can also use method reference to find sum of all elements of stream. int product = nums.stream().reduce(1, (n1,n2)->n1+n2); above is another line of code to find product of the numbers in list. Optional<Integer> sum =  numbers.stream().reduce((n1, n2) -> (n1 + n2)); Above is another line...

findAny() isPresent() and ifPresent() example

import java.util.Arrays; import java.util.List; import java.util.Optional; public class FinyAnyIsPresentIfPresentFlight { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7); Optional<Integer> result = nums .stream() .filter(num->num%2==0) . findAny (); System.out.println("isPresent:"+ result.isPresent() ); System.out.println("resultVal: "+ result.get() ); nums.stream() .filter(num->num%2==0) .findAny() . ifPresent (val->System.out.println("ifPresent: "+val)); } } Output: isPresent:true resultVal: 2 ifPresent: 2 Explanation: findAny(): It find any element from the stream which match the given filter condition. isPresent() : In result optional if the resulted value is available then it will give true else false . result.get(): It returned the found element or it will give NoSuchElementException  ifPresent(): The lamda expr...

allMatch anyMatch and noneMatch

import java.util.Arrays; import java.util.List; public class MatchFlight { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7); boolean allPositive = nums.stream(). allMatch (num->num>0); System.out.println("allPositive: "+allPositive); boolean anyEvenAvailable = nums.stream() . anyMatch (num->num%2==0); System.out.println("anyEvenAvailable: "+anyEvenAvailable); boolean noneNegative = nums.stream(). noneMatch (num->num<0); System.out.println("noneNegative: "+noneNegative); } } Output: allPositive: true anyEvenAvailable: true noneNegative: true Exaplanation: allMatch : It checks all for the elements in the stream match the provided filter condition. anyMatch: It checks any element in the stream match the provided filter condition. noneMatch: It check none element in the stream match the provided filter condition.