Skip to content

Instantly share code, notes, and snippets.

@unclebean
Created April 15, 2018 14:20
Show Gist options
  • Save unclebean/753a04c0eee400bf79b92e54129df05c to your computer and use it in GitHub Desktop.
Save unclebean/753a04c0eee400bf79b92e54129df05c to your computer and use it in GitHub Desktop.
java features...
java 8
Lambdas
List<Person> people = loadPeolple();
people.sort((p1, p2) -> p1.name.compareTo(p2.name));
// no need final
public UnaryOperator<String> upperCaser(Locale locale) {
return str -> str.toUpperCace(locale);
}
private int doFoo() {
return doFooBar(lambdaOfFooSpecificLogic);
}
private int doFooBar(Function<A, B> fn) {
result = fn.apply(arg);
}
Runnable
Comparable
Callable
Function<T, R>
Predicate<T>
Supplier<T>
Consumer<T>
@FunctionalInterface
public interface FooBarQuery{
public abstract Foo findAllFoos(Bar bar);
}
// avoid overloading
public class Foo<T> {
public Foo<R> apply(Function<T, R> fn);
public Foo<T> apply(UnaryOperator<T> fn);
}
// put interface last
public Foo parse(Locale locale, Function<Locale, Foo> fn);
// lambda handling exception
public Function<String, Class> loader() {
return Unchecked.function(className -> Class.forName(className));
}
Unchecked.wrap(()->{
// any code may throw exception
});
//OpenGamma
TestHelper.assertThrows(IllegalArgumentException.class, ()->new FooBar(""));
//Optional
public Optional<Foo> findFoo(String key) {}
Foo foo = findFoo(key).orElse(Foo.DEFAULT);
Foo foo = findFoo(key).orElseThrow(RuntimeException::new);
//Not serializable
//Not ideal to be an instance variable
//http://blog.joda.org/2015/08/java-se-8-optional-pragmatic-approach.html
//Streams
List<Trade> trades = loadTrades();
List<Money> valued = trades.stream().filter(t -> t.isActive()).map(t -> presentValue(t)).collect(Collectors.toList());
// Collector interface ?
// Interfaces
// Default methods
// Static methods
// Extend interfaces without breaking compatibility
// Cannot default equals/hashCode/toString
//www.joda.org/joda-beans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment