generated from nus-cs2030/base-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Lambda expression
notkqXD edited this page Oct 13, 2021
·
1 revision
- lambda functions are similar to methods
- The difference is that they can be created in the methods body and arguments and they don't need a name
- we can assign them as values
- pass them in as arguments
- return lambda from methods
- The simplest way to create a lambda expression which contain 1 parameter and an expression is
- parameter -> expression
eg. x -> x * x (will return you the square of the the argument) similar to
int square(int x) { return x * x; }
- If there is no parameter the syntax will be () -> expression
- If you have more than 1 parameter use parenthesis to contain them
(para1, para2) -> expression
- note within the parenthesis it can contain more than 2 parameter
- There are limitations to expressions
- They have to return a value immediately
- cannot create variables in it
- cannot have assignments
- no if else statements
- no for/while loops
To overcome this limitations we wrap the code using curly braces {}.
- (para1, para2) -> {code}
eg. (Integer i, Interger j) -> {return j-i}
- note within the code if the method return a value or object there must be a return statement
Peer Learning
Guides
Setting Up Checkstyle
Setting Up Java
Setting Up MacVim
Setting Up Stu
Setting Up Unix For Mac
Setting Up Unix For Windows
Setting Up Vim
Setting up SSH Config
SSH Without Password
Copying Files From PE Nodes
Using tmux
CS2030 Contents
Lecture 1 SummaryLecture 2 Summary
Access Modifiers
Lecture 3 Summary (Polymorphism)
Compile Time Type VS Runtime Type
Abstraction, Encapsulation, Inheritance, and Polymorphism
SOLID Principles
Class VS Abstract Class VS Interface
Comparable VS Comparator
Generic Types T
HashMap
Raw Types with Generic
Lambda expression
PECS (Producer Extends Consumer Super)
Optional
Streams
Parallel Streams
Monad
Functors and Monads with Category Theory