Java 8 Stream filter Sample

Java 8 Streams filter operation returns a stream consisting of the elements of this stream that match the given predicate. The Stream.filter() operation will transform the data of one stream into another stream using whatever condition you have set.

The filter() operation allows you to filter a list, an array or a map based on pretty much any condition. The cool part is that this can easily be done in one line using lambda expression with stream filter.

Stream filter() & String.length()

Sample code that implements the filter() operation on a stream based on length using the String.length() of each element in the stream.

//Create an Array of String
List<String> jcdArray = Arrays.asList("Java", "Code", "Depot", "Samples", "And", "Tutorials");
		
//Creating the stream using Array
Stream<String> jcdNames = jcdArray.stream();
			
//Create new stream using filter() based on String length
Stream<String> lenFilter = jcdNames.filter(str -> str.length() >= 6);
		
//Print out String based on length filter
lenFilter.forEach(jcdStr->System.out.print("Length >= 6: "+jcdStr+"\n"));

Output:

Length >= 6: Samples
Length >= 6: Tutorials


Stream filter() & String.contains()

Sample code that implements the filter() operation on a stream based on containing a specific character using the String.contains() of each element in the stream.

//Create an Array of String
List<String> jcdArray = Arrays.asList("Java", "Code", "Depot", "Samples", "And", "Tutorials");
		
//Creating the stream using Array
Stream<String> jcdNames = jcdArray.stream();
		
//Create new stream using filter() based on String length
Stream<String> lenFilter = jcdNames.filter(str -> str.contains("o"));
				
//Print out String based on length filter
lenFilter.forEach(jcdStr->System.out.print("Contains an o: "+jcdStr+"\n"));

Output:

Contains an o: Code
Contains an o: Depot
Contains an o: Tutorials


Stream filter() & Integer Greater Than

Sample code that implements the filter() operation on the stream to filter based on stream element being greater than a given number. Filtering on integers greater than 50.

//Create an Array of Integers
List<Integer> jcdIntArray = Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80, 90);
		
//Creating the stream using integer Array
Stream<Integer> jcdIntegers = jcdIntArray.stream();
		
//Create new stream using filter() based on integer value
Stream<Integer> valueFilter = jcdIntegers.filter(i -> i > 50);	
		
//Print out Integer based on value filter
valueFilter.forEach(jcdInt->System.out.print("Greater Than 50: "+jcdInt+"\n"));

Output:

Greater Than 50: 60
Greater Than 50: 70
Greater Than 50: 80
Greater Than 50: 90


Stream filter() & Integer Less Than & Greater Than

Sample code that implements the filter() operation on the stream to filter based on stream element being greater than a given number and less than another number. Filtering on integers greater than 20 but less than 80.

//Create an Array of Integers
List<Integer> jcdIntArray = Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80, 90);
		
//Creating the stream using integer Array
Stream<Integer> jcdIntegers = jcdIntArray.stream();
		
//Create new stream using filter() based on integer value
Stream<Integer> valueFilter = jcdIntegers.filter(i -> i > 20 && i < 80);	
		
//Print out Integer based on value filter
valueFilter.forEach(jcdInt->System.out.print("Greater > 20 < 80: "+jcdInt+"\n"));

Output:

Greater > 20 < 80: 30
Greater > 20 < 80: 40
Greater > 20 < 80: 50
Greater > 20 < 80: 60
Greater > 20 < 80: 70


The Java 8 Stream filter() operation is an intermediate operation, meaning this operation will return a new stream. The filter() operation/strong> is also referred to as a stateless operation. This just means that it retains no state from the previously seen element when processing a new element.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *