java-stream

Java 8 Stream groupingBy with custom logic

帅比萌擦擦* 提交于 2021-02-16 04:42:04
问题 I have a list of Records . Which has two fields: LocalDateTime instant and a Double data . I want to groupBy all the records by Hour and create a Map<Integer, Double> . Where the keys (Integer) are hours and values(Double) are last Data of that hour - first Data of that hour. What I have done so far is following: Function<Record, Integer> keyFunc = rec->rec.getInstant().getHour(); Map<Integer, List<Record>> valueMap = records.stream().collect(Collectors.groupingBy(keyFunc)); I want the value

Grouping list of objects where each object is 2 elements array

南笙酒味 提交于 2021-02-11 13:01:53
问题 List<Object[]> values = query.getResultList(); //values = [obj1,obj2,obj3] //obj1 = "1", "01"; //obj2 = "1", "02"; //obj3 = "1:, "03"; Map<String, List<String>> resultMap = values.stream().collect(Collectors.groupingBy(???)); I need a map grouped as - {"1",["01","02","03"]} I have seen few references but nothing seems to work. what should i put in place of "???" ? if you need anything else , kindly comment. Thanks in advance. ref : Shortcut for adding to List in a HashMap groupingby list of

groupingby list of arrays

假如想象 提交于 2021-02-11 08:10:37
问题 I get a list of object arrays that I need to group. The arrays contain different types of objects. Here is an example: List<Object[]> resultList = query.getResultList(); // call to DB // Let's say the object array contains objects of type Student and Book // and Student has a property Course. // Now I want to group this list by Student.getCourse() Map<String, Object[]> resultMap = resultList.stream().collect(Collectors.groupingBy(???)); What do I provide to the Collectors.groupingBy() method

Is there simplest possible reduce/fold method in java?

痞子三分冷 提交于 2021-02-10 12:41:16
问题 Other technologies I know (.Net, JS) contain the simplest fold/reduce operation: TResult reduce(TResult result, (TResult prevResult, TValue value) -> TResult) One method I found requires TValue and TResult to be the same type. Other one requires providing BinaryOperation that combines 2 TResults. None of those constraints matches my context. For now I ended up with code like: Accumulator acc = someInitialValue; for(Element element: list) { accumulator = reducer(accumulator, element); } but I

Is there simplest possible reduce/fold method in java?

泄露秘密 提交于 2021-02-10 12:37:47
问题 Other technologies I know (.Net, JS) contain the simplest fold/reduce operation: TResult reduce(TResult result, (TResult prevResult, TValue value) -> TResult) One method I found requires TValue and TResult to be the same type. Other one requires providing BinaryOperation that combines 2 TResults. None of those constraints matches my context. For now I ended up with code like: Accumulator acc = someInitialValue; for(Element element: list) { accumulator = reducer(accumulator, element); } but I

How to groupingBy into a Map and change the key type

依然范特西╮ 提交于 2021-02-08 23:40:26
问题 I have a code which is suppose to group a list of transaction objects into 2 categories; public class Transaction { public String type; public Integer amount; } The following function divided the list into 2 categories by checking a condition. The output map of the stream operation is Map<Boolean, List<Transaction>> , but I would like to use a String as its key. So I converted them manually. public static Map<String, List<Transaction>> partitionTransactionArray(List<Transaction> t1) { Map

Java Stream API : what kind of map method collect(Collectors.toMap()) returns?

风格不统一 提交于 2021-02-08 17:35:27
问题 What kind of map "hm" is? Map<String,Person> hm; try (BufferedReader br = new BufferedReader(new FileReader("person.txt")) { hm = br.lines().map(s -> s.split(",")) .collect(Collectors.toMap(a -> a[0] , a -> new Person(a[0],a[1],Integer.valueOf(a[2]),Integer.valueOf(a[3])))); Does it depend on declaration? Map<String,Person> hm = new HashMap<>(); Map<String,Person> hm = new TreeMap<>(); 回答1: No, initializing the variable referenced by hm is pointless, since the stream pipeline creates a new

How to generate all permutations of a List<List<String>>? [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-08 11:12:37
问题 This question already has answers here : Generating all possible permutations of a list recursively (5 answers) Generate String Permutations from multiple Set values (Java 8 Streams) (3 answers) Closed 1 year ago . I am trying to generate all permutations of a List<List<String>> but I am not getting unique permutations. My List<List<String>> looks something like [ [Test, Test 1, Test 2], [Apple, Sandwich, Banana], [Cake, Ice Cream, Fruit] ] I am trying to get every combination possible for

How to generate all permutations of a List<List<String>>? [duplicate]

泄露秘密 提交于 2021-02-08 11:12:35
问题 This question already has answers here : Generating all possible permutations of a list recursively (5 answers) Generate String Permutations from multiple Set values (Java 8 Streams) (3 answers) Closed 1 year ago . I am trying to generate all permutations of a List<List<String>> but I am not getting unique permutations. My List<List<String>> looks something like [ [Test, Test 1, Test 2], [Apple, Sandwich, Banana], [Cake, Ice Cream, Fruit] ] I am trying to get every combination possible for

Capitalize first letters in words in the string with different separators using java 8 stream

爷,独闯天下 提交于 2021-02-08 09:54:41
问题 I need to capitalize first letter in every word in the string, BUT it's not so easy as it seems to be as the word is considered to be any sequence of letters, digits, "_" , "-", "`" while all other chars are considered to be separators, i.e. after them the next letter must be capitalized. Example what program should do: For input: "#he&llo wo!r^ld" Output should be: "#He&Llo Wo!R^Ld" There are questions that sound similar here, but there solutions really don't help. This one for example: