#Java - Code Snippets for '#.Streams' - 6 code snippet(s) found |
|
Sample 1. Get all elements greater than 2, sort them and then push them to a new set, using Lambda Expression | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Set the predicate or the condition for filtering the elements.
Predicate<Integer> moreThan2Pred = (p) -> (p > 1);
// Use Filter to refine the element set, sort to Sort and Collectors.toSet to get a set out of Stream.
intSet = intSet.stream().filter(moreThan2Pred).sorted().collect(Collectors.toSet());
System.out.println(intSet); // Prints [2, 3, 4]
|
|
Like Feedback lambda expression collections set hashset generics stream predicate filter sort java.util.hashset java.util.stream.Collectors |
|
|
Sample 2. Combine two Summaries and Generate a new Summary using Lambda Expression | |
|
// Populate a List using Set elements.
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
Set<Integer> intSet2 = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
intSet2.add(1);
intSet2.add(2);
intSet2.add(3);
intSet2.add(4);
// Use the stream and collectors to Summarize all Integer elements
IntSummaryStatistics summary = intSet.stream().collect(Collectors.summarizingInt(p->((Integer)p)));
summary.combine(intSet2.stream().collect(Collectors.summarizingInt(p->((Integer)p))));
System.out.println(summary); // Prints IntSummaryStatistics{count=8, sum=20, min=1, average=2.500000, max=4}
|
|
Like Feedback lambda expression collections set hashset generics stream collectors Collectors.summarizingInt summary.combine java.util.hashset java.util.stream.Collectors java.util.IntSummaryStatistics |
|
|
Sample 3. Code Sample / Example / Snippet of org.apache.spark.mllib.stat.test.StreamingTest | |
|
public void streamingTest() {
List<BinarySample> trainingBatch = Arrays.asList(
new BinarySample(true, 1.0),
new BinarySample(false, 2.0));
JavaDStream<BinarySample> training =
attachTestInputStream(ssc, Arrays.asList(trainingBatch, trainingBatch), 2);
int numBatches = 2;
StreamingTest model = new StreamingTest()
.setWindowSize(0)
.setPeacePeriod(0)
.setTestMethod("welch");
model.registerStream(training);
attachTestOutputStream(training);
runStreams(ssc, numBatches, numBatches);
}
|
|
Like Feedback org.apache.spark.mllib.stat.test.StreamingTest |
|
|
Sample 4. Code Sample / Example / Snippet of org.apache.spark.network.protocol.StreamChunkId | |
|
public void handleSuccessfulFetch() throws Exception {
StreamChunkId streamChunkId = new StreamChunkId(1, 0);
TransportResponseHandler handler = new TransportResponseHandler(new LocalChannel());
ChunkReceivedCallback callback = mock(ChunkReceivedCallback.class);
handler.addFetchRequest(streamChunkId, callback);
assertEquals(1, handler.numOutstandingRequests());
handler.handle(new ChunkFetchSuccess(streamChunkId, new TestManagedBuffer(123)));
verify(callback, times(1)).onSuccess(eq(0), (ManagedBuffer) any());
assertEquals(0, handler.numOutstandingRequests());
}
|
|
Like Feedback org.apache.spark.network.protocol.StreamChunkId |
|
|
|
Sample 5. Code Sample / Example / Snippet of org.apache.storm.trident.Stream | |
|
public static StormTopology buildTopology(WindowsStoreFactory windowStore, WindowConfig windowConfig) throws Exception {
FixedBatchSpout spout = new FixedBatchSpout(new Fields("sentence"), 3, new Values("the cow jumped over the moon"),
new Values("the man went to the store and bought some candy"), new Values("four score and seven years ago"),
new Values("how many apples can you eat"), new Values("to be or not to be the person"));
spout.setCycle(true);
TridentTopology topology = new TridentTopology();
Stream stream = topology.newStream("spout1", spout).parallelismHint(16).each(new Fields("sentence"),
new Split(), new Fields("word"))
.window(windowConfig, windowStore, new Fields("word"), new CountAsAggregator(), new Fields("count"))
.peek(new Consumer() {
@Override
public void accept(TridentTuple input) {
LOG.info("Received tuple: [{}]", input);
}
});
return topology.build();
}
|
|
Like Feedback org.apache.storm.trident.Stream |
|
|
Sample 6. Usage of java.util.stream.Stream | |
|
List<Integer> list1 = Arrays.asList(1, 2);
List<Integer> list2 = Arrays.asList(4, 5);
Stream.of(list1, list1)
.flatMap(list -> list.stream())
.forEach(System.out::println);
|
|
Like Feedback Stream.flatMap System.out::println |
|
|