r/learnjava • • Mar 30 '20

Java Lambda expressions

I am a little lost as to what they are, how they work or what I can use them for.

I have no real example, just stumbled across the term and would like to understand it.

Are there understandable easy examples?

Thanks for possible help.

Edit: I found a pretty good explanation here. I just needed a easy and small example to wrap my head around this. Thanks to everyone who tried to help.

41 Upvotes

7 comments sorted by

View all comments

0

u/RexStardust Mar 30 '20

Here's an example from a little side project:

List <Transaction> returnDetailList = transactionArray.stream() .filter(x -> (x.getStatustext().equals(transactionType))) .filter(x -> (x.getTransactionCompleteTime().toLocalDate().isAfter(transactionRange.getAfterStartDate()))) .filter(x -> (x.getTransactionCompleteTime().toLocalDate().isBefore(transactionRange.getBeforeEndDate()))) .collect(Collectors.toList());

This takes an array of Transaction objects (Blue Prism robotics), filters by transaction type (completed or exception), then further filters to select transactions in the given date range. The output of the stream is a List of Transaction objects.

3

u/BananaPunsch Mar 30 '20

Thanks. I think I found a site that explains Lambdas with some easy examples. I appreciate the effort, I was just searching for an easy explantation so I could understand why I would use a Lambda instead of just implementing the full interface. By using Lambdas it seems like I don't have to implement a interface in the normal way. I basically use the interface to provide a head for a method and use the Lambda to complete the method. I just needed a easy visual of picture how everything is connected.