Java 8 Streams

 376 total views,  3 views today

Hello! Welcome to Java 8 series.In this tutorial you will be leaning about Streams in Java.

Stream is a new and important feature of Java 8 and it is available in java.util.stream package.

What is a Stream?

  • Streams are a sequence of objects from a source. It reads the source and forms a sequence of objects.
  • Stream is a pipeline of operations which can transform the source data and do some action on the source data.
  • Stream is a functional programming.

What are the feature of Stream?

  1. Stream doesn’t have any structure. To put it simply, it is not a data structure. It just collects the data from the source. A source can be a collection, a list or a set. Please note, Stream cannot be created from the map as map belongs to collection interface.
  2. We can write many pipeline method one by one on stream and get the desired result.
  3. Applying operations on a stream doesn’t change the source. Each pipeline method is called intermediate operations and end of the method to retrieve the result is called terminal operation.
  4. Stream is lazy loading. It doesn’t load the data when we perform an intermediate operation. It only loads when the desired result is expected.i.e when a terminal operation is invoked.
  5. Stream doesn’t hold or store the data. It collects the data from the source and performs an operation on it.

How to create a Stream in Java?

There are multiple ways to create a Stream. Let us look at it one by one.

To import a Stream -> “import java.util.stream.Stream;”

1. Create Stream from the collection.

Suppose you have a collection. You can create a stream from the collection by calling stream() function on the collection.

Example of List

public static void main(String[] args) {
        //Create a list
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
       
        //Create a stream
        Stream<String> stream = list.stream();
       
        //Access the stream
        stream.forEach(System.out::println);
 }

Example of set

public static void main(String[] args) {     
        //Create a set   
        List<String> set = new ArrayList<>();
        set.add("1");
        set.add("2");
        set.add("3");
        set.add("4");
       
        //Create a stream
        Stream<String> setStream = set.stream();
       
        //Access the stream
        setStream.forEach(System.out::println);
}

Example of Queue

public static void main(String[] args) {      
        Queue<Integer> queue = new PriorityQueue<>();
        queue.add(1);
        queue.add(2);
        queue.add(3);
       
        //Create a Stream
        Stream<Integer> queueStream = queue.stream();
       
        //Access the stream
        queueStream.forEach(System.out::println);
}

2. Using Stream.Of(T t…)

//Stream of Integer
Stream<Integer> integerStream =  Stream.of(1,2,3,4,5,6);
       
//Stream of String
Stream<String> stringStream =  Stream.of("a","b","c","d");

3. Create a Stream from array.

//IntStream
int[] array = { 1, 2, 3, 4, 5 };
IntStream intStream = Arrays.stream(array);

//Stream of String
String[] string = {"a","b","c"};
Stream<String> stringStreams = Arrays.stream(string);

4. Using Stream.empty()

//Empty string stream
Stream<String> emptyStringStream =  Stream.empty();
       
//Empty Integer stream
Stream<Integer> emptyIntegerStream =  Stream.empty();

5. Using Stream.builder

//Stream of Strings
Stream.Builder<String> builder =  Stream.builder();
Stream<String> example = builder.add("1").add("2").add("3").build();
example.forEach(System.out::print);
       
//Stream of integer
Stream.Builder<Integer> builder =  Stream.builder();
Stream<Integer> example = builder.add("1").add("2").add("3").build();
example.forEach(System.out::print);

References

Java 8 Stream official documentation

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.