Collect
Collect the stream values into a list.
/**
* Stream collect ...
*/
package com.minte9.lambdas.common_operations;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
public class Collect {
public static void main(String[] args) {
List<String> A = Stream.of("a", "b", "c")
.collect(Collectors.toList());
assertEquals(Arrays.asList("a", "b", "c"), A); // pass
}
}
Map
Map each value to uppercase.
/**
* Stream map (to uppercase)
*/
package com.minte9.lambdas.common_operations;
import java.util.ArrayList;
import java.util.List;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals;
public class MapApp {
public static void main(String[] args) {
List<String> A, B, C;
A = asList("a", "b", "c");
B = asList("A", "B", "C");
C = new ArrayList<>();
// For
for(String s : A) {
C.add(s.toUpperCase());
}
assertEquals(B, C); // pass
// Stream
C = A.stream()
.map(x -> x.toUpperCase())
.collect(toList());
assertEquals(B, C); // pass
}
}
Filter
Filter only strings that starts with a number.
/**
* Stream filter ...
*
* Strings that starts with a number (example)
*/
package com.minte9.lambdas.common_operations;
import static java.lang.Character.isDigit;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
public class Filter {
public static void main(String[] args) {
List<String> A, B, C;
A = asList("1abc");
B = asList("a", "1abc", "abc1");
C = new ArrayList<>();
// For
for(String s : A) {
if (isDigit(s.charAt(0))) {
C.add(s);
}
}
assertEquals(A, C); // pass
// Filter
C = B.stream()
.filter(x -> isDigit(x.charAt(0)))
.collect(toList());
assertEquals(A, C); // pass
}
}
Reduce
Reduce is used when you want a single result from collection.
/**
* Stream reduce ...
*
* Wwhen you want a single result from collection.
* Calculate the sum (example)
*
* Imperative - using for loop
* Declarative - acc is the "accumulator" (current sum)
*/
package com.minte9.lambdas.common_operations;
import static org.junit.Assert.assertEquals;
import java.util.stream.Stream;
import static java.util.Arrays.asList;
public class Reduce {
public static void main(String[] args) {
int sum = 0;
for(Integer i : asList(1, 2, 3)) {
sum += i;
}
assertEquals(6, sum);
int sum2 = Stream.of(1, 2, 3)
.reduce(0, (acc, x) -> acc + x);
assertEquals(6, sum2);
}
}
Comparing
Comparing to find minimum or maximum of an element.
/**
* Stream Comparator ...
* Comparing to find minimum or maximum of an element (example)
*/
package com.minte9.lambdas.common_operations;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Comparing {
public static void main(String[] args) {
List<Student> students = Arrays.asList(
new Student("John", 23),
new Student("Mary", 30),
new Student("Mike", 27)
);
Student youngest = students.stream()
.min(Comparator.comparing(x -> x.getAge()))
.get();
assertEquals(students.get(0), youngest); // pass
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public int getAge() { return age; }
}
PartitioningBy
Partitioning a Stream into two collections of values.
/**
* Stream partitionBy ...
*
* Partitioning a Stream into two collections of values (example)
*/
package com.minte9.lambdas.common_operations;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class PartitioningBy {
public static void main(String[] args) {
Map<Boolean, List<Integer>> map;
map = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.collect(Collectors.partitioningBy(
x -> x > 3
))
;
System.out.println(map.get(true)); // [4, 5, 6, 7, 8, 9, 10]
System.out.println(map.get(false)); // [1, 2, 3]
}
}
GroupingBy
Groupingby to use whatever keys you want for spliting.
/**
* Stream groupingBy ...
*
* Groupingby to use whatever keys you want for spliting.
* Use first char as key (example)
*/
package com.minte9.lambdas.common_operations;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GroupingBy {
public static void main(String[] args) {
Map<Object, List<String>> map;
map = Stream.of("ab", "bc", "ac", "bd")
.collect(Collectors.groupingBy(
x -> x.charAt(0)
))
;
System.out.println(map); // {a=[ab, ac], b=[bc, bd]}
System.out.println(map.get('a')); // [ab, ac]
System.out.println(map.get('b')); // [bc, bd]
}
}
Last update: 451 days ago