Overview Of Java Streams Class

Posted By : Rohan Dodeja | 27-May-2018

Streams?

Streams are basically just IO to a storage in any system.

In hardware level we use streams of data to and fro from 1 device to another like basic example is a keyboard, keyboard send a stream of character to IO manager of the system and IO manager sends a stream of character to CPU in machine language, this is how stream works at the hardware level.

Similarly in java there are streams to storage classes like collections is the storage classes which uses data structures to store data in different manners, now Stream helps us to have a fast accessible access to storage objects, this will fasten the reads from any collection class.

Stream class is in util package in java library, it is just like InputStream, OutputStream, BufferInputStream.

Following are the ways to use Stream class:-

a) Stream.of(variable args...)

public class StreamEX {
     public static void main(String... params){
         Stream<Float> stream = Stream.of(1f,2f,3f,4f,5f,6f,7f,8f,9f);
         stream.forEach(p -> s.o.p(p));
     }
}
b) Stream.of(array)

public class StreamEX {
     public static void main(String... params){
         Stream<Float> stream = Stream.of( new Float[]{1f,2f,3f,4f,5f,6f,7f,8f,9f} );
         stream.forEach(p -> s.o.p(p));
     }
}
c) anylist.stream()

public class StreamEX {
     public static void main(String... params){
         List<Float> lister = new ArrayList<Float>();
         for(int i = 1; i< 33; i++){
             lister.add(i);
         }
         Stream<Float> streamer = lister.stream();
         streamer.forEach(p -> s.o.p(p));
     }
}
d) Stream.iterate() and Stream.generate() methods

public class StreamEX {
     public static void main(String... params){
         Stream<util.Date> streamer = Stream.generate(() -> { return new util.Date();});
         streamer.forEach(p -> s.o.p(p));
     }
}
e) String chars and String literals

public class StreamEX {
     public static void main(String... params){
        IntStream streamer = "1_abcdg".chars();
        streamer.forEach(p -> s.o.p(p));
         
        Stream<String> streamer = Stream.of("A$C".split("\\$"));
        stream.forEach(p -> s.o.p(p));
     }
}

 

Some more ops with Streams:-


1) filter

used to filter elements in a stream. This op is intermediate this enables to call other stream op like forEach method on a result.

memNames.stream().filter((m) -> m.startsWith("R")).forEach(System.out::println);
                                 
Result:-
 
Rohan
Roshan

2) map

This operation map() converts each datum into other object with the given method. The following ex converts each string into upper case string. But we can also use map to make each object into other object type.

memNames.stream().filter((m) -> m.startsWith("R")).map(String::toUpperCase).forEach(System.out::println);
                             
Result:-

Roshan 
Rohan

About Author

Author Image
Rohan Dodeja

Rohan is a bright web app Lead developer and have experience on Java, J2EE, REDIS, SQL. His hobbies are gaming and Travelling.

Request for Proposal

Name is required

Comment is required

Sending message..