Commits

Micha Kops committed 6769d34

Other examples for aggregates, sorting, parralel execution added.

  • Participants
  • Parent commits 2ebb2a1

Comments (0)

Files changed (1)

src/com/hascode/tutorial/Main.java

         public int getAge(){
             return this.age;
         }
+        
+        public void setAge(int age){
+            this.age=age;
+        }
     }
     
-    public static void main(String[] args) {
+    public static void main(String[] args) throws InterruptedException {
         Predicate<Person> olderThanFifty = p -> p.getAge() > 50;
         Predicate<Person> teenager = p -> p.getAge() < 20 && p.getAge() >= 10;
         
         
         System.out.println("--- thread/runnable example ---");
         new Thread(() -> System.out.println("I run")).start();
+        
+        Thread.sleep(2000);
+        System.out.println("--- parallel/aggregates example ---");
+        long amountTeens = persons.parallelStream().filter(teenager).count();
+        System.out.println(amountTeens+" teens found");
+        
+        int min = persons.stream().min((pers1,pers2) -> Integer.compare(pers1.getAge(), pers2.getAge())).get().getAge();
+        int max = persons.stream().max((pers1,pers2) -> Integer.compare(pers1.getAge(), pers2.getAge())).get().getAge();
+        System.out.println("min age: "+min+", max age: "+max);
+        
+        System.out.println("--- sorting example ---");
+        persons.stream().sorted((pers1,pers2) -> pers1.getName().compareTo(pers2.getName())).forEach(p -> System.out.println("name: "+p.getName()+", age: "+p.getAge()));
     }
     
 }