本文整理匯總了Java中java.util.stream.Stream.generate方法的典型用法代碼示例。如果您正苦於以下問題:Java Stream.generate方法的具體用法?Java Stream.generate怎麽用?Java Stream.generate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.stream.Stream
的用法示例。
在下文中一共展示了Stream.generate方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: fromChannel
import java.util.stream.Stream; //導入方法依賴的package包/類
public static Stream<HttpResponse> fromChannel(final ReadableByteChannel channel) {
HttpResponseSupplier consumer = new HttpResponseSupplier();
FutureTask<Void> futureTask = HttpResponseReader.fromChannel(channel, consumer);
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(futureTask);
boolean incomplete = true;
while (incomplete) {
if (futureTask.isDone()) {
executor.shutdown();
return Stream.generate(consumer);
}
}
executor.shutdown();
return Stream.empty();
}
示例2: main
import java.util.stream.Stream; //導入方法依賴的package包/類
public static void main(String[] args) {
Random r = new Random(9235563856L);
Stream<Point> gen = Stream.generate(() -> new Point(r.nextDouble() * size, r.nextDouble() * size));
Voronoi diagram = new Voronoi(gen.limit(1024).collect(Collectors.toList())).relax().relax();
// assert diagram.getGraph().edgeStream().noneMatch(e -> e.getA() == null && e.getB() == null);
diagram.getGraph().edgeStream().forEach(System.out::println);
RenderVoronoi frame = new RenderVoronoi(diagram);
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.setSize(size, size+32);
frame.setVisible(true);
}
示例3: testBaseStreams
import java.util.stream.Stream; //導入方法依賴的package包/類
public void testBaseStreams() {
Stream<String> arrayList = new ArrayList<String>().stream();
Stream<String> linkedList = new LinkedList<String>().stream();
Stream<String> hashSet = new HashSet<String>().stream();
Stream<String> treeSet = new TreeSet<String>().stream();
Stream<String> linkedHashSet = new LinkedHashSet<String>().stream();
Stream<String> repeat = Stream.generate(() -> "");
assertFlags(OpTestCase.getStreamFlags(arrayList),
EnumSet.of(ORDERED, SIZED),
EnumSet.of(DISTINCT, SORTED, SHORT_CIRCUIT));
assertFlags(OpTestCase.getStreamFlags(linkedList),
EnumSet.of(ORDERED, SIZED),
EnumSet.of(DISTINCT, SORTED, SHORT_CIRCUIT));
assertFlags(OpTestCase.getStreamFlags(hashSet),
EnumSet.of(SIZED, DISTINCT),
EnumSet.of(ORDERED, SORTED, SHORT_CIRCUIT));
assertFlags(OpTestCase.getStreamFlags(treeSet),
EnumSet.of(ORDERED, SIZED, DISTINCT, SORTED),
EnumSet.of(SHORT_CIRCUIT));
assertFlags(OpTestCase.getStreamFlags(linkedHashSet),
EnumSet.of(ORDERED, DISTINCT, SIZED),
EnumSet.of(SORTED, SHORT_CIRCUIT));
assertFlags(OpTestCase.getStreamFlags(repeat),
EnumSet.noneOf(StreamOpFlag.class),
EnumSet.of(DISTINCT, SORTED, SHORT_CIRCUIT));
}
示例4: getFibNos
import java.util.stream.Stream; //導入方法依賴的package包/類
@GET
@Timed
public Response getFibNos(@QueryParam("limit") Optional<Long> limit) {
List<BigInteger> fibonacciWindow = new LinkedList<>(Arrays.asList(BigInteger.ZERO, BigInteger.ONE));
Stream<BigInteger> fibonacciStream = Stream.generate(() -> {
BigInteger nextValue = fibonacciWindow.get(1).add(fibonacciWindow.get(0));
fibonacciWindow.add(nextValue);
return fibonacciWindow.remove(0);
});
if(limit.isPresent()) {
fibonacciStream = fibonacciStream.limit(limit.get());
}
return Response.ok(new ResponseSequenceStreamer(fibonacciStream)).build();
}
示例5: stream
import java.util.stream.Stream; //導入方法依賴的package包/類
@Override
public Stream<E> stream(Random random) {
Preconditions.checkNotNull(random, "random must not be null");
return Stream.generate(() -> pick(random));
}
示例6: main
import java.util.stream.Stream; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException {
//Creating Streams using user/programmatically specified elements
Stream<String> Userstream = Stream.of("Creating","Streams","from","Specific","elements");
Userstream.forEach(p -> System.out.println(p));
//Creating Streams using array of objects
Stream<String> ArrayStream = Stream.of( new String[]{"Stream","from","an","array","of","objects"} );
ArrayStream.forEach(p -> System.out.println(p));
//Creating Streams from an array
String[] StringArray=new String[]{"We","can","convert","an","array","to","a","Stream","using","Arrays","as","well"};
Stream<String> StringStream=Arrays.stream(StringArray);
StringStream.forEach(p -> System.out.println(p));
//Creating Streams from Collection
List<Double> myCollection = new ArrayList<>();
for(int i=0; i<10; i++){
myCollection.add(Math.random());
}
//sequential stream
Stream<Double> sequentialStream = myCollection.stream();
sequentialStream.forEach(p -> System.out.println(p));
//parallel stream
Stream<Double> parallelStream = myCollection.parallelStream();
parallelStream.forEach(p -> System.out.println(p));
//Stream from Hashmap
Map<String, Integer> mapData = new HashMap<>();
mapData.put("This", 1900);
mapData.put("is", 2000);
mapData.put("HashMap", 2100);
mapData.entrySet()
.stream()
.forEach(p -> System.out.println(p));
mapData.keySet()
.stream()
.forEach(p-> System.out.println(p));
//primitive streams
IntStream.range(1, 4)
.forEach(p -> System.out.println(p));
LongStream.rangeClosed(1, 4)
.forEach(p -> System.out.println(p));
DoubleStream.of(1.0,2.0,3.0,4.0)
.forEach(p -> System.out.println(p));
//Infinite Streams using generate()
Stream <Double> sequentialDoubleStream = Stream.generate(Math :: random);
Stream<Integer> sequentialIntegerStream = Stream.generate(new AtomicInteger () :: getAndIncrement);
//Infinite Streams using iterate()
Stream <Integer> sequentialIntegerStream1 = Stream.iterate (Integer.MIN_VALUE, i -> i++);
Stream <BigInteger> sequentialBigIntegerStream = Stream.iterate(BigInteger.ZERO, i -> i.add (BigInteger.TEN));
//Streams from File
Stream<String> streamOfStrings = Files.lines(Paths.get("Apology_by_Plato.txt"));
Stream<String> streamWithCharset = Files.lines(Paths.get("Apology_by_Plato.txt"), Charset.forName("UTF-8"));
}
開發者ID:PacktPublishing,項目名稱:Apache-Spark-2x-for-Java-Developers,代碼行數:72,代碼來源:CreateStreamExample.java
示例7: stream
import java.util.stream.Stream; //導入方法依賴的package包/類
default Stream<T> stream() {
return Stream.generate(this);
}
示例8: verifyInfiniteStreamEmission
import java.util.stream.Stream; //導入方法依賴的package包/類
@Test
public void verifyInfiniteStreamEmission() {
Stream<Double> stream = Stream.generate(Math::random);
Flux<Double> flux = Flux.fromStream(stream);
StepVerifier.create(flux).expectNextCount(5).thenCancel().verify();
}
示例9: generatePojoStream
import java.util.stream.Stream; //導入方法依賴的package包/類
/**
* Generates an infinite stream of POJOs of type <code>T</code>
* @return An infinite stream of <code>T</code> POJOs
*/
default Stream<T> generatePojoStream() {
return Stream.generate(this::generatePojo);
}
示例10: randomDatedCandleStickStream
import java.util.stream.Stream; //導入方法依賴的package包/類
/**
* Creates a stream of random {@link M1} {@link DatedCandleStick}s.
*
* @param seed
* A {@link DatedCandleStick} that is used as both, the first element of the stream, as seed for the
* random price values of the following element and as basis for the variability of the prices of the
* generated stream.
* @return the stream of random candle sticks
*/
public static Stream<DatedCandleStick<M1>> randomDatedCandleStickStream(final DatedCandleStick<M1> seed) {
return Stream.generate(new RandomCandleStickSupplier(seed));
}