java8: language enhancements
Post on 15-Jul-2015
89 Views
Preview:
TRANSCRIPT
Lambda - ?
Expression describing the
anonymous function, the result of the
execution of which is the object of
unknown origin, which implements the
required functional interface
Lambda. Examples
Java < 8Comparator<Integer> cmp = new Comparator<Integer>() { @Override public int compare(Integer x, Integer y) { return (x < y) ? -1 : (x > y) ? 1 : 0; }};
Java 8Comparator<Integer> cmp = (x, y) -> (x < y) ? -1 : (x > y) ? 1 : 0;
Lambda. More examples
//interface Function<T, R> - R apply(T t);Function<String, Integer> f0 = (String s) -> Integer.parseInt(s);
//interface Supplier <T> - T get();Supplier<Integer> answerFactory = () -> 42;
// interface Supplier<T> - T get();Supplier<Integer> deepThought = () -> { long millis = TimeUnit.DAYS.toMillis(2737500000L); Thread.sleep(toThinkInMillis); return 42;};
Method references. Precondition
public class Member { public static int compareByAge(Member a, Member b) { return a.getBDay().compareTo(b.getBDay()); }}
Member[] rosterAsArray = /* init array of Members */;
Method references. Java < 8
class MemberAgeComparator implements Comparator<Member> { public int compare(Member a, Member b) { return Member.compareByAge(a, b); }}
Arrays.sort(rosterAsArray, new MemberAgeComparator());
Method references. Java 8 with Lambda
Arrays.sort(rosterAsArray, (a, b) -> Member.compareByAge(a, b));
Method References. Types
Type Example
Reference to a static method ContainingClass::staticMethodNameReference to an instance method of a particular object
ContainingObject::instanceMethodNamememberInstance::compareByName;(memberInstance.compareByName(a, b))
Reference to an instance method of an arbitrary object of a particular type
ContainingType::methodNameString::compareToIgnoreCase(a.compareToIgnoreCase(b))
Reference to a constructor ClassName::newHashSet::new(() -> { return new HashSet<>(); })
Default methods. The problem
interface Collection<T> {/* @since 1.8 */void removeAll(Predicate<T> p);
}
Default methods. Solution
interface Collection<T> {/* @since 1.8 */default void removeAll(Predicate<T> p) {
// fallback implementation goes here}
}
Static methods
public interface Ticket { String qDublin (); static Ticket random () { return () -> " toDublin "; } }
assertEquals ("toDublin", Ticket.random().qDublin ());
Streams. Problempublic void printGroups(List<People> people) { Set<Group> groups = new HashSet<>(); for (Person p : people) { if (p.getAge() >= 65) groups.add(p.getGroup()); }
List<Group> sorted = new ArrayList <>(groups); Collections.sort(sorted, new Comparator <Group>() { public int compare (Group a, Group b) { return Integer.compare(a.getSize(), b.getSize()); } });
for (Group g : sorted) System.out.println(g.getName());}
Streams. Solution
public void printGroups (List<People> people) { people.stream() .filter(p -> p.getAge() > 65) .map(p -> p.getGroup()) .distinct() .sorted(comparing(g -> g.getSize()) .map(g -> g.getName()) .forEach(n -> System.out.println(n));}
Stream → sequence of elements.
Streams. Design
source op op … sink→ → → →
sources: collections, iterators, channels, ... operations: filter, map, reduce, ... sinks: collectors, forEach, iterator...
Streams. Parallelism
int v = list.parallelStream() .reduce(Math::max) .get ();
Implicit invocation of parallelStream() instead of stream()
Implementation is hidden Uses ForkJoinPool
top related