Skip to content

Commit

Permalink
play with method references and lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
hochraldo committed May 13, 2014
1 parent 84b16e6 commit 1c47a3e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.class
*.iml

# Mobile Tools for Java (J2ME)
.mtj.tmp/
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/hochraldo/LambaMethodReference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package hochraldo;


import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class LambaMethodReference {


public static void main(String... args) {

// static method reference
Predicate<Object> nullCheck = Objects::nonNull;
System.out.println(nullCheck.test(null));
System.out.println(nullCheck.test(new Object()));

// constructor reference
Supplier<List<Integer>> listSupplier = ArrayList::new;
List<Integer> list1 = listSupplier.get();
list1.add(1);
System.out.println(list1);
List<Integer> list2 = listSupplier.get();
list2.add(2);
list2.add(3);
System.out.println(list2);

// method reference
String s = "Foo";
Function<Integer, Character> charAt = s::charAt;
System.out.println(charAt.apply(0));
}
}

0 comments on commit 1c47a3e

Please sign in to comment.