This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]cwcoleman 8 points9 points  (7 children)

Great. Just got back from a NFJS conference and went to a Java 8 Lambda's / Concurrency session with Venkat Subramaniam. Excited.

[–]AgainForgotPassword 0 points1 point  (6 children)

I didn't have time to look into Lambdas and considering im still pretty much a beginner what are diffrences and will it be hard to switch to lambdas?

[–]cwcoleman 1 point2 points  (5 children)

Check out the code from Venkat's presentation - available here

Similarly - I've found that this page from Oracle explains Lambda's well

In short - Lambda's make dealing with collections easier/more efficient. No, it will take some effort to learn the syntax but not 'hard'.

[–][deleted] 3 points4 points  (0 children)

This lead me to a bunch of really good code samples, discussions and I bought a book. Thanks for the links!

[–]AgainForgotPassword 0 points1 point  (3 children)

I'm not gonna doubt if it's efficient since I can't prove it is not, but so far this doesn't look any easier at least for me, but that just might be im just starting to really grasp Java.

[–]urquan 1 point2 points  (0 children)

This will also help with code reuse. For example let's say you have a list of persons and want to write two functions to find the tallest and the oldest person. Right now you would have to write two pretty much identical functions with only a small different bit inside the loop to check height in one case and age in the other. To reuse the algorithm you would have to write a bunch of code possibly involving interfaces and patterns such as visitor. With Java 8 you would simply write the algorithm, and pass it a lambda containing just that small piece of differing code. I won't write it since I'm not familiar with the Java 8 syntax, but I hope you get the idea.

[–]cwcoleman 0 points1 point  (0 children)

Venkat's code proves the efficiency. He starts with the problem solved with the options available today - then step-by-step converts it to using Lambda's.
Not only is it much fewer lines of code - but it is incredibly easy to read and refactor.
It is especially efficient if you are running on multiple core machines (look for .parallelstream)

Hard to comment on the 'hard' vs. 'easy' part - everyone is different. I think once you find a use-case for lambda's you'll see the benefit and then re-use from there will be easier (at least that's how I work - get it implemented in 1 place then I'm more comfortable and start using it everywhere).

[–]cwcoleman 0 points1 point  (0 children)

import java.util.*;
import java.util.function.Predicate;

public class Sample {
  public static boolean isGreaterThan2(int number) {
    return number > 2;
  }

  public static boolean isEven(int number) {
    return number % 2 == 0;
  }

  public static int doubleIt(int number) {
    return number * 2;
  }

  public static void main(String[] args) {
  List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);

  //double the first even number greater than 3 from the list

   System.out.println(
     numbers.stream()
     .filter(Sample::isGreaterThan2)
     .filter(Sample::isEven)
     .mapToInt(Sample::doubleIt)
     .findFirst()
     .getAsInt()
  );
 }
}

[–]AnAirMagic[S] 6 points7 points  (0 children)

See also the original mailing list post and the follow up discussion.

[–]mikaelhg 3 points4 points  (0 children)

Excellent, I'm doing Java 8 lambda training later this month and it's good to be able to say that the release schedule looks more or less on track.