Commits

RapidPM committed 2053922

added chap 1.5

Comments (0)

Files changed (5)

src/main/java/org/rapidpm/demo/entwicklerpress/shortcut/java8_streams/chap_1_5/CHAP_1_5_1.java

+package org.rapidpm.demo.entwicklerpress.shortcut.java8_streams.chap_1_5;
+
+import java.util.stream.IntStream;
+
+/**
+ * User: Sven Ruppert
+ * Date: 03.12.13
+ * Time: 13:22
+ */
+public class CHAP_1_5_1 {
+    public static void main(String[] args) {
+        for(int i = 0; i<1_000_000; i++){
+            final boolean b = isPrime1(i) != isPrime2(i);
+            if(b) System.out.println("ungleiches Ergebnis =  " + i);
+        }
+    }
+    public static boolean isPrime1(int n) {
+        if (n <= 1) return false;
+        if (n == 2) return true;
+        return n >= 2 && IntStream
+                .rangeClosed(2, (int) (Math.sqrt(n)))  //for(int i=3;i*i<=n;i+=2)
+                .allMatch((d) -> n % d != 0);          //if(n%i==0) return false
+    }
+
+    public static boolean isPrime2(int n) {
+        if (n <= 1) return false;
+        if (n == 2) return true;
+        if (n % 2 == 0) return false;
+        for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2) {
+            if (n % i == 0) return false;
+        }
+        return true;
+    }
+
+}

src/main/java/org/rapidpm/demo/entwicklerpress/shortcut/java8_streams/chap_1_5/CHAP_1_5_2.java

+package org.rapidpm.demo.entwicklerpress.shortcut.java8_streams.chap_1_5;
+
+
+import java.util.List;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
+/**
+ * Created by Sven Ruppert on 18.11.13.
+ */
+public class CHAP_1_5_2 {
+
+
+    /**
+     *         final Stream<Long> fibStream = makeFibStream(10);
+     fibStream.forEachOrdered(System.out::println);
+
+     System.out.println("isPrime(7) = " + isPrime1(7));
+     System.out.println("isPrimeOld(7) = " + isPrime2(7));
+     * @param args
+     */
+
+    public static void main(String[] args) {
+
+
+
+        final Stream<Long> fibStream = makeFibStream(10);
+        fibStream.forEachOrdered(System.out::println);
+
+    }
+
+
+    public static Stream<Long> makeFibStream() {
+        return(Stream.generate(new FibonacciSupplier()));
+    }
+    public static Stream<Long> makeFibStream(int numFibs) {
+        return(makeFibStream().limit(numFibs));
+    }
+    public static List<Long> makeFibList(int numFibs) {
+        return(makeFibStream(numFibs).collect(Collectors.toList()));
+    }
+
+    public static class FibonacciSupplier implements Supplier<Long> {
+        private long previous = 0;
+        private long current = 1;
+        @Override
+        public Long get() {
+            long next = current + previous;
+            previous = current;
+            current = next;
+            return(previous);
+        }
+    }
+
+}

src/main/java/org/rapidpm/demo/entwicklerpress/shortcut/java8_streams/chap_1_5/CHAP_1_5_3.java

