java-stream

One upstream stream feeding multiple downstream streams

孤人 提交于 2019-12-03 08:53:42
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 construction. For an infinite stream, this would be a forEach, for a finite one, it might be a collector or

Find the difference between two collections in Java 8?

。_饼干妹妹 提交于 2019-12-03 08:52:09
问题 I am trying to make a List of all of the books in one Collection that are not present in another. My problem is that I need to compare based on book ID, so I can't just test to see whether a book in the first is contained in the second, I have to determine whether any book in the second collection has the same ID as a book in the first. I have the below code to compare two collections of books and filter the first collection: List<Book> parentBooks = listOfBooks1.stream().filter(book->

Java 8 Stream, How to get Top N count? [closed]

穿精又带淫゛_ 提交于 2019-12-03 08:39:55
I need your advice to simplify this code below. I have a player list with an ID of the games won. I want to extract the 2 best players from this list (the 2 players who have a better amount of match Id) Once extracted, I have to return the initial list to do other operations. I think it is possible to improve this code in terms of optimization or reading. If you can help me. public class PlayerStatistics { int id String name; int idMatchWon; // key from Match // getter , setter } public static void main(String[] args) throws Exception { List<PlayerStatistics> _players = new ArrayList

Java 8 stream objects significant memory usage

纵然是瞬间 提交于 2019-12-03 08:36:01
问题 In looking at some profiling results, I noticed that using streams within a tight loop (used instead of another nested loop) incurred a significant memory overhead of objects of types java.util.stream.ReferencePipeline and java.util.ArrayList$ArrayListSpliterator . I converted the offending streams to foreach loops, and the memory consumption decreased significantly. I know that streams make no promises about performing any better than ordinary loops, but I was under the impression that the

Stream groupingBy: reducing to first element of list

烈酒焚心 提交于 2019-12-03 08:22:11
问题 I have a List<Valuta> which can be represented (simplified) JSON-style: [ { codice=EUR, description=Euro, ratio=1 }, { codice=USD, description=Dollars, ratio=1.1 } ] I want to transform that in a Map<String, Valuta> like this: { EUR={ codice=EUR, description=Euro, ratio=1 }, USD={ codice=USD, description=Dollars, ratio=1.1 }} I wrote this one-liner: getValute().stream().collect(Collectors.groupingBy(Valuta::getCodice)); but this returns a Map<String, List<Valuta>> instead of what I need. I

When should I use IntStream.range in Java?

微笑、不失礼 提交于 2019-12-03 08:12:55
问题 I would like to know when I can use IntStream.range effectively. I have three reasons why I am not sure how useful IntStream.range is. (Please think of start and end as integers.) If I want an array, [start, start+1, ..., end-2, end-1] , the code below is much faster. int[] arr = new int[end - start]; int index = 0; for(int i = start; i < end; i++) arr[index++] = i; This is probably because toArray() in IntStream.range(start, end).toArray() is very slow. I use MersenneTwister to shuffle

Finite generated Stream in Java - how to create one?

血红的双手。 提交于 2019-12-03 08:09:05
问题 In Java, one can easily generate an infinite stream with Stream.generate(supplier) . However, I would need to generate a stream that will eventually finish. Imagine, for example, I want a stream of all files in a directory. The number of files can be huge, therefore I can not gather all the data upfront and create a stream from them (via collection.stream() ). I need to generate the sequence piece by piece. But the stream will obviously finish at some point, and terminal operators like (

Java 8 Stream vs Collection Storage

旧街凉风 提交于 2019-12-03 07:45:46
问题 I have been reading up on Java 8 Streams and the way data is streamed from a data source, rather than have the entire collection to extract data from. This quote in particular I read on an article regarding streams in Java 8. "No storage. Streams don't have storage for values; they carry values from a source (which could be a data structure, a generating function, an I/O channel, etc) through a pipeline of computational steps." From the source: http://www.drdobbs.com/jvm/lambdas-and-streams

Extract duplicate objects from a List in Java 8

天大地大妈咪最大 提交于 2019-12-03 07:31:46
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.firstName = builder.firstName; this.secondName = builder.secondName; } public Long getId() { return id; }

Java 8 Stream API toMap converting to TreeMap

扶醉桌前 提交于 2019-12-03 07:14:33
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 User{ ... private List<Message> sendMessages; ... public List<Message> getSendMessages() { return