Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on

Java 8 features
● Lambda
● Interface improvement
● Streams
● new packages

Published in: Software
  • Be the first to comment

  • Be the first to like this


  1. 1. first taste of Java 8 Sunil Kumar | Wiley London | 22-01-2014
  2. 2. Java 8 features ● Lambda ● Interface improvement ● Streams ● new packages Keywords : Big data, Parallelism, Cloud computing
  3. 3. 1. Lambda (anonymous function) In mathematics and computing generally, a lambda expression is a function: for some or all combinations of input values it specifies an output value. Lambda expressions in Java introduce the idea of functions into the language Ex: 0 param Runnable r = () -> System.out.println("hello world");; // prints "hello world" Object o = () -> System.out.println("hello world"); //won’t compile Ex: 1 param FileFilter filter = f -> f.getAbsolutePath().endsWith(".txt"); return filter.accept(new File("myfile.txt")); // returns true
  4. 4. Lambda - syntax Lambda expression has three part : parenthesized params, arrow and body ● (parameters) -> expression ● (parameters) -> {statements…} Type inference Comparator<String> c = (lhs, rhs) -> { System.out.println("comparing : " + lhs + " to " + rhs); return lhs.compareTo(rhs); }; int result =“hello”, “world”)
  5. 5. More examples List<Apple> inventory = Arrays.asList(new Apple(80,"green"), new Apple(155, "green"), new Apple(120, "red")); // [Apple{color='green', weight=80}, Apple{color='green', weight=155}] List<Apple> greenApples2 = filterApples(inventory, (Apple a) -> "green".equals(a.getColor())); System.out.println(greenApples2); // [Apple{color='green', weight=155}] List<Apple> heavyApples2 = filterApples(inventory, (Apple a) -> a.getWeight() > 150); System.out.println(heavyApples2); public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p){ List<Apple> result = new ArrayList<>(); for(Apple apple : inventory){ if(p.test(apple)){ result.add(apple); }} return result; }
  6. 6. Method References Method references are compact lambda expressions for methods that already have a name. ● String::valueOf x -> String.valueOf(x) ● Object::toString x -> x.toString() ● x::toString () -> x.toString() ● ArrayList::new () -> new ArrayList<>()
  7. 7. 2. Interface improvement (Functional interface) A functional interface is one whose type can be used for a method parameter when a lambda is to be supplied as the actual argument. For example, the forEach method on collections could have the following signature: public void forEach(Consumer<? super T> consumer); More precisely, a functional interface is defined as any interface that has exactly one explicitly declared abstract method. ● interface can now define static methods. ● interfaces can now provide default methods (The qualification is necessary because an interface may have non-abstract default methods.) This is why functional interfaces used to be called Single Abstract Method (SAM) interfaces, a term that is still sometimes seen.
  8. 8. 3. Streams A stream is a sequence of values. Allows functional-style operations on streams of values. Stream<T> stream =; Stream<T> stream = collection.parallelStream();
  9. 9. Steams type Operations Interface Used lambda signature return type return value Sample lazy/intermediate operations filter Predicate<T> T -> boolean Stream<T> stream containing input elements that satisfy the Predicate map Function<T, R> T -> R Stream<R> stream of values, the result of applying the Function to each input element sorted Comparator<T> (T, T) -> int Stream<T> stream containing the input elements, sorted by the Comparator Sample eagar/terminal operation reduce BinaryOperator<T> (T, T) -> T Optional<T> forEach Consumer<T> T -> void void
  10. 10. 4. java.util.function Function<T, R> - take T as input, return R as output Predicate<T> - take T as input, return a boolean as output Consumer<T> - take T as input, don't return anything Supplier<T> - take no input, return T BinaryOperator<T> - take two T's as input, return one T as output
  11. 11. Thank You. Q & A.
  12. 12. Reference: ● ● ● ● ● ●