Last active
November 17, 2017 12:22
-
-
Save nashcft/79e1bcd8b34bd43b4052a19d6daa1c58 to your computer and use it in GitHub Desktop.
『Javaによる関数型プログラミング - Java 8ラムダ式とStream』例6-12 の処理内容と同等のものを Stream API, Eclipse Collections (逐次実行/遅延実行) で比較
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
package com.nashcft; | |
import org.eclipse.collections.api.list.MutableList; | |
import org.eclipse.collections.impl.factory.Lists; | |
import org.junit.Test; | |
public class EclipseCollectionsLazyTest { | |
private MutableList<String> list = Lists.mutable.of("a", "bb", "ccc", "dd", "e", "ff", "ggg", "hh", "i"); | |
@Test | |
public void seq() { | |
String result = list | |
.select(s -> predicate(s)) | |
.collect(s -> toUpper(s)) | |
.getFirst(); | |
System.out.println("result: " + result); | |
// => | |
// length: 1 | |
// length: 2 | |
// length: 3 | |
// length: 2 | |
// length: 1 | |
// length: 2 | |
// length: 3 | |
// length: 2 | |
// length: 1 | |
// toUpper: CCC | |
// toUpper: GGG | |
// result: CCC | |
} | |
@Test | |
public void lazy() { | |
String result = list.asLazy() | |
.select(s -> predicate(s)) | |
.collect(s -> toUpper(s)) | |
.getFirst(); | |
System.out.println("result: " + result); | |
// => | |
// length: 1 | |
// length: 2 | |
// length: 3 | |
// length: 1 <= ??? | |
// length: 2 <= ??? | |
// length: 3 <= ??? | |
// toUpper: CCC | |
// result: CCC | |
} | |
@Test | |
public void streamApi() { | |
String result = list.stream() | |
.filter(s -> predicate(s)) | |
.map(s -> toUpper(s)) | |
.findFirst().orElse("none"); | |
System.out.println("result: " + result); | |
// => | |
// length: 1 | |
// length: 2 | |
// length: 3 | |
// toUpper: CCC | |
// result: CCC | |
} | |
private boolean predicate(String s) { | |
System.out.println("length: " + s.length()); | |
return s.length() == 3; | |
} | |
private String toUpper(String s) { | |
System.out.println("toUpper: " + s.toUpperCase()); | |
return s.toUpperCase(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment