java-stream

Single element in multiple groups when grouping with stream API

余生颓废 提交于 2020-12-10 08:50:59
问题 I'm reviewing some old code, where I'm grouping elements. It looks more or less like this: Map<Long,List<Items>> groupedItems = ... for (long groupid : groups){ for (Item item :items){ if (isGroupAccepting(item.getId(),groupid) || groupid == item.getGroup()) { groupedItems.get(groupid).add(item); } } } I planned to replace it using grouping from stream API, but I'm stuck. It works fine for my second condition, but how to deal with the first one, where item should be added to every group which

Single element in multiple groups when grouping with stream API

三世轮回 提交于 2020-12-10 08:50:06
问题 I'm reviewing some old code, where I'm grouping elements. It looks more or less like this: Map<Long,List<Items>> groupedItems = ... for (long groupid : groups){ for (Item item :items){ if (isGroupAccepting(item.getId(),groupid) || groupid == item.getGroup()) { groupedItems.get(groupid).add(item); } } } I planned to replace it using grouping from stream API, but I'm stuck. It works fine for my second condition, but how to deal with the first one, where item should be added to every group which

How to use stream on method that return boolean value with condition

谁说胖子不能爱 提交于 2020-12-08 06:46:40
问题 I am using this method: public boolean checkRowsFilterNameBy(String filter){ waitForElmContainsText(searchButton, "Search"); List<AuditRow> listRows = auditTable.getTable(); for(AuditRow row : listRows){ if(!row.nameStr.equals(filter)||!row.nameStr.contains(filter)) return false; } return true; } and I want to be able to change it using Stream , I've tried the following, but I am missing something: listRows.stream().forEach(auditRow -> { if(auditRow.actionStr.equals(filter))return true;} else

Buffer Operator on Java 8 Streams

帅比萌擦擦* 提交于 2020-12-07 05:02:55
问题 I am trying to write a java 8 stream collector which mirrors the functionality of rxjava buffer operator I have a working code for this: // This will gather numbers 1 to 13 and combine them in groups of // three while preserving the order even if its a parallel stream. final List<List<String>> triads = IntStream.range(1, 14) .parallel() .boxed() .map(Object::toString) .collect(ArrayList::new, accumulator, combiner); System.out.println(triads.toString()) The accumulator here is this: final

Buffer Operator on Java 8 Streams

徘徊边缘 提交于 2020-12-07 05:01:31
问题 I am trying to write a java 8 stream collector which mirrors the functionality of rxjava buffer operator I have a working code for this: // This will gather numbers 1 to 13 and combine them in groups of // three while preserving the order even if its a parallel stream. final List<List<String>> triads = IntStream.range(1, 14) .parallel() .boxed() .map(Object::toString) .collect(ArrayList::new, accumulator, combiner); System.out.println(triads.toString()) The accumulator here is this: final

Buffer Operator on Java 8 Streams

北城以北 提交于 2020-12-07 05:01:19
问题 I am trying to write a java 8 stream collector which mirrors the functionality of rxjava buffer operator I have a working code for this: // This will gather numbers 1 to 13 and combine them in groups of // three while preserving the order even if its a parallel stream. final List<List<String>> triads = IntStream.range(1, 14) .parallel() .boxed() .map(Object::toString) .collect(ArrayList::new, accumulator, combiner); System.out.println(triads.toString()) The accumulator here is this: final

Java 8 Stream Collectors - Collector to create a Map with objects in multiple buckets

我是研究僧i 提交于 2020-12-05 20:26:32
问题 The following code works and is readable but it seems to me I have intermediate operations that feel like they shouldn't be necessary. I've written this simplified version as the actual code is part of a much larger process. I've got a Collection of Widget , each with a name and multiple types (indicated by constants of the WidgetType enum). These multiple types are gettable as a Stream<WidgetType> though, if necessary, I could return those as some other type. (For various reasons, it is

Java 8 Stream Collectors - Collector to create a Map with objects in multiple buckets

人盡茶涼 提交于 2020-12-05 20:23:09
问题 The following code works and is readable but it seems to me I have intermediate operations that feel like they shouldn't be necessary. I've written this simplified version as the actual code is part of a much larger process. I've got a Collection of Widget , each with a name and multiple types (indicated by constants of the WidgetType enum). These multiple types are gettable as a Stream<WidgetType> though, if necessary, I could return those as some other type. (For various reasons, it is

Java 8 Stream Collectors - Collector to create a Map with objects in multiple buckets

一个人想着一个人 提交于 2020-12-05 20:21:37
问题 The following code works and is readable but it seems to me I have intermediate operations that feel like they shouldn't be necessary. I've written this simplified version as the actual code is part of a much larger process. I've got a Collection of Widget , each with a name and multiple types (indicated by constants of the WidgetType enum). These multiple types are gettable as a Stream<WidgetType> though, if necessary, I could return those as some other type. (For various reasons, it is

Stuck with lambda expression and Map

六月ゝ 毕业季﹏ 提交于 2020-12-05 05:30:06
问题 I have the Person class: import java.util.*; public class Person { private String name; Map<String,Integer> Skills=new HashMap<>(); // skill name(String) and level(int) public String getName(){ return this.name; } public Map<String,Integer> getSkills(){ return this.Skills; } } And the App class: import java.util.*; import java.util.Map.Entry; import static java.util.stream.Collectors.*; import static java.util.Comparator.*; public class App { private List<Person> people=new ArrayList<>(); //