java-stream

Convert Nested List of Integers to a two dimensional array using Streams ( List<List<Integer>> -> int[][] ) [duplicate]

倖福魔咒の 提交于 2021-01-29 07:58:07
问题 This question already has answers here : Convert ArrayList into 2D array containing varying lengths of arrays (6 answers) Closed 1 year ago . I searched for similar questions but I found only for the object String which dont apply to this case: Want to convert List<List<Integer>> list to int[][] array using streams So far I got this: int[][] array= list.stream().map(List::toArray)... I used as a base other similar questions I found. But these use String and can't make it work for Integers-

Convert For-Loop into Java stream

我的梦境 提交于 2021-01-29 07:28:32
问题 I'm new to Java 8 Streams and I'm currently trying to convert a for loop into a java 8 stream. Could I get some help? for (Subscription sub : sellerSubscriptions) { if (orders.get(Product).test(sub)) { orderableSubscriptions.add(sub.getId()); } } sellerSubscriptions = List. orders = Map<String,Predicate<Subscription>> orderableSubscriptions = Set<String> 回答1: Create a Stream of Subscriptions via the Collection#stream() method Use of the Stream#filter() method to "simulate" the if statement,

Report on a multimap by producing a new map of each key mapped to the count of elements in its collection value

佐手、 提交于 2021-01-29 02:34:58
问题 Imagine a multimap tracking persons, each of whom has a list of assigned tasks. Map< Person , List< Task > > personToTasks = Map.of( new Person( "Alice" ) , List.of( new Task( "a1" ), new Task( "a2") ) , new Person( "Bob" ) , List.of( new Task( "b1" ) ) , new Person( "Carol" ) , List.of( new Task( "c1" ), new Task( "c2"), new Task( "c3") ) ) ; How can I use streams to get a new map, mapping each Person to an Integer with the count of items found in their list of assigned tasks? How to get a

Report on a multimap by producing a new map of each key mapped to the count of elements in its collection value

久未见 提交于 2021-01-29 02:32:35
问题 Imagine a multimap tracking persons, each of whom has a list of assigned tasks. Map< Person , List< Task > > personToTasks = Map.of( new Person( "Alice" ) , List.of( new Task( "a1" ), new Task( "a2") ) , new Person( "Bob" ) , List.of( new Task( "b1" ) ) , new Person( "Carol" ) , List.of( new Task( "c1" ), new Task( "c2"), new Task( "c3") ) ) ; How can I use streams to get a new map, mapping each Person to an Integer with the count of items found in their list of assigned tasks? How to get a

Sorting and Grouping on a list of objects

我的未来我决定 提交于 2021-01-28 05:08:12
问题 I have a List of Procedure objects as below Procedure1 01/01/2020 Procedure2 03/01/2020 Procedure3 03/01/2020 Procedure1 04/01/2020 Procedure5 05/01/2020, 02/01/2020 Procedure2 06/01/2020 and my Procedure class is like Class Procedure { List<Date> procedureDate; String procedureName; } I want to sort and group the objects based on the below conditions. All procedures should be grouped based on the procedure name. Procedures must be in descending order of procedure date. [first element in date

Sorting and Grouping on a list of objects

余生长醉 提交于 2021-01-28 05:06:55
问题 I have a List of Procedure objects as below Procedure1 01/01/2020 Procedure2 03/01/2020 Procedure3 03/01/2020 Procedure1 04/01/2020 Procedure5 05/01/2020, 02/01/2020 Procedure2 06/01/2020 and my Procedure class is like Class Procedure { List<Date> procedureDate; String procedureName; } I want to sort and group the objects based on the below conditions. All procedures should be grouped based on the procedure name. Procedures must be in descending order of procedure date. [first element in date

Java stream groupingBy and sum multiple fields

一曲冷凌霜 提交于 2021-01-27 18:54:39
问题 Here is my List fooList class Foo { private String name; private int code; private int account; private int time; private String others; ... constructor, getters & setters } e.g.(all the value of account has been set to 1) new Foo(First, 200, 1, 400, other1), new Foo(First, 200, 1, 300, other1), new Foo(First, 201, 1, 10, other1), new Foo(Second, 400, 1, 20, other2), new Foo(Second, 400, 1, 40, other2), new Foo(Third, 100, 1, 200, other3), new Foo(Third, 101, 1, 900, other3) I want to

How to get all elements in nested collection with streams

我与影子孤独终老i 提交于 2021-01-27 17:38:59
问题 I have a class which contains different nested collections, now I want to receive all the elements of the nested collections, concrete I want to collect all the StrokePoints of the collections. I can solve it with "old" java but how to do it with streams? int strokesCounter = 0; List<StrokePoint> pointList = new ArrayList<>(); if (!strokesData.getListOfSessions().isEmpty()) { for (SessionStrokes session : strokesData.getListOfSessions()) { List<Strokes> strokes = session.getListOfStrokes();

Comparing two collections using Stream - anyMatch

倾然丶 夕夏残阳落幕 提交于 2021-01-26 21:18:54
问题 I want to compare if any of the objects in a list2 is present in a list1 . I could iterate over both lists and compare all elements using .contains() but I am wondering if there is not a more efficient way. I found this and I am trying to implement the suggested method: List<Item> list1; List<Item> list2; boolean anyMatch = list1.stream().anyMatch(x -> x.equals(list2.stream())); System.out.println(anyMatch); When I do this I constantly get false , even when I'd expect a true . How come? 回答1:

Comparing two collections using Stream - anyMatch

↘锁芯ラ 提交于 2021-01-26 21:16:32
问题 I want to compare if any of the objects in a list2 is present in a list1 . I could iterate over both lists and compare all elements using .contains() but I am wondering if there is not a more efficient way. I found this and I am trying to implement the suggested method: List<Item> list1; List<Item> list2; boolean anyMatch = list1.stream().anyMatch(x -> x.equals(list2.stream())); System.out.println(anyMatch); When I do this I constantly get false , even when I'd expect a true . How come? 回答1: