Stream API are designed to be efficient and can support improving the performance of programs. We can avoid unnecessary loops and iterations in our programs by using Stream functionality. Streams can be used for filtering, collecting, printing, and converting data from one structure to another such as one collection to another collection.
Stream API are designed to be efficient and can support improving the performance of programs. We can avoid unnecessary loops and iterations in our programs by using Stream functionality. Streams can be used for filtering, collecting, printing, and converting data from one structure to another such as one collection to another collection.
Stream stream = collection.strim();
import java.io.*; import java.util.*; import java.util.stream.*; public class StreamFilterExample { static void main (
String
args[]){ int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; //Filtering the even numbers from a collection List<
Integer
> evenList = Arrays.asList(numbers).stream() .filter(i -> i % 2 == 0) .collect(Collectors.toList()); System.out.println( "Even numbers after stream operation : "+ evenList ); } } Output: Even numbers after stream operation : [2, 4, 6, 8, 10]
The stream filter() method provides the functionality to filter stream's elements on the basis of given lambda expression or predicate. The stream filter() method expects a lambda expression or predicate in argument and the lambda expression returns a boolean(true/false) value, based on boolean value filter method filters the elements.
Stream<
T
> filter(Predicate
super T> predicate)
//Importing required classes import java.util.*; import java.io.*; import java.util.stream.*; public class FilterExample { static void main (
String
args[]) {
Integer
[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; //Filtering the even numbers from a collection List<
Integer
> evenList = Arrays.asList(numbers) .stream() //passing lambda expression in argument .filter(i -> i % 2 == 0) .collect(Collectors.toList()); System.out.println( "Filtered even numbers : "+ evenList ); } } Output: Filtered even numbers : [2, 4, 6, 8, 10, 12]
//Importing required classes for stream import java.util.*; import java.io.*; import java.util.stream.*; public class PredicateFilterExample static void main (
String
args[] ){ //Creating predicate Predicate<
Integer
> pr = i -> ( i % 2 == 0 );
Integer
[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8 }; //Filtering the even numbers from a collection List<
Integer
> evenList = Arrays.asList(numbers) .stream() //passing predicate in argument .filter(pr) .collect(Collectors.toList()); System.out.println( "Filtered even numbers : "+ evenList ); } } Output: Filtered even numbers : [2, 4, 6, 8]
The stream map() method is an intermediate operation. This method transforms elements of a stream by applying a mapping function to each of the elements and returns a new stream. It accepts a Function mapper object as an argument. This function describes how each element in the original stream is transformed into an element in the new stream.
Stream map(Function mapper)
//Importing required classes import java.util.*; import java.io.*; import java.util.stream.*; public class StreamMapExample { public static void main (
String
args[]){
Integer
[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; //Filtering the even numbers from a collection List<
Integer
> evenList = Arrays.asList(numbers).stream() //passing lambda expression in argument .filter(i -> i % 2 == 0) //multiplying by 2 for each element after filter .map(i -> i*2) .collect(Collectors.toList()); System.out.println("Filtered even numbers : "+ evenList); } } Output: Filtered even numbers : [4, 8, 12, 16, 20, 24]
//Importing required classes import java.util.*; import java.io.*; import java.util.stream.*; public class StreamMapExample { public static void main (
String
args[]){ Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; //Filtering the even numbers from a collection Arrays.asList(numbers).stream() //passing lambda expression in argument .filter(i -> i % 2 == 0) //multiplying by 3 for each element after filter .map(i -> i*3) //forEach with method reference .forEach(System.out::println); } } Output: 6 12 18 24 30 36
Stream interface provides a sorted() method to sort a list. The sorted() method returns a sorted stream according to the natural order. If the elements are not comparable, it throws java.lang.ClassCastException.
Stream<
T<
> sorted()
Stream<
T<
> sorted(Comparator<
? Super T
> comparator)
//Importing required classes
import java.util.*;
import java.io.*;
import java.util.stream.*;
public class StreamSortedExample {
public static void main (
String
args[]){
Integer
[] numbers = { 100, 20, 13, 440, 5, 16, 7, 28, 2, 11, 1 };
List
Integer
> sortedList =
Arrays.asList(numbers).stream()
.sorted()
.collect( Collectors.toList() );
System.out.println( ""Sorted list : "+ sortedList );
}
}
Output:
Sorted list : [1, 2, 5, 7, 11, 13, 16, 20, 28, 100, 440]
//Importing required classes import java.util.*; import java.io.*; import java.util.stream.*; public class StreamSortedExample { public static void main (String args[]){ Integer[] numbers = { 100, 20, 13, 440, 5, 16, 7, 28, 2, 11, 1 }; List<
Integer
> sortedList = Arrays.asList( numbers ).stream() .sorted() //forEach with method reference .forEach( System.out :: println ); } } Output: 1 2 5 7 11 13 16 20 28 100 440
public class Students { private
Integer
age; private
String
name; Students(
Integer
age,
String
name){ this.age=age; this.name=name; } public String toString() { return this.age + ", "+ this.name; } } //Importing required classes import java.util.*; import java.io.*; import java.util.stream.*; public class SortedExampleWithComparator { public static void main (
String
args[]){ List<
Students
> studentsList = new ArrayList<
Students
>(); studentsList.add( new Students( 18, "Pankaj" )); studentsList.add( new Students( 20, "Prakash" )); studentsList.add( new Students( 16, "Karim" )); studentsList.add( new Students( 19, "Shyam" )); studentsList .stream() //comparator .sorted(( s1, s2 ) -> s1.age.compareTo( s2.age )) //forEach with method reference .forEach( System.out :: println ); } } Output: 16, Karim 18, Pankaj 19, Shyam 20, Prakash
The forEach() method is a default method defined in the Iterable and Stream interface.
It is a terminal operation in stream API. The purpose of the forEach() method is iterating
the elements one by one of a collection to create a result.
The forEach() method accept a single argument which is a
functional interface. So, you can pass a lambda expression in argument.
default void forEach(Consumer<super T
>action)
//Importing required classes import java.util.ArrayList; import java.util.List; public class ForEachExample { public static void main(
String
args[]){ //Declaring List List<
String
> countryList = new ArrayList<
String
>(); countryList.add( "Afghanistan" ); countryList.add( "Albania" ); countryList.add( "Algeria" ); countryList.add( "Australia" ); countryList.add( "India" ); countryList.add( "Brazil" ); countryList.add( "Japan" ); countryList.add( "Italy" ); //forEach method to printing country names countryList.forEach( country -> System.out.println( country ) ); } } Output: Afghanistan Albania Algeria India Australia Brazil Japan Italy
//Importing required classes import java.util.ArrayList; import java.util.List; public class ForEachExampleWithFilter { public static void main(String args[]){ //Declaring List List<
Integer
> intList = new ArrayList<
Integer
>(); intList.add( 100 ); intList.add( 99 ); intList.add( 78 ); intList.add( 3 ); intList.add( 6 ); intList.add( 8 ); intList.add( 50 ); intList.add( 55 ); //forEach method to printing even numbers intList.stream() .filter(i -> i%2==0) //method reference .forEach( System.out :: println ); } } Output: 100 78 6 8 50
public class Students {
privateInteger
age; private
String
name; Students(
Integer
age,
String
name){ this.age=age; this.name=name; } //toString() method for printing public
String
toString() { return this.age + ", "+ this.name; } } //Importing required classes import java.util.*; import java.io.*; import java.util.stream.*; public class SortedExampleWithComparator { public static void main (String args[]){ List<
Students
> studentsList = new ArrayList<
Students
>(); studentsList.add(new Students( 18, "Pankaj" )); studentsList.add(new Students( 20, "Prakash" )); studentsList.add(new Students( 16, "Karim" )); studentsList.add(new Students( 19, "Shyam" )); studentsList .stream() //filtering the studentsList with age greater than 15 .filter(s -> s.age>15) //forEach with method reference .forEach(System.out::println); } } Output: 20, Prakash 16, Karim 19, Tejas
The stream.collect() method accumulate elements of any Stream into a Collection,
it is a part of stream terminal operation.
The Collector class provides different methods like toList(), toSet(), toMap(), and toConcurrentMap() to collect the result of Stream into
List, Set, Map, and ConcurrentMap in Java.
It accepts a Collector in arguments to accumulate elements of Stream into a specified Collection
such as stream().collect( Collectors.toList() ), stream().collect( Collectors.toSet() ),
stream().collect (Collectors.toMap(....) ), stream().collect( Collectors.counting()) etc..
It is used to perform different types of reduction operations such as
calculating the sum of numeric values, finding the minimum or maximum number,
concatenating strings to new string, collecting elements into a new stream.
stream.collect()
List<
Integer
> evenNumList = list.stream().filter( i-> i%2 == 0 ) //Converting filtered data to a List using collect method .collect(Collectors.toList());
//Importing required classes import java.util.ArrayList; import java.util.List; import java.util.stream.*; public class CollectMethodExample { public static void main(String args[]){ //Declaring List List<
Integer
> intList = new ArrayList<
Integer
>(); intList.add( 6 ); intList.add( 11 ); intList.add( 14 ); intList.add( 7 ); intList.add( 3 ); intList.add( 2 ); intList.add( 16 ); intList.add( 15 ); intList.add( 19 ); List<
Integer
> evenNumList = intList.stream() .filter( i-> i%2==0 ) //Converting filtered data to a List .collect( Collectors.toList() ); System.out.println( "Even Num List" +evenNumList ) } } Output: Even Num List [6, 14, 2, 16]
public class Students {
Integer
rollNumber; final
String
studentName;; Students(
Integer
rollNumber,
String
name){ this.rollNumber=rollNumber; this.studentName=name; } public
String
toString() { return this.rollNumber + ", "+ this.studentName; } } //Importing required classes import java.util.*; import java.io.*; import java.util.stream.*; public class CollectMapExample { public static void main (String args[]){ List<
Students
> studentsList = new ArrayList<
Students
>(); studentsList.add( new Students( 1, "Pankaj" )); studentsList.add( new Students( 6, "Prakash" )); studentsList.add( new Students( 3, "Karim" )); studentsList.add( new Students( 2, "Shyam" )); studentsList.add( new Students( 4, "Tejas" )); studentsList.add( new Students( 5, "Rahul" )); Map<
Integer
,
String
> studentMap = studentsList.stream() .collect(Collectors .toMap( p -> p.rollNumber, p -> p.studentName)); //Printing the map System.out.println( "Student Map " +studentMap); } } Output: {1=Pankaj, 2=Shyam, 3=Karim, 4=Tejas, 5=Rahul, 6=Prakash}
The anyMatch() method returns elements whether any match with the provided predicate. The anyMatch() method executes untill determining the result. Its terminate the loop execution when found any match and returns the value.
boolean anyMatch(Predicate super T> predicate)
//Importing required classes import java.util.ArrayList; import java.util.List; import java.util.stream.*; public class AnyMatchMethodExample { public static void main(String args[]){ //Declaring List List<
Integer
> intList = new ArrayList<
Integer
>(); intList.add( 6 ); intList.add( 11 ); intList.add( 14 ); intList.add( 7 ); intList.add( 3 ); intList.add( 2 ); intList.add( 16 ); intList.add( 15 ); intList.add( 19 ); //calling anymatch method boolean result = intList.stream().anyMatch( n -> n==14 ); System.out.println( result ) } } Output: true
The stream allMatch() method returns whether all elements of this stream match the provided predicate. Its check whether all elements are present or not in the given stream.
boolean allMatch(Predicate super T> predicate)
//Importing required classes import java.util.ArrayList; import java.util.List; import java.util.stream.*; public class AllMatchMethodExample { public static void main(String args[]){ //Declaring List List<
Integer
> intList = new ArrayList<
Integer
>(); intList.add( 6 ); intList.add( 11 ); intList.add( 14 ); intList.add( 7 ); intList.add( 3 ); intList.add( 2 ); intList.add( 16 ); intList.add( 15 ); intList.add( 19 ); boolean result = intList.stream().anyMatch( n -> n%2 == 0 ); System.out.println( result ) } } Output: false
//Importing required classes import java.util.ArrayList; import java.util.List; import java.util.stream.*; public class AllMatchMethodExample { public static void main(String args[]){ //Declaring List List<
Integer
> intList = new ArrayList<
Integer
>(); intList.add(6); intList.add(14); intList.add(2); intList.add(16); boolean result = intList.stream().anyMatch( n -> n%2 == 0 ); System.out.println( result ) } } Output: true
The stream count() method is a part of terminal operations in java stream. The count() method returns the count of elements in the stream. The return type of count() method is long. It is the reduction process, it may traverse the stream to produce a result.
long count()
//Importing required classes import java.util.ArrayList; import java.util.List; import java.util.stream.*; public class CountMethodExample { public static void main(String args[]){ //Declaring List List<
Integer
> intList = new ArrayList<
Integer
>(); intList.add( 6 ); intList.add( 11 ); intList.add( 14 ); intList.add( 7 ); intList.add( 3 ); intList.add( 2 ); intList.add( 16 ); intList.add( 15 ); intList.add( 19 ); //calling count method long count = intList.stream().count(); System.out.println( count ) } } Output: 9
//Importing required classes import java.util.ArrayList; import java.util.List; import java.util.stream.*; public class CountMethodExample { public static void main(String args[]){ //Declaring List List<
Integer
> intList = new ArrayList<
Integer
>(); intList.add( 6 ); intList.add( 11 ); intList.add( 14 ); intList.add( 7 ); intList.add( 3 ); intList.add( 2 ); intList.add( 16 ); intList.add( 15 ); intList.add( 19 ); long count = intList.stream() .filter(i -> i%2==0) .count(); //calling count method System.out.println( count ) } } Output: 4
The reduce() method is a part of terminal operations in java stream. It performs operations to produce a single value as result by reducing, such as average(), sum(), min(), max(), and count() etc. The reduce() method applies the binary operator to each of the elements to reduce it to a single value. The return type of the reduce() method is type T or it can be an Optional.
Optional<
Integer
> max = intList.stream().reduce(( i, j ) -> i > j ? i : j);
Stream.reduce()
//Importing required classes import java.util.ArrayList; import java.util.List; import java.util.stream.*; public class ReduceMethodExample { public static void main(String args[]){ //Declaring List List<
Integer
> intList = new ArrayList<
Integer
>(); intList.add( 6 ); intList.add( 11 ); intList.add( 14 ); intList.add( 7 ); intList.add( 3 ); intList.add( 2 ); intList.add( 16 ); intList.add( 15 ); intList.add( 19 ); Optional<
Integer
> max = intList.stream() .reduce(( i, j ) -> i > j ? i : j); System.out.println( max ) System.out.println( max.get() ) } } Output: Optional[19] 19
//Importing required classes import java.util.ArrayList; import java.util.List; import java.util.stream.*; public class ReduceMethodExample { public static void main(String args[]){ //Declaring List List<
Integer
> intList = new ArrayList<
Integer
>(); intList.add( 6 ); intList.add( 3 ); intList.add( 2 ); intList.add( 4 ); intList.add( 5 ); //sum of all elements int sum = intList.stream().reduce(0, ( i, j ) -> i + j); System.out.println( sum ) } } Output: 20
//Importing required classes import java.util.ArrayList; import java.util.List; import java.util.stream.*; public class ReduceMethodExample { public static void main(
String
args[] ){ //Multiplication of all elements with in range //Range will be started from 2 to 5 //Its similar to 2*3*4*5 iint multiplicationValue = IntStream.range(2, 6) .reduce(( num1, num2 ) -> num1 * num2 ) .orElse(-1); System.out.println( multiplicationValue ) } } Output: 120
//Importing required classes import java.util.ArrayList; import java.util.List; import java.util.stream.*; public class ReduceMethodExample { public static void main(String args[]){ List<
String
> strList = new ArrayList<
String
>(); strList.add( "One" ); strList.add( "Two" ); strList.add( "Three" ); strList.add( "Four" ); strList.add( "Five" ); Optional<
String
> concatString = strList.stream() .reduce(( str1, str2 ) -> str1 + "-" + str2); System.out.println( concatString ); System.out.println( concatString.get() ); } } Output: Optional[One-Two-Three-Four-Five] One-Two-Three-Four-Five
The findFirst() method is a part of Short circuit operation in Java Stream. It returns an Optional 1st element of the stream or empty if the stream is empty.
T
> findFirst().Optional<T
> findFirst()
//Importing required classes
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class FindFirstExample {
public static void main(String args[]) {
List<String
> strList = new ArrayList<String
>();
strList.add("One");
strList.add("Two");
strList.add("Three");
strList.add("Four");
strList.add("Five");
Optional<String
> firstElement = strList.stream().findFirst();
System.out.println(firstElement);
System.out.println(firstElement.get());
}
}
Output:
Optional[One]
One
//Importing required classes
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class FindFirstExample {
public static void main(String args[]) {
List<Integer
> intList = new ArrayList<Integer
>();
intList.add(100);
intList.add(90);
intList.add(80);
intList.add(70);
intList.add(200);
Optional<Integer
> firstElement = intList.stream().findFirst();
System.out.println(firstElement);
System.out.println(firstElement.get());
}
}
Output:
Optional[100]
100
//Importing required classes
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class FindFirstExample {
public static void main(String args[]) {
List<String
> strList = new ArrayList<String
>();
Optional<String
> firstElement = strList.stream().findFirst();
System.out.println( firstElement );
System.out.println( "Optional.get() of empty" );
System.out.println( firstElement.get() );
}
}
Output:
Optional.empty
Optional.get() of empty
Exception in thread "main" java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.get(Optional.java:143)
at com.school.stream.FindFirstExample.main(FindFirstExample.java:21)
The andThen() and accept() methods are methods of Consumer Interfaces.
import java.util.function.Consumer; public class ConsumerInterfaceExample{ static void welcomeGreeting (
String
name){ System.out.println( "Welcome "+name ); } static void showAge (int age){ System.out.println( "Your age is "+age ); } static void showSalary (int sal){ System.out.println( "Your salary is "+sal ); } public static void main(
String
[] args) { //method reference Consumer<
String
> consumer1 = ConsumerInterfaceExample :: welcomeGreeting; // Calling Consumer method consumer1.accept( "Tejas" ); //method reference Consumer<
Integer
> consumer2 = ConsumerInterfaceExample::showAge; // Calling Consumer method consumer2.accept( 30 ); //method reference Consumer<
Integer
> consumer2 = ConsumerInterfaceExample :: showSalary; // Calling Consumer method consumer2.accept( 20000 ); } } Output: Welcome Tejas Your age is 30 Your salary is 20000
import java.util.function.Consumer; public class ConsumerInterfaceExample{ public static void main(String[] args) { //method reference Consumer<
String
> consumer1 = s -> System.out.println( "Welcome "+s ); Consumer<
String
> consumer2 = s -> System.out.println( "Welcome "+s+ "to the World!" ); Consumer<
String
> consumer2 = consumer1.andThen( consumer2 ); // Calling Consumer method consumer1.accept( "Tejas" ); } } Output: Welcome Tejas Welcome Tejas to the World!
Predicate accepts an argument and returns a boolean value. Predicate functional interface in Java is a type of function that accepts a single argument and returns a boolean (True/ False). It provides the functionality of filtering, it filter a stream components on the base of a provided predicate.
public static void main(
String[] args) { Predicate<
Integer
> pr1 = x -> (x > 20); Predicate<
Integer
> pr2 = x -> (x < 80); // Calling Predicate method System.out.println( pr1.and( pr2 ).test(100) ); // Calling Predicate method System.out.println( pr1.and( pr2 ).test(50) ); // Calling Predicate method System.out.println( pr1.and( pr2 ).test(10) ); } Output: false true false
public static void mai(
String[] args) { Predicate<
String
> pr = Predicate.isEqual( "MyAllText" ); // Calling Predicate method System.out.println( pr.test( "AllText" ) ); // Calling Predicate method System.out.println( pr.test( "MyAllText" ) ); // Calling Predicate method System.out.println( pr.test( "MyAll" ) ); } Output: false true false
public static void main(
String
[] args) { Predicate<
String
> startsWithCharJ = s -> s.startsWith( "H" ); Predicate<
String
> hasLengthOfInt5 = s -> s.length() == 5; // Calling without negate method System.out.println( startsWithCharJ.test( "Horse" ) ); // Calling without negate method System.out.println( hasLengthOfInt5.test( "India" ) ); Predicate<
String
> negateStartsWithCharJ = startsWithCharJ.negate(); Predicate<
String
> negatehasLengthOfInt5 = hasLengthOfInt5.negate(); // Calling after negate predicate System.out.println( negateStartsWithCharJ.test( "Horse" ) ); // Calling after negate predicate System.out.println( negatehasLengthOfInt5.test( "India" ) ); } Output: true true false false
public static void main(
String
[] args) { Predicate<
String
> startsWithCharH = s -> s.startsWith( "H" ); Predicate<
String
> hasLengthOfInt5 = s -> s.length() == 5; Predicate<
String
> startsWithCharHOrHasLengthOf5 = startsWithCharH.or( hasLengthOfInt5 ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "Hero" ) ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "India" ) ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "Method" ) ); } Output: true true false
public static void main(
String
[] args){ Predicate<
Integer
> pr = a -> ( a > 10 ); // Calling Predicate method System.out.println( pr.test(20) ); } Output: true
Predicate accepts an argument and returns a boolean value. Predicate functional interface in Java is a type of function that accepts a single argument and returns a boolean (True/ False). It provides the functionality of filtering, it filter a stream components on the base of a provided predicate.
public static void mai(
String[] args) { Predicate<
String
> pr = Predicate.isEqual( "MyAllText" ); // Calling Predicate method System.out.println( pr.test( "AllText" ) ); // Calling Predicate method System.out.println( pr.test( "MyAllText" ) ); // Calling Predicate method System.out.println( pr.test( "MyAll" ) ); } Output: false true false
public static void main(
String
[] args) { Predicate<
String
> startsWithCharJ = s -> s.startsWith( "H" ); Predicate<
String
> hasLengthOfInt5 = s -> s.length() == 5; // Calling without negate method System.out.println( startsWithCharJ.test( "Horse" ) ); // Calling without negate method System.out.println( hasLengthOfInt5.test( "India" ) ); Predicate<
String
> negateStartsWithCharJ = startsWithCharJ.negate(); Predicate<
String
> negatehasLengthOfInt5 = hasLengthOfInt5.negate(); // Calling after negate predicate System.out.println( negateStartsWithCharJ.test( "Horse" ) ); // Calling after negate predicate System.out.println( negatehasLengthOfInt5.test( "India" ) ); } Output: true true false false
public static void main(
String
[] args) { Predicate<
String
> startsWithCharH = s -> s.startsWith( "H" ); Predicate<
String
> hasLengthOfInt5 = s -> s.length() == 5; Predicate<
String
> startsWithCharHOrHasLengthOf5 = startsWithCharH.or( hasLengthOfInt5 ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "Hero" ) ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "India" ) ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "Method" ) ); } Output: true true false
public static void main(
String
[] args){ Predicate<
Integer
> pr = a -> ( a > 10 ); // Calling Predicate method System.out.println( pr.test(20) ); } Output: true
public static void main(
String[] args) { Predicate<
Integer
> pr1 = x -> (x > 20); Predicate<
Integer
> pr2 = x -> (x < 80); // Calling Predicate method System.out.println( pr1.and( pr2 ).test(100) ); // Calling Predicate method System.out.println( pr1.and( pr2 ).test(50) ); // Calling Predicate method System.out.println( pr1.and( pr2 ).test(10) ); } Output: false true false
Predicate accepts an argument and returns a boolean value. Predicate functional interface in Java is a type of function that accepts a single argument and returns a boolean (True/ False). It provides the functionality of filtering, it filter a stream components on the base of a provided predicate.
public static void main(
String
[] args) { Predicate<
String
> startsWithCharJ = s -> s.startsWith( "H" ); Predicate<
String
> hasLengthOfInt5 = s -> s.length() == 5; // Calling without negate method System.out.println( startsWithCharJ.test( "Horse" ) ); // Calling without negate method System.out.println( hasLengthOfInt5.test( "India" ) ); Predicate<
String
> negateStartsWithCharJ = startsWithCharJ.negate(); Predicate<
String
> negatehasLengthOfInt5 = hasLengthOfInt5.negate(); // Calling after negate predicate System.out.println( negateStartsWithCharJ.test( "Horse" ) ); // Calling after negate predicate System.out.println( negatehasLengthOfInt5.test( "India" ) ); } Output: true true false false
public static void main(
String
[] args) { Predicate<
String
> startsWithCharH = s -> s.startsWith( "H" ); Predicate<
String
> hasLengthOfInt5 = s -> s.length() == 5; Predicate<
String
> startsWithCharHOrHasLengthOf5 = startsWithCharH.or( hasLengthOfInt5 ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "Hero" ) ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "India" ) ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "Method" ) ); } Output: true true false
public static void main(
String
[] args){ Predicate<
Integer
> pr = a -> ( a > 10 ); // Calling Predicate method System.out.println( pr.test(20) ); } Output: true
public static void mai(
String[] args) { Predicate<
String
> pr = Predicate.isEqual( "MyAllText" ); // Calling Predicate method System.out.println( pr.test( "AllText" ) ); // Calling Predicate method System.out.println( pr.test( "MyAllText" ) ); // Calling Predicate method System.out.println( pr.test( "MyAll" ) ); } Output: false true false
public static void main(
String[] args) { Predicate<
Integer
> pr1 = x -> (x > 20); Predicate<
Integer
> pr2 = x -> (x < 80); // Calling Predicate method System.out.println( pr1.and( pr2 ).test(100) ); // Calling Predicate method System.out.println( pr1.and( pr2 ).test(50) ); // Calling Predicate method System.out.println( pr1.and( pr2 ).test(10) ); } Output: false true false
Predicate accepts an argument and returns a boolean value. Predicate functional interface in Java is a type of function that accepts a single argument and returns a boolean (True/ False). It provides the functionality of filtering, it filter a stream components on the base of a provided predicate.
public static void main(
String
[] args) { Predicate<
String
> startsWithCharH = s -> s.startsWith( "H" ); Predicate<
String
> hasLengthOfInt5 = s -> s.length() == 5; Predicate<
String
> startsWithCharHOrHasLengthOf5 = startsWithCharH.or( hasLengthOfInt5 ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "Hero" ) ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "India" ) ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "Method" ) ); } Output: true true false
public static void main(
String
[] args){ Predicate<
Integer
> pr = a -> ( a > 10 ); // Calling Predicate method System.out.println( pr.test(20) ); } Output: true
public static void mai(
String[] args) { Predicate<
String
> pr = Predicate.isEqual( "MyAllText" ); // Calling Predicate method System.out.println( pr.test( "AllText" ) ); // Calling Predicate method System.out.println( pr.test( "MyAllText" ) ); // Calling Predicate method System.out.println( pr.test( "MyAll" ) ); } Output: false true false
public static void main(
String[] args) { Predicate<
Integer
> pr1 = x -> (x > 20); Predicate<
Integer
> pr2 = x -> (x < 80); // Calling Predicate method System.out.println( pr1.and( pr2 ).test(100) ); // Calling Predicate method System.out.println( pr1.and( pr2 ).test(50) ); // Calling Predicate method System.out.println( pr1.and( pr2 ).test(10) ); } Output: false true false
public static void main(
String
[] args) { Predicate<
String
> startsWithCharJ = s -> s.startsWith( "H" ); Predicate<
String
> hasLengthOfInt5 = s -> s.length() == 5; // Calling without negate method System.out.println( startsWithCharJ.test( "Horse" ) ); // Calling without negate method System.out.println( hasLengthOfInt5.test( "India" ) ); Predicate<
String
> negateStartsWithCharJ = startsWithCharJ.negate(); Predicate<
String
> negatehasLengthOfInt5 = hasLengthOfInt5.negate(); // Calling after negate predicate System.out.println( negateStartsWithCharJ.test( "Horse" ) ); // Calling after negate predicate System.out.println( negatehasLengthOfInt5.test( "India" ) ); } Output: true true false false
Predicate accepts an argument and returns a boolean value. Predicate functional interface in Java is a type of function that accepts a single argument and returns a boolean (True/ False). It provides the functionality of filtering, it filter a stream components on the base of a provided predicate.
public static void main(
String
[] args){ Predicate<
Integer
> pr = a -> ( a > 10 ); // Calling Predicate method System.out.println( pr.test(20) ); } Output: true
public static void mai(
String[] args) { Predicate<
String
> pr = Predicate.isEqual( "MyAllText" ); // Calling Predicate method System.out.println( pr.test( "AllText" ) ); // Calling Predicate method System.out.println( pr.test( "MyAllText" ) ); // Calling Predicate method System.out.println( pr.test( "MyAll" ) ); } Output: false true false
public static void main(
String[] args) { Predicate<
Integer
> pr1 = x -> (x > 20); Predicate<
Integer
> pr2 = x -> (x < 80); // Calling Predicate method System.out.println( pr1.and( pr2 ).test(100) ); // Calling Predicate method System.out.println( pr1.and( pr2 ).test(50) ); // Calling Predicate method System.out.println( pr1.and( pr2 ).test(10) ); } Output: false true false
public static void main(
String
[] args) { Predicate<
String
> startsWithCharJ = s -> s.startsWith( "H" ); Predicate<
String
> hasLengthOfInt5 = s -> s.length() == 5; System.out.println("**Before Negating**") // Calling without negate method System.out.println( startsWithCharJ.test( "Horse" ) ); // Calling without negate method System.out.println( hasLengthOfInt5.test( "India" ) ); // Here we are negating Predicate<
String
> negateStartsWithCharJ = startsWithCharJ.negate(); Predicate<
String
> negatehasLengthOfInt5 = hasLengthOfInt5.negate(); System.out.println("**After Negating**") // Calling after negate predicate System.out.println( negateStartsWithCharJ.test( "Horse" ) ); // Calling after negate predicate System.out.println( negatehasLengthOfInt5.test( "India" ) ); } Output: **Before Negating** true true **After Negating** false false
public static void main(
String
[] args) { Predicate<
String
> startsWithCharH = s -> s.startsWith( "H" ); Predicate<
String
> hasLengthOfInt5 = s -> s.length() == 5; Predicate<
String
> startsWithCharHOrHasLengthOf5 = startsWithCharH.or( hasLengthOfInt5 ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "Hero" ) ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "India" ) ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "Method" ) ); } Output: true true false