java-stream

Split a collection of objects based on a condition

送分小仙女□ 提交于 2020-02-06 07:52:24
问题 I have a list of objects to be added in a bag and bags capacity is 100 qty The Object and Bag look like below public class MyObject{ String id; int qty; } public class MyBag{ String id; int qty; } Is there any way to split MyObject in multiple MyBags grouping on the qty limit using Java 8 streams For example: myObjects are [myObject1:{id1, 150}, myObject2:{id2, 30}, myObject3:{id3, 150}] Since bag has a capacity of 100. Bags should be grouped as [ bag1:[{id1, 100}], bag2:[{id1, 50},{id2, 30},

Split a collection of objects based on a condition

╄→гoц情女王★ 提交于 2020-02-06 07:52:05
问题 I have a list of objects to be added in a bag and bags capacity is 100 qty The Object and Bag look like below public class MyObject{ String id; int qty; } public class MyBag{ String id; int qty; } Is there any way to split MyObject in multiple MyBags grouping on the qty limit using Java 8 streams For example: myObjects are [myObject1:{id1, 150}, myObject2:{id2, 30}, myObject3:{id3, 150}] Since bag has a capacity of 100. Bags should be grouped as [ bag1:[{id1, 100}], bag2:[{id1, 50},{id2, 30},

convert one class object into another using stream

最后都变了- 提交于 2020-02-04 05:29:27
问题 I know this question can be misleading, if possible someone may correct it, I am not sure how to ask a question with this situation. I am trying to Convert Class X into Class Y (here class Y contains fields of class X, but in different way, Eg:- Integer a, b; inside class X , converts to Map<a, b> in class Y while other variables stay the same.), using streams in java 8. I am trying to return the final object to the UI part as json. The project is done in spring boot . Both Class X and Y

Inclusive takeWhile() for Streams

大憨熊 提交于 2020-02-02 15:44:46
问题 I want to know if there is a way to add the last element of the stream that was tested against the condition of the method takeWhile(). I believe I want to achieve something similar to RxJava's takeUntil() method. I'm guessing there is no direct way to do this (correct me if I am mistaken), but I would like to know if there is a proper workaround to achieve this that I am now aware of. I've searched throughout Stackoverflow with little to no success. If you think there are threads that could

Why many Java Stream interface methods use lower bounded wildcard in parameters instead of generic type?

萝らか妹 提交于 2020-01-31 18:17:53
问题 Many Java Stream interface methods use lower bounded wildcard in parameters for example Stream<T> filter(Predicate<? super T> pred) and void forEach(Consumer<? super T> action) What is the advantage of using Predicate<? super T> over Predicate<T> here? I understand, with Predicate<? super T> as parameter, Predicate object of T and super types can be passed into the method but i can't think of a situation where a Predicate of super type needed over the specific type? For example if i have a

Java 8 modify stream elements

对着背影说爱祢 提交于 2020-01-30 20:00:10
问题 I wanted to write pure function with Java 8 that would take a collection as an argument, apply some change to every object of that collection and return a new collection after the update. I want to follow FP principles so I dont want to update/modify the collection that was passed as an argument. Is there any way of doing that with Stream API without creating a copy of the original collection first (and then using forEach or 'normal' for loop)? Sample object below and lets assume that I want

Java 8 modify stream elements

筅森魡賤 提交于 2020-01-30 19:59:52
问题 I wanted to write pure function with Java 8 that would take a collection as an argument, apply some change to every object of that collection and return a new collection after the update. I want to follow FP principles so I dont want to update/modify the collection that was passed as an argument. Is there any way of doing that with Stream API without creating a copy of the original collection first (and then using forEach or 'normal' for loop)? Sample object below and lets assume that I want

Using Java 8 stream methods to get a max value

爱⌒轻易说出口 提交于 2020-01-30 07:45:49
问题 I would like to get the max value out of a list using java 8 stream methods. The structure is the following: I read a csv file and store the data of every line in a separate object of type Round . all these Round objects are stored in an ArrayList called arrRound all Round objects have a field: List<Hit> hits a Hit consists of 2 fields: int numberOfGames and int prizeAmount public class Round{ private List<Hits> hits; } public class Hits{ private int numberOfGames; private int prizeAmount; }

Additional brackets in Java8 stream map() function

馋奶兔 提交于 2020-01-30 02:56:30
问题 I'm working/testing streams in Java8 and come across very frustrating issue. I've got the code which compiles well: List<String> words = Arrays.asList("Oracle", "Java", "Magazine"); List<String> wordLengths = words.stream().map((x) -> x.toUpperCase()) .collect(Collectors.toList()); And second one (nearly the same) which throw a warnings: List<String> words = Arrays.asList("Oracle", "Java", "Magazine"); List<String> wordLengths = words.stream().map((x) -> { x.toUpperCase(); }).collect

How to use thenComparing in java stream

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-30 02:53:12
问题 I have a map with strings as values. I want to sort it firstly by length, and if length of the strings is the same, i want to sort it alphabetic. I wrote those code : String out = outMap.values().stream() .sorted(Comparator.comparing(e -> e.length()).thenComparing()...) .collect(Collectors.joining()); The problem is, when i am writing thenComparing, I couldn't use e.length() anymore. How can i fix it? EDIT : Map<Character, String> . I want to sort the strings and make one string in output by