Streams

Java 8 Streams

To process objects of the collection, in 1.8 version Streams concept introduced.

What is the differences between Java.util.streams and Java.io streams?

java.util streams meant for processing objects from the collection. i.e, it represents a stream of objects from the collection but Java.io streams meant for processing binary and character data with respect to file. i.e it represents stream of binary data or character data from the file .hence Java.io streams and Java.util streams both are different.

What is the difference between collection and stream?
  • If we want to represent a group of individual objects as a single entity then We should go for collection.
  • If we want to process a group of objects from the collection then we should go for streams.
  • We can create a stream object to the collection by using stream() method of Collection interface. stream() method is a default method added to the Collection in 1.8 version.
default Stream stream()

E.g

Stream stream = c.stream();
  • Stream is an interface present in java.util.stream. Once we got the stream, by using that we can process objects of that collection.
  • We can process the objects in the following 2 phases

1.Configuration
2.Processing

1. Configuration

We can configure either by using filter mechanism or by using map mechanism.

1. Filtering

We can configure a filter to filter elements from the collection based on some boolean condition by using filter() method of Stream interface.

public Stream filter(Predicate<T> t)

Here (Predicate<T > t) can be a boolean valued function/lambda expression.

E.g

Stream s = c.stream();
Stream s1 = s.filter(i -> i%2==0);

Hence to filter elements of collection based on some Boolean condition we should go for filter() method.

2. Mapping

If we want to create a separate new object, for every object present in the collection based on our requirement then we should go for map() method of Stream interface.

public Stream map (Function f);

Here Function can be lambda expression also.

E.g

Stream s = c.stream();
Stream s1 = s.map(i-> i+10);

Once we performed configuration we can process objects by using several methods.

2. Processing
  • processing by collect() method
  • Processing by count()method
  • Processing by sorted()method
  • Processing by min() and max() methods
  • forEach() method
  • toArray() method
  • Stream.of()method
1. Processing by collect() method

This method collects the elements from the stream and adding to the specified to the collection indicated (specified) by argument.

E.g

To collect only even numbers from the array list

Without Streams

package com.ashok.java8.streams;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test {
   public static void main(String[] args) {
      List<Integer> list1 = new ArrayList<>();
      for (int i = 0; i < 10; i++) {
         list1.add(i);
      }
      System.out.println(list1);

      List<Integer> list2 = new ArrayList<>();
      for (Integer i : list1) {
         if (i % 2 == 0)
            list2.add(i);
      }
      System.out.println(list2);
   }
}

Output

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 4, 6, 8]

With Streams

package com.ashok.java8.streams;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test {
   public static void main(String[] args) {
      List<Integer> list1 = new ArrayList<>();
      for (int i = 0; i < 10; i++) {
         list1.add(i);
      }
      System.out.println(list1);

      List<Integer> list2 = list1.stream().filter(i -> i%2==0).collect(Collectors.toList());
      System.out.println(list2);
   }
}

Output

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 4, 6, 8]

Program for map() and collect() Method

package com.ashok.java8.streams;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class MapFilterTest {
   public static void main(String[] args) {
      List<String> list1 = new ArrayList<>();
      list1.add("Ashok");
      list1.add("Kumar");
      list1.add("Vinod");
      list1.add("Kumari");
      System.out.println(list1);
      
      List<String> list2 = list1.stream().map(s ->s.toUpperCase()).collect(Collectors.toList());
      System.out.println(list2);
   }
}

Output

[Ashok, Kumar, Vinod, Kumari]
[ASHOK, KUMAR, VINOD, KUMARI]
2. Processing by count()method
package com.ashok.java8.streams;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class FilterCountTest {
   public static void main(String[] args) {
      List<String> list1 = new ArrayList<>();
      list1.add("Ashok");
      list1.add("Kumar");
      list1.add("Vinod");
      list1.add("Kumari");
      System.out.println(list1);

      long count = list1.stream().filter(s -> s.length() == 5).count();
      System.out.println(count);
   }
}

Output

[Ashok, Kumar, Vinod, Kumari]
3
3. Processing by sorted()method

If we sort the elements present inside stream then we should go for sorted() method. The sorting can either default natural sorting order or customized sorting order specified by comparator.

1. sorted() : default natural sorting order
2. sorted(Comparator c) : customized sorting order.

package com.ashok.java8.streams;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class SortedTest {
   public static void main(String[] args) {
     List<Integer> list = new ArrayList<>();
      list.add(10);
      list.add(5);
      list.add(7);
      list.add(21);
      list.add(35);
      List<Integer> list1 = list.stream().sorted().collect(Collectors.toList());
      System.out.println(list1);
   }
}

Output

[5, 7, 10, 21, 35]  
package com.ashok.java8.streams;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class SortedComparatorTest {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>();
      list.add(10);
      list.add(5);
      list.add(7);
      list.add(21);
      list.add(35);
      List<Integer> list1 = list.stream().sorted((s1, s2) -> -s1.compareTo(s2)).collect(Collectors.toList());
      System.out.println(list1);
   }
}

Output

[35, 21, 10, 7, 5]
4. Processing by min() and max() methods
package com.ashok.java8.streams;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class MinMaxTest {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>();
      list.add(10);
      list.add(5);
      list.add(7);
      list.add(21);
      list.add(35);
      Integer min = list.stream().min((s1,s2) -> s1.compareTo(s2)).get();;
      System.out.println(min); // 5
      
      Integer max = list.stream().max((s1,s2) -> s1.compareTo(s2)).get();;
      System.out.println(max); // 35
   }
}
5. forEach() method

This method will not return anything. This method will take lambda expression as argument and apply that lambda expression for each element present in the stream.

package com.ashok.java8.streams;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class ForEachTest {
   public static void main(String[] args) {
     List<Integer> list = new ArrayList<>();
      list.add(0);
      list.add(15);
      list.add(10);
      list.add(5);
      list.add(30);
      list.add(25);
      list.add(20);

      list.stream().forEach(i -> System.out.println(i));
      System.out.println("Using :: operator ");
      list.stream().forEach(System.out::println);
   }
}

Output

0
15
10
5
30
25
20
Using :: operator 
0
15
10
5
30
25
20
6. toArray() method

We can use toArray() method to copy elements present in the stream into specified array.

package com.ashok.java8.streams;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class ToArrayTest {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>();
      list.add(0);
      list.add(15);
      list.add(10);
      list.add(5);
      list.add(30);
      list.add(25);
      list.add(20);
      Integer[] ir = list.stream().toArray(Integer[]::new);
      for (Integer i : ir) {
         System.out.println(i);
      }
   }
}

Output

0
15
10
5
30
25
20
7. Stream.of() method
package com.ashok.java8.streams;

import java.util.stream.Stream;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class StreamOfTest {
   public static void main(String[] args) {
      Stream s = Stream.of(10, 20, 30, 40, 50);
      s.forEach(System.out::println);
      Double[] d = { 10.0, 10.1, 10.2, 10.3, 10.4};
      Stream s1 = Stream.of(d);
      s1.forEach(System.out::println);
   }
}
Streams
Scroll to top