Last active
August 23, 2017 08:25
-
-
Save aya-eiya/b423e04ba9c7d6e27a4d3ff99fb93141 to your computer and use it in GitHub Desktop.
初心者向けJava8ラムダ演習1 解答例 ref: http://qiita.com/aya_eiya/items/99ec41b1a49549017bd0
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
import java.util.stream.Stream; | |
public class Practice1ASol { | |
static public void main(String[] args) { | |
Stream.of("Hello", " ", "World", "!").forEach(System.out::print); | |
} | |
} |
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
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); | |
}); | |
} | |
} |
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
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."); | |
} | |
} | |
} |
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
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