Jan Amoyo

on software development and possibly other things

Showing posts with label stream. Show all posts
Showing posts with label stream. Show all posts

Examples of Streams and Lamdas in Java 8

Below is a code showcasing Streams and Lamdas in Java 8 (written as a JUnit test allow execution of individual methods).
@RunWith(BlockJUnit4ClassRunner.class)
public class Java8Showcase {

  @FunctionalInterface
  public static interface Concat {
    String concat(String str1, String str2);
  }

  @Test
  public void functional_interfaces() {
    // Traditional way of declaring an
    // interface implementation
    @SuppressWarnings("unused")
    Concat _concat = new Concat() {
      @Override
      public String concat(String str1, String str2) {
        return str1.concat(str2);
      }
    };

    Concat contact = (str1, str2) -> str1.concat(str2);
    System.out.println(contact.concat("hello", " world"));
  }

  @Test
  public void function_reference() {
    List<Integer> nums = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    // nums.stream().reduce((a, b) -> Math.max(a, b))
    Integer max = nums.stream().reduce(Math::max).get();

    System.out.println(max);
  }

  @Test
  public void streams_filter() {
    List<Integer> nums = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    List<Integer> evens = nums.stream().filter((n) -> n % 2 == 0)
      .collect(Collectors.toList());

    System.out.println(evens);
  }

  @Test
  public void streams_map() {
    List<Integer> nums = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    List<Integer> x2 = nums.stream().map((n) -> n * 2)
      .collect(Collectors.toList());

    System.out.println(x2);
  }

  @Test
  public void streams_reduce() {
    List<Integer> nums = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    Integer sum = nums.stream().reduce((a, b) -> a + b).get();

    System.out.println(sum);
  }

  @Test
  public void streams_fluent() {
    List<Integer> nums = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    Integer sum = nums.stream()
        .filter((n) -> n % 2 == 0) // filter the even numbers
        .map((n) -> n * 2)         // multiply each number by 2
        .reduce((a, b) -> a + b)   // add each number
        .get();

    System.out.println(sum);
  }

  @Test
  public void streams_parallel() {
    List<Integer> nums = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    Integer sum = nums.parallelStream().reduce((a, b) -> a + b).get();

    System.out.println(sum);
  }

  @Test
  public void streams_forEach() {
    List<Integer> nums = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    nums.stream().forEach(System.out::println);
  }

  @Test
  public void streams_collectors() {
    List<Integer> nums = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    Double ave = nums.stream().collect(Collectors.averagingInt((i) -> i));

    System.out.println(ave);
  }

  @Test
  public void streams_collectors_grouping1() {
    List<Integer> nums = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    Map<Boolean, List<Integer>> oddEven = nums.stream()
      .collect(Collectors.groupingBy((i) -> i % 2 == 0));

    System.out.println(oddEven);
  }

  @Test
  public void streams_collectors_grouping2() {
    List<Integer> nums = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    Map<Boolean, Integer> oddEvenSum = nums.stream()
      .collect(Collectors.groupingBy((i) -> i % 2 == 0, Collectors.summingInt((Integer i) -> i)));

    System.out.println(oddEvenSum);
  }
}