@@ -0,0 +1,3430 @@
+package test.io.dindinw.lang;%0A%0Aimport java.util.Arrays;%0Aimport java.util.concurrent.TimeUnit;%0Aimport java.util.stream.IntStream;%0A%0Aimport org.junit.Ignore;%0Aimport org.junit.Test;%0A%0Aimport static org.junit.Assert.assertFalse;%0A%0A/**%0A * Unit Test for Java 8 stream%0A * %3Cp/%3E%0A * Ref :%0A * http://www.drdobbs.com/jvm/lambdas-and-streams-in-java-8-libraries/240166818?pgno=1%0A */%0Apublic class StreamTest %7B%0A%0A    @Ignore(%22Time consuming for demonstrate wrong use%22)%0A    @Test%0A    public void testOnClose() %7B%0A        try (IntStream s = Arrays.stream(new int%5B%5D%7B1, 2, 3%7D)) %7B%0A            s.onClose(() -%3E System.out.println(%22I am first.%22))%0A                    .onClose(() -%3E System.out.println(%22I am second%22))%0A                    .onClose(() -%3E evilThings())%0A                    .onClose(StreamTest::evilThings);%0A        %7D%0A    %7D%0A    // when a lambda is use to call a existed method. we can use method reference instead of lambda%0A    // 4 kinds of method refs :%0A    // Kind                                                                         Example%0A    // ----------------------------------                                           ------------------------------------%0A    // Reference to a static method                                                 ContainingClass::staticMethodName%0A    // Reference to an instance method of a particular object                       containingObject::instanceMethodName%0A    // Reference to an instance method of an arbitrary object of a particular type  ContainingType::methodName%0A    // Reference to a constructor                                                   ClassName::new%0A%0A    /**%0A     * If we set a onClose() Runnable never(almost) stopped%0A     * The try-close is vulnerable to run%0A     */%0A    private static void evilThings() %7B%0A        try %7B%0A            TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);%0A        %7D catch (InterruptedException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        System.out.println(%22I am evil.%22);%0A    %7D%0A%0A    @Test%0A    public void testParallel() %7B%0A        final int%5B%5D intArr = new int%5B%5D%7B0, 1, 2, 3, 4, 5, 6, 7, 8, 9%7D;%0A        convertArray(intArr);%0A        int%5B%5D array2 = new int%5B20%5D;%0A        Arrays.fill(array2,0);%0A        convertArray(array2);%0A    %7D%0A%0A    private void convertArray(int%5B%5D intArr) %7B%0A        try (IntStream s = Arrays.stream(intArr)) %7B%0A            assertFalse(s.isParallel());%0A            final long startSeq = System.currentTimeMillis();%0A            s.mapToObj(StreamTest::timeConsume).toArray();%0A            printDuration(s.isParallel(),intArr.length,System.currentTimeMillis()-startSeq);%0A        %7D%0A        try (IntStream s = Arrays.stream(intArr)) %7B%0A            s.parallel(); //use parallel for a time-consumed task, if the task can be executed in parallel%0A            final long startPal = System.currentTimeMillis();%0A            s.mapToObj(StreamTest::timeConsume).toArray();%0A            printDuration(s.isParallel(),intArr.length,System.currentTimeMillis()-startPal);%0A        %7D%0A    %7D%0A%0A%0A    private static void printDuration(boolean parallel,int size,long duration) %7B%0A        System.out.printf(%22Parallel=%25s%5CtSize=%25s %5CtDuration: %25s ms  %5Cn%22,parallel,size,duration);%0A    %7D%0A%0A    private static String timeConsume(int i) %7B%0A        String convertTo = Integer.toBinaryString(i);%0A        try %7B%0A            TimeUnit.MILLISECONDS.sleep(100);%0A        %7D catch (InterruptedException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return convertTo;%0A    %7D%0A%7D%0A

test for Java 8 stream API