-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* lambda and update .gitignore * add package * refactor package Co-authored-by: bhimsur <bhimantoro.admodjo@ist.id>
- Loading branch information
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
.vscode | ||
*.log | ||
*.sh | ||
out/ | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package learn.intermediate.JavaLambdaExpressions; | ||
|
||
public class Lambda { | ||
// Membuat interface | ||
interface PrintNumbers{ | ||
void print(Integer number); | ||
} | ||
public static void main(String[] args) { | ||
// pada ekspresi berikut nilai n akan melakukan perulangan sampai batasnya yaitu 20 | ||
// dimana n merupakan parameter dan perulangan merupakan fungsi yang akan dilakukan | ||
PrintNumbers p = n -> { | ||
while(n <= 20) { | ||
System.out.println(n++); | ||
} | ||
}; | ||
// akan mencetak angka 1 sampai dengan 20 | ||
p.print(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Java Lambda Expressions | ||
|
||
_Lambda Expressions_ atau biasa dikenal _Lambda Functions_ adalah blok kode yang memiliki parameter dan mengembalikan suatu nilai. _Lambda Function_ sama seperti _method_ atau _function_ , namun tidak perlu mendefinisikannya secara eksplisit dan bisa langsung implementasi didalam _body_ sebuah _method_ | ||
|
||
Implementasi simpel dari _Lambda Expression_ memiliki satu parameter dan _expression_ | ||
|
||
|
||
> *parameter* -> *expression* | ||
Untuk menggunakan lebih dari satu parameter bisa meletakkan parameter tersebut kedalam tanda kurung | ||
|
||
> *(parameter1, parameter2)* -> *expression* | ||
Namun, jika ingin melakukan sebuah operasi kondisional bisa menggunakan tanda kurung kurawal untuk membungkus blok kode tersebut | ||
|
||
> *(parameter1, parameter2)* -> { *blok kode* } | ||
Contoh simpel penggunaan _Lambda Expressions_ | ||
|
||
```java | ||
public class Lambda { | ||
interface PrintNumbers{ | ||
void print(Integer number); | ||
} | ||
public static void main(String[] args) { | ||
PrintNumbers p = n -> { | ||
while(n <= 20) { | ||
System.out.println(n++); | ||
} | ||
}; | ||
p.print(1); | ||
} | ||
} | ||
``` |