Member-only story

Java Streams

Swati Sinha
2 min readAug 31, 2024

Stream says a lot itself by the name of what it does! An interesting feature introduced in Java 8 that simplifies working with data collections by providing a functional, declarative approach to processing sequences of elements. Let’s revise the concept quickly -

Photo by Brian Botos on Unsplash

The easiest way to remember, how to use the stream functions -

  1. Create Stream
  2. Intermediate Operations
  3. Terminal Operations

empList is ArrayList, create a stream , filter is intermediated operations and count use as Terminal Operations.

For example : empList.stream().filter((Integer dept)-> dept>10).count();

public class StreamExample1 {

public static void main(String[] args) {
Integer arr[] ={12,3,33,3,44,44,5,5};
System.out.println("Using distinct() Operation-" + Arrays.stream(arr).distinct().collect(Collectors.toList()));

System.out.println("Using sorted() Operation-" + Arrays.stream(arr).sorted().collect(Collectors.toList()));

System.out.println("Using map() Operation-" +Arrays.stream(arr).map((Integer a)->a*a).collect(Collectors.toList()));

Arrays.stream(arr).limit(5).forEach(System.out::println);
}
}
Response from above output###
***********************************

Using distinct() Operation-[12, 3, 33, 44, 5]
Using sorted()…

--

--

No responses yet