java-stream

Can i partition a stream combining with the grouping by functionality?

我们两清 提交于 2021-02-08 08:56:19
问题 I am grouping and partioning a stream as follows: // Partioning Map<Boolean, List<Person>> partitioned = persons.stream(). collect(Collectors.partitioningBy(p -> p.getAge() > 20)); // Grouping Map<String, List<Person>> grouped = persons.stream() .collect(Collectors.groupingBy(p -> p.getCity())); Is there a way i can combine both of these? I tried combining both with using groupingBy inside partioningBy, but did not get the things right. Any suggestion? The expected result is the partition the

Java streams zip two lists

此生再无相见时 提交于 2021-02-08 06:22:09
问题 I have a HashSet of Persons. A person has a firstName, lastName and age like: Person("Hans", "Man", 36) My task is to get a list of the persons who are older than 17, sort them by age and concat the firstName with the lastName like: ["Hans Man","another name", "another name"] Im just allowed to import: import java.util.stream.Stream; import java.util.stream.Collectors; import java.util.List; import java.util.ArrayList; My idea was to sort them first, map the names in separate Streams and to

Searching anagrams with Java 8

我只是一个虾纸丫 提交于 2021-02-08 06:14:27
问题 I have to write program which should be reading file for anagrams and show word + his anagrams. Txt files is very big, after using scanner, listOfWords size is: 25000. Output example: word anagram1 anagram2 anagram3 ... word2 anagram1 anagram2... I have code, it works but very slow: private static List<String> listOfWords = new ArrayList<String>(); private static List<ArrayList<String>> allAnagrams = new ArrayList<ArrayList<String>>(); public static void main(String[] args) throws Exception {

Searching anagrams with Java 8

我的未来我决定 提交于 2021-02-08 06:09:51
问题 I have to write program which should be reading file for anagrams and show word + his anagrams. Txt files is very big, after using scanner, listOfWords size is: 25000. Output example: word anagram1 anagram2 anagram3 ... word2 anagram1 anagram2... I have code, it works but very slow: private static List<String> listOfWords = new ArrayList<String>(); private static List<ArrayList<String>> allAnagrams = new ArrayList<ArrayList<String>>(); public static void main(String[] args) throws Exception {

Java 8 Map to a List of Objects with one field grouped into a List

北战南征 提交于 2021-02-08 05:36:16
问题 Newbie question. I have an original bean coming from the DB row-by-row as public class DataBean { private Integer employeeId; private String org; private String comments; // + Constructors, getters/setters } I need to map it to a different bean with multiple Org's grouped by Employee ID into a List. Only Orgs can be multiple for an EmployeeID; the Comments field is guaranteed to be the same. public class CustomDataBean { private Integer employeeId; private List<String> orgs; private String

Collection of lambda functions in Java Streams

泄露秘密 提交于 2021-02-08 05:31:14
问题 I have a stream function KStream<K, V>[] branch(final Predicate<? super K, ? super V>... predicates) . I wanted to create a list of predicates dynamically. Is that possible? KStream<Long, AccountMigrationEvent>[] branches = stream .map((key, event) -> enrich(key, event)) .branch(getStrategies()); [...] private List<org.apache.kafka.streams.kstream.Predicate<Long, AccountMigrationEvent>> getStrategies() { ArrayList<Predicate<Long, AccountMigrationEvent>> predicates = new ArrayList<>(); for

How to merge child objects in list based on duplicate parent object

久未见 提交于 2021-02-08 03:45:30
问题 I have two entities with one to many relationship which are joined together by composite primary keys. Since Spring Data generates wrong count distinct query for oracle database , I have SQL output with cartesian join which leads to repeating row for parent object for every row of child object. I need to find out distinct parent objects based on composite keys and then add each child object for parent in a list and set it as property of parent object I am able to find out distinct parent

Stream.dropWhile() doesn't return correct value in two different values

瘦欲@ 提交于 2021-02-07 18:38:16
问题 I am trying to learn new features in Java-9 I come to know about the dropWhile method of Stream but it is returning different values in two different scenarios. Here is my code package src.module; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.List; public class Test { public static void main(String[] args) { String s[] = new String[3]; s[0] = "Hello"; s[1] = ""; s[2] = "World"; List<String> value = Stream.of(s).dropWhile(a -> a.isEmpty()).collect

How to extract a List<D> from a HashMap<E,R> using stream

空扰寡人 提交于 2021-02-07 12:27:17
问题 I want to know how to extract a List<D> from a HashMap<E,R> considering these constraints: E is a custom class; R is a custom class containing a Set<D> of custom objects; What I have tried: I tried addressing the issue in this question. In that previous case, I had a simple Map<E,List<R>> , but in this case I have to access the R class which has the targeted Set<D> . What I want to do in the following portion of the code is to get the Elements of the Set<D> whose country names are equal to

Java map an array of Strings to an array of Integers

牧云@^-^@ 提交于 2021-02-07 12:15:24
问题 I found this code on SO to map strings to ints Arrays.stream(myarray).mapToInt(Integer::parseInt).toArray(); But how do I make it map to Integer type not the primitive int? I tried switching from Integer.parseInt to Integer.valueOf , but it seems that the mapToInt() method forces the primitive type. I have an ArrayList of arrays of Integers, so I cannot use primitive ints. 回答1: Since String and Integer are both reference types you can simply call Stream::map to transform your array. Integer[]