java-stream

Java 8 streams group by 3 fields and aggregate by sum and count produce single line output

寵の児 提交于 2019-12-04 13:54:17
问题 I know there a similar questions asked in the forum but none of them seem to be addressing my problem fully. Now I'm very new to Java 8, so please bear with me. I have a list of Products, for example: Input: name category type cost prod1 cat2 t1 100.23 prod2 cat1 t2 50.23 prod1 cat1 t3 200.23 prod3 cat2 t1 150.23 prod1 cat2 t1 100.23 Output: Single line (name, category, type) summing the cost and count of products. Product { public String name; public String category; public String type;

Group, Sum byType then get diff using Java streams

只谈情不闲聊 提交于 2019-12-04 13:53:32
I would like to use stream to group, get the sum by type, then find the result of the different by type. So this is my data set. Sample(SampleId=1, SampleTypeId=1, SampleQuantity=5, SampleType=ADD), Sample(SampleId=2, SampleTypeId=1, SampleQuantity=15, SampleType=ADD), Sample(SampleId=3, SampleTypeId=1, SampleQuantity=25, SampleType=ADD), Sample(SampleId=4, SampleTypeId=1, SampleQuantity=5, SampleType=SUBTRACT), Sample(SampleId=5, SampleTypeId=1, SampleQuantity=25, SampleType=SUBTRACT) Sample(SampleId=6, SampleTypeId=2, SampleQuantity=10, SampleType=ADD), Sample(SampleId=7, SampleTypeId=2,

Convert InputStream into Stream<String> of strings of fixed length

时光总嘲笑我的痴心妄想 提交于 2019-12-04 13:23:51
Like in Convert InputStream into Stream<String> given a Charset I want to convert an InputStream is into a Stream<String> stream . But this time instead of splitting the InputStream at the new line characters, I want to split it into parts of equal length. So all strings of the stream would have the same length (with a possible exception on the last element of the stream, that may be shorter). I don't think this is possible using class library methods only, so you'll have to write your own logic that follows the same pattern as BufferedReader.lines : InputStreamReader - Start by creating an

One upstream stream feeding multiple downstream streams

半腔热情 提交于 2019-12-04 13:08:00
问题 I have a general Streams API problem I'd like to solve "efficiently". Suppose I have a (possibly very large, possibly infinite) stream. I want to pre-process it in some way, for example, filtering out some items, and mutating some. Let's assume that this pre-processing is complex, time and compute intensive, so I do not want to do it twice. Next I want to do two distinct sets of operations to the item sequence and process the far end of each distinct sequence with a different stream-type

Lazy sorting of entities in Java 8 Stream API on a daily basis?

不羁的心 提交于 2019-12-04 12:38:30
I have a large Java 8 Stream ( Stream<MyObject> ) with objects that looks like this: class MyObject { private String string; private Date timestamp; // Getters and setter removed from brevity } I know that all timestamps for day 1 will arrive before those in day 2 but within each day the timestamps could be out of order. I'd like to sort the MyObject 's in timestamp order on a per daily basis using the Stream API. Since the Stream is large I have to do this as lazily as possible, i.e. it would be OK to hold one days worth of MyObject 's in memory but it would not be OK to hold much more than

May Java group&order&top in a call chain?

大兔子大兔子 提交于 2019-12-04 12:22:28
I have a POJO class class A { public int id; public String groupName; public String getGroupName() { return this.groupName; } public int value; public A(int id, String groupName, int value) { this.id = id; this.groupName = groupName; this.value = value; } } And id is Unique, but groupName is not. Then I have a List of A. List<A> list = new ArrayList<A>(); list.add(new A(1, "A", 3)); list.add(new A(2, "B", 5)); list.add(new A(3, "B", 7)); list.add(new A(4, "C", 7)); I want filter the list by groupName and value, return the biggest value each groupName. List<B> filtedList = list.... //filtedList

Extract duplicate objects from a List in Java 8

☆樱花仙子☆ 提交于 2019-12-04 12:01:35
问题 This code removes duplicates from the original list, but I want to extract the duplicates from the original list -> not removing them (this package name is just part of another project): Given: a Person pojo: package at.mavila.learn.kafka.kafkaexercises; import org.apache.commons.lang3.builder.ToStringBuilder; public class Person { private final Long id; private final String firstName; private final String secondName; private Person(final Builder builder) { this.id = builder.id; this

Is it important to use Characteristics.UNORDERED in Collectors when possible?

血红的双手。 提交于 2019-12-04 11:37:49
问题 Since I use streams a great deal, some of them dealing with a large amount of data, I thought it would be a good idea to pre-allocate my collection-based collectors with an approximate size to prevent expensive reallocation as the collection grows. So I came up with this, and similar ones for other collection types: public static <T> Collector<T, ?, Set<T>> toSetSized(int initialCapacity) { return Collectors.toCollection(()-> new HashSet<>(initialCapacity)); } Used like this Set<Foo> fooSet =

Java 8 Stream API toMap converting to TreeMap

北城以北 提交于 2019-12-04 11:34:18
问题 public class Message { private int id; private User sender; private User receiver; private String text; private Date senddate; .. } I have List<Message> list= new ArrayList<>(); I need to transform them to TreeMap<User,List<Message>> map I know how to do transform to HashMap using list.stream().collect(Collectors.groupingBy(Message::getSender)); But I need TreeMap with: Key - User with newest message senddate first Value - List sorted by senddate newest first Part of User class public class

Java Streams - Using a setter inside map()

可紊 提交于 2019-12-04 11:32:52
I have a discussion with colleague that we should not be using setters inside stream.map() like the solution suggested here - https://stackoverflow.com/a/35377863/1552771 There is a comment to this answer that discourages using map this way, but there hasn’t been a reason given as to why this is a bad idea. Can someone provide a possible scenario why this can break? I have seen some discussions where people talk about concurrent modification of the collection itself, by adding or removing items from it, but are there any negatives to using map to just set some values to a data object? Using