Java 8 lambda: iterate over stream objects and use previous/next object(s) in stream
Tag : java , By : Nate Bedortha
Date : March 29 2020, 07:55 AM
wish helps you Sometimes, attempting to do everything with lambda expressions makes solutions more complicated. You can use: messages.stream()
.mapToLong(Message::getOffset)
.sorted()
.forEachOrdered(new LongConsumer() {
boolean first=true;
long expected;
public void accept(long value) {
if(first) first=false;
else if(value!=expected)
LOG.error("Missing offset(s) found in messages: missing from {} to {}",
expected, value);
expected=value+1;
}
});
long[] l = messages.stream().mapToLong(Message::getOffset).toArray();
Arrays.sort(l);
for(int ix=1; ix<l.length; ix++) {
long value = l[ix], expected = l[ix-1]+1;
if(value!=expected)
LOG.error("Missing offset(s) found in messages: missing from {} to {}",
expected, value);
}
OptionalLong optMin = messages.stream().mapToLong(Message::getOffset).min();
if(!optMin.isPresent()) return;
long min = optMin.getAsLong();
BitSet bset = messages.stream()
.mapToLong(Message::getOffset)
.collect(BitSet::new, (bs,l) -> bs.set((int)(l-min)), BitSet::or);
for(int set=0, clear; set>=0; ) {
clear = bset.nextClearBit(set);
set = bset.nextSetBit(clear);
if(set >= 0)
LOG.error("Missing offset(s) found in messages: missing from {} to {}",
min+clear, min+set);
}
LongSummaryStatistics stat = messages.stream()
.mapToLong(Message::getOffset).summaryStatistics();
if(stat.getCount()==0 ||
// all solutions assume that there are no duplicates, in this case,
// the following test allows to prove that there are no gaps:
stat.getMax()-stat.getMin()==messages.size()-1) {
return;
}
if(stat.getMax()-stat.getMin()>Integer.MAX_VALUE) {
// proceed with array based test
…
}
else {
long min = stat.getMin();
// proceed with BitSet based test
…
|
replace statement lambda with regular expression Lambda java 8 stream map
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I have this map function as part of my stream. parse.apply is basically doing Double::valueOf. My intelliJ suggest me I can replace statement lambda with regular expression Lambda. , You can filter out all empty strings before reaching .map: .filter(s -> !StringUtils.isEmpty(s))
.map(parse)
.map(s -> StringUtils.isEmpty(s) ? "0": s)
.map(parse)
|
Convert Stream<Stream<T>> to T[][] with Streams/Lambda's in Java
Tag : java , By : user91848
Date : March 29 2020, 07:55 AM
I wish this help you I am looking to see if there was a better way to solve my dilemma having to use these signatures (note: the T[][] is required because of Spock testing and I am providing T[][] as a data provider) , You can use Array.newInstance like this: public static <T> T[][] createArrays(Class<T> clazz, T... items) {
Stream<Stream<T>> streams = EnumPerm.of(items);
return streams
.map(s -> s.toArray(len -> (T[])Array.newInstance(clazz, len)))
.toArray(len -> (T[][])Array.newInstance(items.getClass(), len));
}
public static <T> T[][] createArrays(Class<T> clazz, T... items) {
Stream<Stream<T>> streams = EnumPerm.of(items);
T[] template = Arrays.copyOf(items, 0);
return streams
.map(s -> s.toArray(len -> Arrays.copyOf(template, len)))
.toArray(len -> (T[][])Array.newInstance(template.getClass(), len));
}
public static <T> T[][] createArrays(Class<T> clazz, T... items) {
Stream<Stream<T>> streams = EnumPerm.of(items);
return streams
.map(s -> s.toArray(len -> (T[])Array.newInstance(clazz, len)))
.toArray(len -> (T[][])Array.newInstance(clazz, len, 0));
}
|
AWS Lambda + Kinesis : from what position in the stream do lambda functions restart?
Date : March 29 2020, 07:55 AM
|
Should I care about dynamodb stream shards if I process stream events by lambda?
Tag : java , By : user130518
Date : March 29 2020, 07:55 AM
|