Skip to content

Instantly share code, notes, and snippets.

@DaHoC
Created July 10, 2014 09:59
Show Gist options
  • Save DaHoC/7a94d8213522e1cdb389 to your computer and use it in GitHub Desktop.
Save DaHoC/7a94d8213522e1cdb389 to your computer and use it in GitHub Desktop.
Java 8 playground
package de.janhendriks.java8test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
/**
* Used tutorials:
http://www.tutego.de/blog/javainsel/2012/12/einfhrung-in-java-8-lambda-ausdrcke-code-sind-daten/
http://www.lmis.de/java-8-die-wichtigsten-neuerungen-fur-entwickler
http://howtodoinjava.com/2014/04/04/how-to-use-predicate-in-java-8/
http://www.tutego.de/blog/javainsel/2014/03/funktionale-schnittstelle-aus-dem-java-util-function-paket/
http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
http://winterbe.com/posts/2014/03/16/java-8-tutorial/
http://javarevisited.blogspot.de/2014/02/10-example-of-lambda-expressions-in-java8.html
http://technology.amis.nl/2013/10/03/java-8-the-road-to-lambda-expressions/
*/
public class Java8Tests {
final static List<String> someStrings = Arrays.asList("one", "two", "three");
protected static String testAccess = "asdas";
final static double pi = 3.14_1592_6535;
/**
* java.util.function Predicate test - used e.g. to filter from a collection
* This example from http://howtodoinjava.com/2014/04/04/how-to-use-predicate-in-java-8/
* @return
*/
public static Predicate<String> matchesFilterCrit() {
return someTestStrings -> someTestStrings.equalsIgnoreCase("filterCrit");
}
public static void main(String [] args) {
// Foreach test
System.out.println("Foreach test");
someStrings.forEach(new Consumer<String>() {
@Override
public void accept(String element) {
System.out.println(element);
}
});
// Lambda-Expression test
System.out.println("Lambda-Expression test");
someStrings.forEach((String element) -> System.out.println(element));
// java.util.function Predicate test - used e.g. to filter from a collection
System.out.println("java.util.function Predicate test");
final Predicate<String> filterPred = new Predicate<String>() {
@Override
public boolean test(String test) {
return test.equalsIgnoreCase("one");
}
};
List<String> filteredResult = someStrings.stream().filter(filterPred).collect(Collectors.<String>toList());
filteredResult.forEach((String element) -> System.out.println(element));
/*
// Example from http://www.tutego.de/blog/javainsel/2014/03/funktionale-schnittstelle-aus-dem-java-util-function-paket/ does not work?!
Predicate<String> matchesFilterCrit() = someStrings -> someStrings.equalsIgnoreCase("filterCrit");
*/
// java.util.function Supplier test
System.out.println("java.util.function Supplier test");
final Supplier<String> supplier = new Supplier<String>() {
@Override
public String get() {
return "supplies!";
}
};
/// TODO How to use this properly?
System.out.println(supplier.get());
// Thread with lambda expression in Java8
new Thread( () -> System.out.println("In Java8, Lambda expressions in threads rock!") ).start();
// Java8 java.util.function Function - just a placeholder for ANY function (cool :-)
System.out.println("java.util.function Function test");
ArrayList<String> someStringsWith4 = new ArrayList<String>(someStrings);
Function<ArrayList<String>, List<String>> stringMorpher = test -> {
test.add("four");
return test;
};
System.out.println("stringMorpher added four: " + stringMorpher.apply(someStringsWith4).toString());
Function <Integer, Integer> inte = inInt -> {return inInt+3;};
inte.apply(new Integer(5));
System.out.println("Function: " + testFun( inInt -> inInt+3, 5));
System.out.println("Gen Function: " + genFun( inInt -> (Integer)inInt+3, 5));
genFun(new UnaryOperator() {
@Override
public Object apply(Object t) {
return null;
}
}, new Object());
System.out.println("Gen Function toString: " + genFun(inObj -> inObj.toString(), new Object()));
}
/**
* WTFunk is does this function doooo? :-)
* @param inF
* @param obj
* @return Object
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Object genFun(Function inF, Object obj) {
return inF.apply(obj);
}
/**
* Führt die eingegebene Funktion Int->Int auf dem 2.Parameter Int aus
* @param inF Auszuführende Funktion
* @param inInt Eingabeargument der übergebenen, auszuführenden Funktion
* @return Ausgabe der übergebenen, auszuführenden Funktion
*/
private static Integer testFun(Function <Integer, Integer> inF, Integer inInt) {
return inF.apply(inInt);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment