Created
May 7, 2015 03:41
-
-
Save tag1216/fecba7db9b53f9dca91d to your computer and use it in GitHub Desktop.
Java8で独自のStreamを作成する ref: http://qiita.com/tag1216/items/3ebe6bf69903026a0520
This file contains hidden or 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
Stream<String> stream = Stream.of("a", "b"); | |
stream.forEach(System.out::println); |
This file contains hidden or 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
Stream.Builder<Integer> builder = Stream.builder(); | |
for (int i = 0; i < 5; i++) { | |
builder.add(i); | |
} | |
Stream<Integer> stream = builder.build(); | |
stream.forEach(System.out::println); |
This file contains hidden or 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
Stream<Double> stream = Stream.generate(() -> Math.random()); | |
stream.limit(3).forEach(System.out::println); |
This file contains hidden or 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
Stream<Integer> stream = Stream.iterate(2, x -> x * 2); | |
stream.limit(8).forEach(System.out::println); | |
//結果 | |
//2 | |
//4 | |
//8 | |
//16 | |
//32 | |
//64 | |
//128 | |
//256 |
This file contains hidden or 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
Iterator<Integer> itr = new Iterator<Integer>() { | |
public boolean hasNext() { | |
return true; | |
} | |
public Integer next() { | |
return (int) (Math.random() * 100); | |
} | |
}; | |
Spliterator<Integer> spliterator = | |
Spliterators.spliteratorUnknownSize(itr, Spliterator.NONNULL); | |
Stream<Integer> stream = StreamSupport.stream(spliterator, true); | |
stream.limit(5).forEach(System.out::println); |
This file contains hidden or 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 static java.util.Spliterator.*; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.nio.charset.StandardCharsets; | |
import java.util.Iterator; | |
import java.util.Spliterator; | |
import java.util.Spliterators; | |
import java.util.function.Consumer; | |
import java.util.stream.Stream; | |
import java.util.stream.StreamSupport; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.methods.CloseableHttpResponse; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.impl.client.CloseableHttpClient; | |
import org.apache.http.impl.client.HttpClients; | |
import com.google.gson.Gson; | |
import com.google.gson.stream.JsonReader; | |
public class QiitaApiExamples { | |
public static void items(Consumer<Stream<Item>> consumer) | |
throws ClientProtocolException, IOException { | |
Gson gson = new Gson(); | |
HttpGet httpGet = new HttpGet("http://qiita.com/api/v2/items"); | |
try ( | |
CloseableHttpClient httpClient = HttpClients.createDefault(); | |
CloseableHttpResponse res = httpClient.execute(httpGet); | |
JsonReader reader = new JsonReader(new InputStreamReader( | |
res.getEntity().getContent(), StandardCharsets.UTF_8)) | |
) { | |
reader.beginArray(); | |
Iterator<Item> itr = new Iterator<Item>() { | |
public boolean hasNext() { | |
try { | |
return reader.hasNext(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return false; | |
} | |
} | |
public Item next() { | |
return gson.fromJson(reader, Item.class); | |
} | |
}; | |
Spliterator<Item> spliterator = Spliterators.spliteratorUnknownSize( | |
itr, NONNULL | ORDERED | SIZED); | |
Stream<Item> stream = StreamSupport.stream(spliterator, false); | |
consumer.accept(stream); | |
reader.endArray(); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
items(stream -> stream.forEach(System.out::println)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment