Java 8 — Streams | Code Factory

Code Factory
4 min readMay 11, 2020

--

Index Page : Link

Reference Link : Link

Donate : Link

Introduction to Stream :

Difference between Collection & Stream

  • Collection is a group of objects as a single entity.
  • To process objects from the collection we should go for Stream.

Methods of Stream : filter() and map() :

filter() method :

package com.codeFactory.stream;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String... args) {
ArrayList<Integer> i = new ArrayList<>();
i.add(0);
i.add(10);
i.add(20);
i.add(5);
i.add(15);
i.add(25);

// without stream (until 1.7v)
List<Integer> i1 = new ArrayList<>();
for(Integer x : i) {
if(x%2 == 0) {
i1.add(x);
}
}
System.out.println(i1);

// with stream (1.8v onwords)
List<Integer> i2 = i.stream().filter(x -> x%2 == 0).collect(Collectors.toList());
System.out.println(i2);
}
}

Output :

[0, 10, 20]
[0, 10, 20]

map() method :

package com.codeFactory.stream;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String... args) {
ArrayList<Integer> i = new ArrayList<>();
i.add(0);
i.add(10);
i.add(20);
i.add(5);
i.add(15);
i.add(25);

// without stream (until 1.7v)
List<Integer> i1 = new ArrayList<>();
for(Integer x : i) {
i1.add(x * 3);
}
System.out.println(i1);

// with stream (1.8v onwords)
List<Integer> i2 = i.stream().map(x -> x * 3).collect(Collectors.toList());
System.out.println(i2);
}
}

Output :

[0, 30, 60, 15, 45, 75]
[0, 30, 60, 15, 45, 75]

Differnce between filter() and map() :

Filtering

  • If we want to filter elements from the collection based on some boolean condition, then we should go for filtering.
  • We can configure filter by using filter() method of stream interface.
Stream<T> filter(Predicate<? super T> predicate)argument can be boolean valued function or Lambda Expression
e.g. Stream s = c.stream().filter(i -> i%2 == 0);

Mapping

  • If we want to create a seperate new object for every object present in the Collection based on some function then we should go for mapping mechanism.
  • We can implement mapping by using map() method of stream interface.
<R> Stream<R> map(Function<? super T,? extends R> mapper)e.g. Stream s = c.stream().map(i -> i * 2);

Various methods of Stream :

1. Processing by collect() method :

This method collects the elements from the stream and adding to the specified collection.

package com.codeFactory.stream;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String... args) {
ArrayList<Integer> i = new ArrayList<>();
i.add(0);
i.add(10);
i.add(20);
i.add(5);
i.add(15);
i.add(25);

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

List<Integer> i2 = i.stream().map(x -> x * 3).collect(Collectors.toList());
System.out.println(i2);
}
}

Output :

[0, 10, 20]
[0, 30, 60, 15, 45, 75]

2. Processing by count() method :

This method returns the number of elements present in Stream

package com.codeFactory.stream;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String... args) {
ArrayList<Integer> i = new ArrayList<>();
i.add(0);
i.add(10);
i.add(20);
i.add(5);
i.add(15);
i.add(25);

long count = i.stream().filter(x -> x%2 == 0).count();
System.out.println(count);
}
}

Output :

3

3. Processing by sorted() method :

  • We can use sorted() method to sort elements inside stream
  • We can sort either based on default natural sorting order or based on our own customized sorting order specified by Comparator object
  1. sorted() — for default natural sorting order
  2. sorted(Comparator c) — for customized sorting order
package com.codeFactory.stream;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String... args) {
ArrayList<Integer> i = new ArrayList<>();
i.add(0);
i.add(10);
i.add(20);
i.add(5);
i.add(15);
i.add(25);

List<Integer> i1 = i.stream().sorted().collect(Collectors.toList());
System.out.println(i1);

List<Integer> i2 = i.stream().sorted((x1, x2) -> x2.compareTo(x1)).collect(Collectors.toList());
//List<Integer> i2 = i.stream().sorted((x1, x2) -> x2 - x1).collect(Collectors.toList());
System.out.println(i2);
}
}

Output :

[0, 5, 10, 15, 20, 25]
[25, 20, 15, 10, 5, 0]

4. Processing by min() and max() methods :

min()

  • returns minimum value according to specified Comparator
Optional<T> min(Comparator<? super T> comparator)

max()

  • returns maximum value according to specified Comparator
Optional<T> max(Comparator<? super T> comparator)package com.codeFactory.stream;import java.util.ArrayList;public class Test {
public static void main(String... args) {
ArrayList<Integer> i = new ArrayList<>();
i.add(0);
i.add(10);
i.add(20);
i.add(5);
i.add(15);
i.add(25);

int min = i.stream().min((x1, x2) -> x1 - x2).get();
int max = i.stream().max((x1, x2) -> x1 - x2).get();
System.out.println("Min : " + min + ", Max : " + max);
}
}

Output :

Min : 0, Max : 25

5. Processing by using forEach() method :

  • This method won’t return anything
  • This method can take Lambda Expression as argument and apply that Lambda Expression for each element present in stream
package com.codeFactory.stream;import java.util.ArrayList;public class Test {
public static void main(String... args) {
ArrayList<Integer> i = new ArrayList<>();
i.add(0);
i.add(10);
i.add(20);
i.add(5);
i.add(15);
i.add(25);

i.stream().forEach(s -> System.out.print(s + " "));
System.out.println();
i.stream().forEach(System.out::println);
}
}

Output :

0 10 20 5 15 25 
0
10
20
5
15
25

6. Processing by toArray() method :

  • We can use toArray() method to copy
  • Elements present in the stream into specified array
package com.codeFactory.stream;import java.util.ArrayList;public class Test {
public static void main(String... args) {
ArrayList<Integer> i = new ArrayList<>();
i.add(0);
i.add(10);
i.add(20);
i.add(5);
i.add(15);
i.add(25);

Integer[] array = i.stream().toArray(Integer[]::new);
for(Integer x : array) {
System.out.println(x);
}
}
}

Output :

0
10
20
5
15
25

7. Stream.of() method :

  • We can also apply stream for group of values & for arrays

1. For group of values

Stream<Integer> s = Stream.of(9,99,999,9999);

2. For Arrays

Double[] d = {152.5, 42.3, 64.2};
Stream<Double> s1 = Stream.of(d);
package com.codeFactory.stream;import java.util.ArrayList;
import java.util.stream.Stream;
public class Test {
public static void main(String... args) {
ArrayList<Integer> i = new ArrayList<>();
i.add(0);
i.add(10);
i.add(20);
i.add(5);
i.add(15);
i.add(25);

Stream<Integer> s = Stream.of(9,99,999,9999);
s.forEach(System.out::println);

Double[] d = {152.5, 42.3, 64.2};
Stream<Double> s1 = Stream.of(d);
s1.forEach(System.out::println);
}
}

Output :

9
99
999
9999
152.5
42.3
64.2

--

--