Difference between revisions of "Lambda Demos"
Jump to navigation
Jump to search
| Line 39: | Line 39: | ||
==compute== | ==compute== | ||
| + | <nowiki> String text = "abracadabra"; | ||
| + | // NOTE: we use a TreeMap<K,V> since it is sorted producing consistent output | ||
| + | Map<Character, Integer> mapCharacterToCount = new TreeMap<>(); | ||
| + | for( char ch : text.toCharArray() ) { | ||
| + | // https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#compute-K-java.util.function.BiFunction- | ||
| + | mapCharacterToCount.compute(ch, (Character character, Integer count)-> { | ||
| + | if( count != null ) { | ||
| + | //we have associated character before | ||
| + | return count+1; | ||
| + | } else { | ||
| + | //first encounter with character | ||
| + | return 1; | ||
| + | } | ||
| + | }); | ||
| + | } | ||
| + | for( Entry<Character, Integer> entry : mapCharacterToCount.entrySet() ) { | ||
| + | System.out.println(entry); | ||
| + | }</nowiki> | ||
| + | |||
{{CodeToImplement|LambdasMapComputeExample|main|lambda.demo.compute}} | {{CodeToImplement|LambdasMapComputeExample|main|lambda.demo.compute}} | ||
Revision as of 18:49, 28 February 2018
Contents
Motivation
JDK 8 added lambdas to alleviate the bulkiness of anonymous inner classes. We use lambdas heavily in 231.
Background
Code To Investigate
async,finish
launchApp(() -> {
System.out.println("start");
finish(() -> {
async(() -> {
sleepRandom(1_000);
System.out.println("-apples");
});
async(() -> {
sleepRandom(1_000);
System.out.println("--oranges");
});
async(() -> {
sleepRandom(1_000);
System.out.println("---bananas");
});
async(() -> {
sleepRandom(1_000);
System.out.println("----mangoes");
});
});
System.out.println("stop");
});
| class: | LambdasAsyncAndFinishExample.java | |
| methods: | main | |
| package: | lambda.demo.asyncfinish | |
| source folder: | student/src/main/java |
| class: | AnonymousInnerClassesAsyncAndFinishExample.java | |
| methods: | main | |
| package: | lambda.demo.asyncfinish | |
| source folder: | student/src/main/java |
| class: | NamedClassesAsyncAndFinishExample.java | |
| methods: | main | |
| package: | lambda.demo.asyncfinish | |
| source folder: | student/src/main/java |
compute
String text = "abracadabra";
// NOTE: we use a TreeMap<K,V> since it is sorted producing consistent output
Map<Character, Integer> mapCharacterToCount = new TreeMap<>();
for( char ch : text.toCharArray() ) {
// https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#compute-K-java.util.function.BiFunction-
mapCharacterToCount.compute(ch, (Character character, Integer count)-> {
if( count != null ) {
//we have associated character before
return count+1;
} else {
//first encounter with character
return 1;
}
});
}
for( Entry<Character, Integer> entry : mapCharacterToCount.entrySet() ) {
System.out.println(entry);
}
| class: | LambdasMapComputeExample.java | |
| methods: | main | |
| package: | lambda.demo.compute | |
| source folder: | student/src/main/java |
| class: | AnonymousInnerClassesMapComputeExample.java | |
| methods: | main | |
| package: | lambda.demo.compute | |
| source folder: | student/src/main/java |
| class: | NamedClassesMapComputeExample.java | |
| methods: | main | |
| package: | lambda.demo.compute | |
| source folder: | student/src/main/java |
user interface
| class: | FxLambdaDemoApp.java | VIZ |
| package: | lambda.demo.viz | |
| source folder: | student/src//java |