Java 8 — Streams | Code Factory

Introduction to 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() :

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);
}
}
[0, 10, 20]
[0, 10, 20]
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);
}
}
[0, 30, 60, 15, 45, 75]
[0, 30, 60, 15, 45, 75]

Differnce between filter() and map() :

  • 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);
  • 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 :

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);
}
}
[0, 10, 20]
[0, 30, 60, 15, 45, 75]
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);
}
}
3
  • 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);
}
}
[0, 5, 10, 15, 20, 25]
[25, 20, 15, 10, 5, 0]
  • returns minimum value according to specified Comparator
Optional<T> min(Comparator<? super T> comparator)
  • 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);
}
}
Min : 0, Max : 25
  • 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);
}
}
0 10 20 5 15 25 
0
10
20
5
15
25
  • 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);
}
}
}
0
10
20
5
15
25
  • We can also apply stream for group of values & for arrays
Stream<Integer> s = Stream.of(9,99,999,9999);
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);
}
}
9
99
999
9999
152.5
42.3
64.2

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Best Cloud Cost Management Tools of 2020

Choosing Clarity: Data infrastructure and your inner-Neo

YoungInnovations Weekly #75: Innovators@Meet-ups, Introducing Peer Reward, Code Kata, Celebrating…

Apache Beam: AVRO streaming using Pub/Sub in Python

Weekly Digest #34

Zenity Commands

A little diary | Day 3 🇧🇾

The strange art of Project Management

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

What is java?

Single Responsibility Principle (SRP)

Java Functional Interfaces