Java 8 — Streams | Code Factory

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

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);
<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
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]
Optional<T> min(Comparator<? super T> 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
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
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
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

--

--

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