PROGRAMMING

 
REMEMBERS




Collect

p33 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
    }
}
... 11 lines
 

Map

p34 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
    }
}
... 22 lines
 

Filter

p38 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
    }
}
... 27 lines
 

Reduce

p43 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);
    }
}

... 19 lines
 

Comparing

p41 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; }
}
... 27 lines
 

PartitioningBy

p89 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]
    }
}
... 17 lines
 

GroupingBy

p91 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]
    }
}
... 19 lines
 

Questions     github Lambdas
Default Methods

        A B C D E F
🔔
1/1