+package org.rapidpm.demo.entwicklerpress.shortcut.java8_streams.chap_1_5;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * User: Sven Ruppert
+ * Date: 03.12.13
+ * Time: 13:23
+ */
+public class CHAP_1_5_3 {
+    public static void main(String[] args) {
+
+        final List<List<Integer>> matrix = new ArrayList<>();
+
+        matrix.add(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
+        matrix.add(Arrays.asList(1,2,3,4,5,66,7,8,9)); //hier eine 66
+        matrix.add(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
+
+        matrix.forEach(System.out::println);
+
+        final Integer s = matrix.stream()
+                .map(l -> l.stream()
+                        .filter(v -> v.equals(66))
+                        .findFirst().orElse(null))
+                .filter(f -> f != null)
+                .findFirst().orElse(null);
+        System.out.println("s = " + s);
+
+        Integer result = null;
+        endPos:
+        for (final List<Integer> integers : matrix) {
+            for (final Integer integer : integers) {
+                if(integer.equals(66)){
+                    result = integer;
+                    break endPos;
+                }
+            }
+        }
+        System.out.println("result " + result);
+
+    }
+
+    //        final String s = matrix.stream()
+//                .map((l -> l.stream()
+//                        .filter(v -> {
+//                            System.out.println("v = " + v);
+//                            return v.equals(66);
+//                        })
+//                        .findFirst()
+//                        .map(m -> {
+//                            System.out.println("m = " + m);
+//                            return m.toString() + "XX";
+//                        }).orElse(null)
+//
+//
+//                ))
+//                .filter(f->f != null)
+//                .findFirst()
+//                .orElse("yy");
+}

src/main/java/org/rapidpm/demo/entwicklerpress/shortcut/java8_streams/unsorted_yet/Part07.java

-package org.rapidpm.demo.entwicklerpress.shortcut.java8_streams.unsorted_yet;
-
-
-import java.util.List;
-import java.util.function.Supplier;
-import java.util.stream.Collectors;
-import java.util.stream.IntStream;
-import java.util.stream.Stream;
-
-/**
- * Created by Sven Ruppert on 18.11.13.
- */
-public class Part07 {
-
-
-    public static void main(String[] args) {
-        final Stream<Long> fibStream = makeFibStream(10);
-        fibStream.forEachOrdered(System.out::println);
-
-        System.out.println("isPrime(7) = " + isPrime(7));
-
-    }
-
-    public static boolean isPrime(int n) {
-        return n >= 2 && IntStream.rangeClosed(2, (int) (Math.sqrt(n))).allMatch((d) -> n % d != 0);
-    }
-
-    public static Stream<Long> makeFibStream() {
-        return(Stream.generate(new FibonacciSupplier()));
-    }
-    public static Stream<Long> makeFibStream(int numFibs) {
-        return(makeFibStream().limit(numFibs));
-    }
-    public static List<Long> makeFibList(int numFibs) {
-        return(makeFibStream(numFibs).collect(Collectors.toList()));
-    }
-
-    public static class FibonacciSupplier implements Supplier<Long> {
-        private long previous = 0;
-        private long current = 1;
-        @Override
-        public Long get() {
-            long next = current + previous;
-            previous = current;
-            current = next;
-            return(previous);
-        }
-    }
-
-}

src/main/java/org/rapidpm/demo/entwicklerpress/shortcut/java8_streams/unsorted_yet/Part08.java

-package org.rapidpm.demo.entwicklerpress.shortcut.java8_streams.unsorted_yet;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import org.rapidpm.demo.entwicklerpress.shortcut.java8_streams.Pair;
-import org.rapidpm.demo.entwicklerpress.shortcut.java8_streams.PairListGenerator;
-
-/**
- * User: Sven Ruppert
- * Date: 22.11.13
- * Time: 16:26
- */
-public class Part08 {
-
-    public static void main(String[] args) {
-
-        final List<List<Integer>> matrix = new ArrayList<>();
-
-        matrix.add(Arrays.asList(1,2,3,4,5,6,7,8,9));
-        matrix.add(Arrays.asList(1,2,3,4,5,66,7,8,9)); //hier eine 66
-        matrix.add(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
-
-
-        matrix.forEach(System.out::println);
-
-
-//        final String s = matrix.stream()
-//                .map((l -> l.stream()
-//                        .filter(v -> {
-//                            System.out.println("v = " + v);
-//                            return v.equals(66);
-//                        })
-//                        .findFirst()
-//                        .map(m -> {
-//                            System.out.println("m = " + m);
-//                            return m.toString() + "XX";
-//                        }).orElse(null)
-//
-//
-//                ))
-//                .filter(f->f != null)
-//                .findFirst()
-//                .orElse("yy");
-
-        final Integer s = matrix.stream()
-                .map(l -> l.stream()
-                        .filter(v -> v.equals(66))
-                        .findFirst()
-                        .map(m -> m).orElse(null))
-                .filter(f->f != null)
-                .findFirst() .orElse(null);
-        System.out.println("s = " + s);
-
-
-        Integer result = null;
-        endPos:
-        for (final List<Integer> integers : matrix) {
-            for (final Integer integer : integers) {
-                if(integer.equals(66)){
-                    result = integer;
-                    break endPos;
-                }
-            }
-        }
-        System.out.println("result " + result);
-
-    }
-}