Created
December 18, 2014 15:53
-
-
Save madhub/6c3574b43f684b9cd81a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package demo; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.function.BiConsumer; | |
import java.util.function.Consumer; | |
import java.util.function.Function; | |
import java.util.function.Predicate; | |
import java.util.function.Supplier; | |
import java.util.stream.Collectors; | |
/** | |
* | |
* @author madhub | |
*/ | |
public class FunctionalInterfaceWithAnonymousInnerClassLambdaAndMethodReference { | |
public static void functionalInterfaceWithAnonymousInnerClassLambdaAndMethodReference() { | |
final List<String> friends = Arrays.asList("Rachel Green", "Monica Geller", "Phoebe Buffay", | |
"Joey Tribbiani", " Chandler Bing", " Ross Geller "); | |
// transform the list to upper case | |
// using anonymous inner class | |
friends.stream().map( new Function<String, String>() { | |
@Override | |
public String apply(String t) { | |
return t.toUpperCase(); | |
} | |
}).forEach( new Consumer<String>() { | |
@Override | |
public void accept(String t) { | |
System.out.println(t); | |
} | |
}); | |
// using lambda expression | |
friends.stream().map( f -> f.toLowerCase()) | |
.forEach(f -> System.out.println(f)); | |
// using Method reference | |
friends.stream().map(String::toUpperCase) | |
.forEach(System.out::println); | |
// convert to string to upper case & join using comad | |
friends.stream().map(String::toUpperCase) | |
.collect(Collectors.joining()); | |
Runnable runnable = () -> System.out.println("Runnabe Called"); | |
Consumer<String> consumer = s -> System.out.println(s); | |
consumer.accept("Hello"); | |
BiConsumer<Integer,String> biconsumer = (i,s) -> System.out.println(i+s); | |
Supplier<String> supplier = () -> "Hello World"; | |
supplier.get(); | |
Function<String,Integer> getLen = s -> s.length(); | |
Predicate<String> pre = s -> s.startsWith("S"); | |
pre.test("Hello"); | |
} | |
public static void main(String[] args) { | |
functionalInterfaceWithAnonymousInnerClassLambdaAndMethodReference(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment