Java Code Examples for java.util.stream.Stream#generate()
The following examples show how to use
java.util.stream.Stream#generate() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TableDisplayStreamMapModelTest.java From beakerx with Apache License 2.0 | 6 votes |
@NotNull private Stream<Map<String, Object>> generateInfiniteStream() { return Stream.generate(new Supplier<Map<String, Object>>() { Random random = new Random(); int index = 0; @Override public Map<String, Object> get() { return new LinkedHashMap<String, Object>() { { put("str1", index++); put("str2", random.nextFloat()); put("str3", random.nextInt()); } }; } }); }
Example 2
Source File: DataStreamGeneratorTest.java From ignite with Apache License 2.0 | 6 votes |
/** */ @Test public void testAsMap() { DataStreamGenerator generator = new DataStreamGenerator() { @Override public Stream<LabeledVector<Double>> labeled() { return Stream.generate(() -> new LabeledVector<>(VectorUtils.of(1., 2.), 100.)); } }; int N = 100; Map<Vector, Double> dataset = generator.asMap(N); assertEquals(N, dataset.size()); dataset.forEach(((vector, label) -> { assertArrayEquals(new double[] {1., 2.}, vector.asArray(), 1e-7); assertEquals(100., label, 1e-7); })); }
Example 3
Source File: StreamFlagsTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
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)); }
Example 4
Source File: DataStreamGeneratorTest.java From ignite with Apache License 2.0 | 5 votes |
/** */ @Test public void testAsDatasetBuilder() throws Exception { AtomicInteger cntr = new AtomicInteger(); DataStreamGenerator generator = new DataStreamGenerator() { @Override public Stream<LabeledVector<Double>> labeled() { return Stream.generate(() -> { int val = cntr.getAndIncrement(); return new LabeledVector<>(VectorUtils.of(val), (double)val % 2); }); } }; int N = 100; cntr.set(0); DatasetBuilder<Vector, Double> b1 = generator.asDatasetBuilder(N, 2); cntr.set(0); DatasetBuilder<Vector, Double> b2 = generator.asDatasetBuilder(N, (v, l) -> l == 0, 2); cntr.set(0); DatasetBuilder<Vector, Double> b3 = generator.asDatasetBuilder(N, (v, l) -> l == 1, 2, new UpstreamTransformerBuilder() { @Override public UpstreamTransformer build(LearningEnvironment env) { return new UpstreamTransformerForTest(); } }); checkDataset(N, b1, v -> (Double)v.label() == 0 || (Double)v.label() == 1); checkDataset(N / 2, b2, v -> (Double)v.label() == 0); checkDataset(N / 2, b3, v -> (Double)v.label() < 0); }
Example 5
Source File: DataStreamGeneratorTest.java From ignite with Apache License 2.0 | 5 votes |
/** */ @Test public void testUnlabeled() { DataStreamGenerator generator = new DataStreamGenerator() { @Override public Stream<LabeledVector<Double>> labeled() { return Stream.generate(() -> new LabeledVector<>(VectorUtils.of(1., 2.), 100.)); } }; generator.unlabeled().limit(100).forEach(v -> assertArrayEquals(new double[] {1., 2.}, v.asArray(), 1e-7)); }
Example 6
Source File: ClientServerTest.java From monsoon with BSD 3-Clause "New" or "Revised" License | 5 votes |
private static Stream<Integer> generate() { class Generator implements Supplier<Integer> { private int i; @Override public Integer get() { return i++; } } return Stream.generate(new Generator()); }
Example 7
Source File: StreamFlagsTest.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
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)); }
Example 8
Source File: StreamFlagsTest.java From hottub with GNU General Public License v2.0 | 5 votes |
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)); }
Example 9
Source File: StreamFlagsTest.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
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)); }
Example 10
Source File: TicketCategoryGenerator.java From alf.io with GNU General Public License v3.0 | 5 votes |
public static Stream<TicketCategory> generateCategoryStream() { AtomicInteger generator = new AtomicInteger(); return Stream.generate(() -> { boolean isBounded = generator.incrementAndGet() % 3 != 0; TicketCategory tc = Mockito.mock(TicketCategory.class); when(tc.isBounded()).thenReturn(isBounded); when(tc.getMaxTickets()).thenReturn(isBounded ? 2 : -1); return tc; }); }
Example 11
Source File: RandomInsertMutationGenerator.java From DataflowTemplates with Apache License 2.0 | 5 votes |
public Stream<MutationGroup> stream() { Map<String, TableSupplier> suppliers = new HashMap<>(); for (Table t : ddl.allTables()) { suppliers.put(t.name(), new TableSupplier(t)); } Collection<Table> roots = ddl.rootTables(); List<Supplier<MutationGroup>> groupSuppliers = new ArrayList<>(); Random rand = new Random(); for (Table r : roots) { groupSuppliers.add(new TableHierarchySupplier(ddl, r, suppliers, rand)); } return Stream.generate(new DbSupplier(groupSuppliers, rand)); }
Example 12
Source File: RandomValueGenerator.java From DataflowTemplates with Apache License 2.0 | 5 votes |
public Stream<Value> valueStream(Column column) { return Stream.generate(() -> { int threshold = nullThreshold; if (column.notNull()) { threshold = -1; } if (random.nextInt(100) < threshold) { return generateNullValue(column.type()); } return generate(column); }); }
Example 13
Source File: DataStreamGeneratorTest.java From ignite with Apache License 2.0 | 5 votes |
/** */ @Test public void testMapVectors() { DataStreamGenerator generator = new DataStreamGenerator() { @Override public Stream<LabeledVector<Double>> labeled() { return Stream.generate(() -> new LabeledVector<>(VectorUtils.of(1., 2.), 100.)); } }; generator.mapVectors(v -> VectorUtils.of(2., 1.)).labeled().limit(100).forEach(v -> { assertArrayEquals(new double[] {2., 1.}, v.features().asArray(), 1e-7); assertEquals(100., v.label(), 1e-7); }); }
Example 14
Source File: CreateStreamExample.java From Apache-Spark-2x-for-Java-Developers with MIT License | 4 votes |
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")); }
Example 15
Source File: ArrayDomain.java From tcases with MIT License | 4 votes |
/** * Returns a random sequence of values from this domain. */ public Stream<DataValue<List<DataValue<T>>>> values( ResolverContext context) { return Stream.generate( () -> dataValueOf( newArray( context))); }
Example 16
Source File: ObjectDomain.java From tcases with MIT License | 4 votes |
/** * Returns a random sequence of values from this domain. */ public Stream<DataValue<Map<String,DataValue<?>>>> values( ResolverContext context) { return Stream.generate( () -> dataValueOf( newObject( context))); }
Example 17
Source File: AbstractStringDomain.java From tcases with MIT License | 4 votes |
/** * Returns a random sequence of possible members of this domain. */ protected Stream<String> candidates( ResolverContext context) { return Stream.generate( () -> newValue( context)); }
Example 18
Source File: GenTest.java From QuickTheories with Apache License 2.0 | 4 votes |
private <T> Stream<T> generate(Gen<T> gen) { return Stream.generate( () -> gen.generate(source)); }
Example 19
Source File: ElmScope.java From elm-plugin with MIT License | 4 votes |
public static Stream<Optional<ElmLowerCaseId>> recordFieldsFor(ElmFile file) { ElmRecordFieldsProvider p = new ElmRecordFieldsProvider(file); return Stream.generate(p::nextField); }
Example 20
Source File: UnixStream.java From unix-stream with MIT License | 2 votes |
/** * Create a new infinite UnixStream from the standard input. * * @return a new infinite UnixStream from the standard input. * @throws IOException thrown if an error occurs during reading the standard input */ static UnixStream<String> cat() throws IOException { return new UnixStreamImpl<>(Stream.generate(new StandardInputSupplier(new BufferedReader(new InputStreamReader(in))))); }