Commits

Micha Kops  committed a35309a

Initial import.

  • Participants

Comments (0)

Files changed (2)

+build
+build.xml
+nbproject
+manifest.mf

File src/com/hascode/tutorial/Main.java

+package com.hascode.tutorial;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+public class Main {
+    static class Person {
+        String name;
+        int age;
+        
+        public Person(String name, int age){
+            this.name=name;
+            this.age=age;
+        }
+        
+        public String getName(){
+            return this.name;
+        }
+        
+        public int getAge(){
+            return this.age;
+        }
+    }
+    
+    public static void main(String[] args) {
+        Predicate<Person> olderThanFifty = p -> p.getAge() > 50;
+        Predicate<Person> teenager = p -> p.getAge() < 20 && p.getAge() >= 10;
+        
+        Person p1 = new Person("Tim", 13);
+        Person p2 = new Person("Lisa", 52);
+        Person p3 = new Person("Bart", 9);
+        Person p4 = new Person("Mitch", 14);
+        Person p5 = new Person("Beth", 64);
+        
+        List<Person> persons = new ArrayList<>();
+        persons.add(p1);
+        persons.add(p2);
+        persons.add(p3);
+        persons.add(p4);
+        persons.add(p5);
+        
+        System.out.println("--- experienced ---");
+        persons.stream().filter(olderThanFifty).forEach(p -> System.out.println(p.getName()));
+        
+        System.out.println("--- teenagers ---");
+        List<Person> teens = persons.stream().filter(teenager).collect(Collectors.<Person>toList());
+        teens.forEach(t -> System.out.println(t.getName()));
+        
+        List<String> namesBeginningWithLetterB = persons.stream().filter(p -> p.getName().startsWith("B")).map(p -> p.getName()).collect(Collectors.<String>toList());
+        System.out.println("--- names beginning with 'B' ---");
+        namesBeginningWithLetterB.forEach(name -> {
+            System.out.println(name);
+        });
+    }
+    
+}