lambda expression java

A lambda expression is a short block 
of code which takes in parameters
and returns a value. Lambda expressions
are similar to methods, but they do
not need a name and they can be
implemented right in the body of a method.

parameter -> expression

To use more than one parameter, wrap them in parentheses:

(parameter1, parameter2) -> expression

Example
Use a lamba expression in the ArrayList's 
forEach() method to print every item in the list:

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    numbers.add(5);
    numbers.add(9);
    numbers.add(8);
    numbers.add(1);
    numbers.forEach( (n) -> { System.out.println(n); } );
  }
}

4.2
5
Rosey28 110 points

                                    public class TestLambda {

   public static void main(String args[]) {
      List&lt;String&gt; lowerCaseStringsList = Arrays.asList(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;);
     // the :: notation is the lambda expression
     // it's the same as the anonymous function s -&gt; s.toUpperCase()
      List&lt;String&gt; upperCaseStringsList = lowerCaseStringsList.stream().
        map(String::toUpperCase).
        collect(Collectors.toList());
   }   
}

4.2 (5 Votes)
0
0
6
CompuChip 115 points

                                    StateOwner stateOwner = new StateOwner();

stateOwner.addStateListener(
    (oldState, newState) -&gt; System.out.println(&quot;State changed&quot;)
);

0
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source