java/ch/wlkl/javaExamples/java8.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.wlkl.javaExamples;

import static ch.wlkl.env.Env.implement;
import java.util.Arrays;

/**
 *
 * @author walter
 */
public class java8 {

    public void stream0() {
        int[] ar = {3, 4, 5, 13, 18};
        System.out.println("begin " + methodLine());

        System.out.println("external iteration");
        for (int i = 0; i < ar.length; i++) {
            System.out.println("\t[" + i + "] " + ar[i]);
        }
    
        System.out.println("stream iteration");
        Arrays.stream(ar).forEach(i -> System.out.println("\t" + i));
    
        System.out.println("stream filter gerade");
        Arrays.stream(ar).filter(i -> i % 2 == 0 ).forEach(i -> System.out.println("\t" + i + "\t" + methodLine()));

        System.out.println("stream mapToObj gerade");
        Arrays.stream(ar).mapToObj(i -> i % 2 == 0 ).forEach(i -> System.out.println("\t" + i + "\t" + methodLine()));
    
        System.out.println("stream reduce    + " + Arrays.stream(ar).reduce((i, j) -> i+j ));
        System.out.println("stream reduce 1, * " + Arrays.stream(ar).reduce((i, j) -> i*j ));
        implement("partitionBy" /*
        Arrays.stream(ar).partitionBy(i -> ((Integer)i).intValue() % 3).forEach(i -> System.out.println(i));
        */);
        System.out.println("end   " + methodLine());
}

    public void all() {
        System.out.println("begin " + methodLine());
        stream0();
        System.out.println("end   " + methodLine());
    }

    public static String methodLine() {
        StackTraceElement ste = Thread.currentThread().getStackTrace()[2];
        return ste.getClassName() + "#" + ste.getMethodName() + ":" + ste.getLineNumber();
    }

    public static void main(String[] args) {
        System.out.println("begin " + methodLine());
        new java8().all();
        System.out.println("end   " + methodLine());
    }
}