java-stream

Lazy sorting of entities in Java 8 Stream API on a daily basis?

一世执手 提交于 2019-12-06 07:14:11
问题 I have a large Java 8 Stream ( Stream<MyObject> ) with objects that looks like this: class MyObject { private String string; private Date timestamp; // Getters and setter removed from brevity } I know that all timestamps for day 1 will arrive before those in day 2 but within each day the timestamps could be out of order. I'd like to sort the MyObject 's in timestamp order on a per daily basis using the Stream API. Since the Stream is large I have to do this as lazily as possible, i.e. it

Inversion sum is not correct using Stream.reduce

邮差的信 提交于 2019-12-06 07:11:32
Inversion sum is not correct using Stream.reduce, what is going wrong here ? double[] array = {1.0, 2.0}; double iunversionSum = Arrays.stream(array).reduce(0.0, (a, b) -> Double.sum(1.0 / a, 1.0 / b)); output is .5 but expected is 1.5 (1/1 + 1/2) I think using map() it could be simplier. double inversionSum = Arrays.stream(arr).map(val -> 1 / val).sum(); The error in your reduce is: Double.sum(1.0 / a, 1.0 / b), starting the series with 0.0. Now check why your outcome is .5. Use Double.sum(a, 1.0 / b), if you want to use reduce. 来源: https://stackoverflow.com/questions/54144110/inversion-sum

Java 8 Streams - Sort nested lists hierarchically

旧时模样 提交于 2019-12-06 07:05:16
问题 Given the following example, I would like a stream function that sorts the list and also the nested list class Foo { public int sort; public List<Bar> bars; public Foo(int sort) { this.sort = sort; } } class Bar { public int sort; public Bar(int sort) { this.sort = sort; } } @Test public void testSortering() { Foo foo = new Foo(1); Foo foo2 = new Foo(2); Bar bar = new Bar(1); Bar bar2 = new Bar(2); foo.bars = Arrays.asList(bar2, bar); foo2.bars = Arrays.asList(bar2, bar); List<Foo> foos =

Group, Sum byType then get diff using Java streams

大憨熊 提交于 2019-12-06 07:04:24
问题 I would like to use stream to group, get the sum by type, then find the result of the different by type. So this is my data set. Sample(SampleId=1, SampleTypeId=1, SampleQuantity=5, SampleType=ADD), Sample(SampleId=2, SampleTypeId=1, SampleQuantity=15, SampleType=ADD), Sample(SampleId=3, SampleTypeId=1, SampleQuantity=25, SampleType=ADD), Sample(SampleId=4, SampleTypeId=1, SampleQuantity=5, SampleType=SUBTRACT), Sample(SampleId=5, SampleTypeId=1, SampleQuantity=25, SampleType=SUBTRACT) Sample

Do I need a custom Spliterator to avoid extra .stream() call?

吃可爱长大的小学妹 提交于 2019-12-06 06:19:11
I have this code which works fine, but I find it ugly. @EqualsAndHashCode public abstract class Actions { @Getter private List<ActionsBloc> blocs; public Actions mergeWith(@NotNull Actions other) { this.blocs = Stream.of(this.blocs, other.blocs) .flatMap(Collection::stream) .collect(groupingBy(ActionsBloc::getClass, reducing(ActionsBloc::mergeWith))) .values() .stream() .filter(Optional::isPresent) .map(Optional::get) .collect(toList()); return this; } } ActionsBloc is a super type which contains a list of Action . public interface ActionsBloc { <T extends Action> List<T> actions(); default

How do I deal with Function<T, R> and ellipsis/varargs in this case?

自闭症网瘾萝莉.ら 提交于 2019-12-06 06:18:46
问题 One of my project is throwing-lambdas; in it I aim to ease the use of potential @FunctionalInterface s in Stream s, whose only "defect" for being used in streams is that they throw checked exceptions (on my part I'd rather call defective the fact that you can't throw checked exceptions from streams but that's another story). So, for Function<T, R> I define this: @FunctionalInterface public interface ThrowingFunction<T, R> extends Function<T, R> { R doApply(T t) throws Throwable; default R

How to turn a List<Item> to a Map<Item, completeablefuture<xyz>>

…衆ロ難τιáo~ 提交于 2019-12-06 06:08:12
I have an async method with a completeablefuture result: public CompletableFuture<DogLater> asyncDogLater(String dogName){} I have a list of dogs: List<Dog> dogs; Now, I want to create a map from the dog's name to the Completeablefuture: Map<String, CompletableFuture<DogLater>> map; After checking this and this I was trying to do so: Map<String, CompletableFuture<DogLater>> completableFutures = dogs.stream() .collect( Collectors.toMap(Dog::getName, asyncDogLater(Dog::getName ))); But the compiler complains that the first Dog::getName is problematic since: Non-static method cannot be referenced

Java 8 - Streams Nested ForEach with different Collection

一笑奈何 提交于 2019-12-06 06:03:37
I try to understand the new Java 8 Streams and I tried for days to transfer nested foreach loops over collection in Java 8 Streams. Is it possible to refactor the following nested foreach loops including the if-conditions in Java-8-Streams? If yes what would it look like. ArrayList<ClassInq> Inq = new ArrayList<>(); TreeMap<String, SalesQuot> Quotations = new TreeMap<>(); ArrayList<ClassInq> tempInqAndQuot = new ArrayList<>(); ArrayList<SalesQuot> tempQuotPos = new ArrayList<>(); for(ClassInq simInq : this.Inq) { if(!simInq.isClosed() && !simInq.isDenied()) { for(Map.Entry<String, SalesQuot>

How to choose one of the largest elements in a collection

左心房为你撑大大i 提交于 2019-12-06 05:14:53
问题 I have a list of tuples, and I want to find the tuples with the largest x value. In the case where there are multiple largest x values, I want to choose one at random. I can't figure out how to implement this random selection functionality. Below is the code I have so far: public void testSelectRandomFromLargestVals() { List<Tuple<Integer, String>> list = new ArrayList<>(); list.add(new Tuple<>(5, "five-1")); list.add(new Tuple<>(2, "two")); list.add(new Tuple<>(3, "three")); list.add(new

Java8 stream lines and aggregate with action on terminal line

荒凉一梦 提交于 2019-12-06 05:09:25
Question How to execute an action after the last item of an ordered stream is processed but before it's closed ? This Action should be able to inject zero or more items in the stream pipe. Context I've got a very large file of the form : MASTER_REF1 SUBREF1 SUBREF2 SUBREF3 MASTER_REF2 MASTER_REF3 SUBREF1 ... Where SUBREF (if any) is applicable to MASTER_REF and both are complex objects (you can imagine it somewhat like JSON). On first look I tried something like : public void process(Path path){ MyBuilder builder = new MyBuilder(); Files.lines(path) .map(line->{ if(line.charAt(0)==' '){