Streams
With Java 8 Streams we can make bulk operations.
/**
* With Java 8 Streams we can make bulk operations ...
* sequentiallly or paralel.
*
* The source for streams can be arrays, files, regex, etc.
* We don't have to create a collection in order to work with streams.
*/
package com.minte9.collections.streams;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class Streams {
public static void main(String[] args) {
List<String> myList = Arrays.asList("aa", "ba", "bc");
myList
.stream()
.filter(s -> s.startsWith("b"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
// BA BC
Stream.of("aa", "ba", "bc") // no collection
.filter(s -> s.startsWith("b"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
// BA BC
Arrays.stream(new int[] {1, 2, 3}) // primitives
.map(n -> n * 2)
.average()
.ifPresent(System.out::println);
// 4.0
}
}
Declarative
Using declarative approach we can create code that is reusable and testable.
/**
* The Problem:
*
* Given a list of integers,
* find the square of all the even numbers
* and return them in a new list.
*/
package com.minte9.collections.streams;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Declarative {
static List<Integer> mySquareList = new ArrayList<>();
static List<Integer> mylist = Arrays.asList(
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
);
public static void main(String[] args) {
// Imperative programming
for(Integer n : mylist) {
if (n % 2 == 0) {
mySquareList.add(n * n);
}
}
System.out.println(mySquareList); // [4, 16, 36, 64, 100]
// Declarative - no lambdas
mySquareList = mylist.stream()
.filter(
new Predicate<Integer>() {
@Override public boolean test(Integer n) {
return n % 2 == 0;
}
})
.map(
new Function<Integer, Integer>() {
@Override public Integer apply(Integer n) {
return n * n;
}
}
)
.collect(Collectors.toList());
System.out.println(mySquareList); // [4, 16, 36, 64, 100]
// Declarative - lambdas
mySquareList = mylist.stream()
.filter(x -> x % 2 == 0)
.map(y -> y * y)
.collect(Collectors.toList());
System.out.println(mySquareList); // [4, 16, 36, 64, 100]
}
}
Anonimous
Lambdas removed the need of writing anonymous functions.
/**
* Streams are collection of objects on which you can apply ...
* various methods while they are in the pipeline of execution.
*
* Lambdas are language constructs that remove the need of ...
* writing anonymous functions.
*/
package com.minte9.collections.streams;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class Anonimous {
public static void main(String[] args) {
/**
* Filter:
* ----------------
* Only elements that meet the condition ...
* will be passed to the next function in the pipeline
*/
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream()
.filter(x -> x % 2 == 0)
.forEach(System.out::println);
// 2 4
/**
* Map:
* ---------------
* Used to transform an element into other element
*/
List<String> letters = Arrays.asList("a", "b", "c");
letters.stream()
.map(x -> x.toUpperCase())
.forEach(System.out::println);
// A B C
/**
* ForEach:
* ---------------
* Runs for every element in the stream
*/
List<Integer> myNumbers = Arrays.asList(1, 2, 3, 4, 5);
myNumbers.stream()
.filter(x -> x % 2 == 0)
.map(x -> x * x)
.forEach(System.out::println);
// 4 16
/**
* Collect:
* ----------------
* Collects every element in the stream to another collection
*/
List<String> myList = Arrays.asList("London", "Paris", "London");
Set<String> mySet = myList.stream()
.filter(x -> x.length() % 2 == 0)
.collect(Collectors.toSet()); // no duplicates (Set)
System.out.println(mySet);
// [London]
}
}
Last update: 402 days ago