Skip to content

Instantly share code, notes, and snippets.

@aya-eiya
Last active August 23, 2017 08:25
Show Gist options
  • Save aya-eiya/b423e04ba9c7d6e27a4d3ff99fb93141 to your computer and use it in GitHub Desktop.
Save aya-eiya/b423e04ba9c7d6e27a4d3ff99fb93141 to your computer and use it in GitHub Desktop.
初心者向けJava8ラムダ演習1 解答例 ref: http://qiita.com/aya_eiya/items/99ec41b1a49549017bd0
import java.util.stream.Stream;
public class Practice1ASol {
static public void main(String[] args) {
Stream.of("Hello", " ", "World", "!").forEach(System.out::print);
}
}
import java.util.List;
import java.util.ArrayList;
public class Practice1Sol {
static public void main(String[] args) {
List<String> helloWorld = new ArrayList<String>(3);
helloWorld.add("Hello");
helloWorld.add(" ");
helloWorld.add("World");
helloWorld.add("!");
helloWorld.stream().forEach(s -> {
System.out.print(s);
});
}
}
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
public class Practice2ASol {
static private final int MAX = 1024;
static public void main(String[] args) {
String sourceText = "this is an test script to read.";
List<Integer> sumBox = new ArrayList<>(sourceText.length());
sourceText
.chars()
.mapToObj(c->{
sumBox.add(sumBox.size() == 0 ? c : c + sumBox.get(sumBox.size() - 1) );
return new AbstractMap.SimpleEntry<>(sumBox.get( sumBox.size()-1),c);
})
.filter(ent->ent.getKey()<=MAX)
.mapToInt(Entry::getValue)
.map(c -> c > 'm' ? Character.toUpperCase(c) : c)
.forEach(c -> System.out.print((char) c));
System.out.print("\n");
if (sumBox.get(sumBox.size() - 1) > MAX) {
System.out.println("script stopped for MAX size limitation.");
}
}
}
public class Practice2Sol {
static public void main(String[] args) {
String sourceText = "this is an test script to read.";
sourceText
.chars()
.limit(sourceText.indexOf('.'))
.map(c -> c > 'm' ? Character.toUpperCase(c) : c)
.forEach(c -> System.out.print((char) c));
System.out.print(".\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment