forked from winterbe/java8-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request winterbe#16 from grijeshsaini/master
BiConsumer Example
- Loading branch information
Showing
1 changed file
with
32 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.winterbe.java8.samples.lambda; | ||
|
||
import java.util.HashMap; | ||
import java.util.function.BiConsumer; | ||
|
||
/** | ||
* Created by grijesh | ||
*/ | ||
public class Lambda5 { | ||
|
||
//Pre-Defined Functional Interfaces | ||
public static void main(String... args) { | ||
|
||
//BiConsumer Example | ||
BiConsumer<String,Integer> printKeyAndValue | ||
= (key,value) -> System.out.println(key+"-"+value); | ||
|
||
printKeyAndValue.accept("One",1); | ||
printKeyAndValue.accept("Two",2); | ||
|
||
System.out.println("##################"); | ||
|
||
//Java Hash-Map foreach supports BiConsumer | ||
HashMap<String, Integer> dummyValues = new HashMap<>(); | ||
dummyValues.put("One", 1); | ||
dummyValues.put("Two", 2); | ||
dummyValues.put("Three", 3); | ||
|
||
dummyValues.forEach((key,value) -> System.out.println(key+"-"+value)); | ||
|
||
} | ||
} |