java-stream

How should I check whether a Stream<T> is sorted?

穿精又带淫゛_ 提交于 2019-12-04 17:36:38
问题 With an Iterable<T> , it's easy: T last = null; for (T t : iterable) { if (last != null && last.compareTo(t) > 0) { return false; } last = t; } return true; But I can't think of a clean way to do the same thing for a Stream<T> that avoids consuming all the elements when it doesn't have to. 回答1: There are several methods to iterate over the successive pairs of the stream. For example, you can check this question. Of course my favourite method is to use the library I wrote: boolean unsorted =

Will parallel stream work fine with distinct operation?

青春壹個敷衍的年華 提交于 2019-12-04 17:16:36
问题 I was reading about statelessness and came across this in doc: Stream pipeline results may be nondeterministic or incorrect if the behavioral parameters to the stream operations are stateful. A stateful lambda (or other object implementing the appropriate functional interface) is one whose result depends on any state which might change during the execution of the stream pipeline. Now if I have the a list of string ( strList say) and then trying to remove duplicate strings from it using

How to reuse application of filter & map on a Stream?

ⅰ亾dé卋堺 提交于 2019-12-04 17:08:48
问题 I have a set of domain objects that inherit from a shared type (i.e. GroupRecord extends Record , RequestRecord extends Record ). The subtypes have specific properties (i.e. GroupRecord::getCumulativeTime , RequestRecord::getResponseTime ). Further, I have a list of records with mixed subtypes as a result of parsing a logfile. List<Record> records = parseLog(...); In order to compute statistics on the log records, I want to apply math functions only on a subset of the records that matches a

Join a list of object's properties into a String

China☆狼群 提交于 2019-12-04 16:30:25
问题 I'm learning lambda right now, and I wonder how can I write this code by a single line with lambda. I have a Person class which includes an ID and name fields Currently, I have a List<Person> which stores these Person objects. What I want to accomplish is to get a string consisting of person's id just like. "id1,id2,id3" . How can I accomplish this with lambda? 回答1: To retrieve a String consisting of all the ID's separated by the delimiter "," you first have to map the Person ID's into a new

Convert Map of maps into Lists using Java 8 Streams

六月ゝ 毕业季﹏ 提交于 2019-12-04 16:15:09
I have a map: Map<String, Map<Integer, List<Integer>>> e.g. Map<Name, Map<Id, List<ReferenceId>>> Outcome: List<Id> List<ReferenceId> I wanna convert this map into two list of Integers. One list contains inner-map keys, and other contains inner-map value (i.e. List<Integer> ) Can anyone tell me how to do this in Java 8 using streams? I tried this way but got Cast Exception, can not convert String to Integer. map.values().stream() .map(m -> m.entrySet() .stream() .map(e -> e.getKey()) .collect(Collectors.toList())) .flatMap(l -> l.stream()) .collect(Collectors.toList()); Map<String, Map<Integer

How to iterate nested lists with lambda streams?

﹥>﹥吖頭↗ 提交于 2019-12-04 16:09:32
问题 I'm trying to refactor the following code to lambda expressions with `stream, especially the nested foreach loops: public static Result match (Response rsp) { Exception lastex = null; for (FirstNode firstNode : rsp.getFirstNodes()) { for (SndNode sndNode : firstNode.getSndNodes()) { try { if (sndNode.isValid()) return parse(sndNode); //return the first match, retry if fails with ParseException } catch (ParseException e) { lastex = e; } } } //throw the exception if all elements failed if

How can I use Java 8 Streams with an InputStream?

左心房为你撑大大i 提交于 2019-12-04 16:06:59
问题 I would like to wrap a java.util.streams.Stream around an InputStream to process one Byte or one Character at a time. I didn't find any simple way of doing this. Consider the following exercise: We wish to count the number of times each letter appears in a text file. We can store this in an array so that tally[0] will store the number of times a appears in the file, tally[1] stores the number of time b appears and so on. Since I couldn't find a way of streaming the file directly, I did this:

Turn linked Objects into Stream or Collection

六眼飞鱼酱① 提交于 2019-12-04 15:56:36
问题 I want to iterate over a stacktrace. The stacktrace consists of throwables whose getCause() returns the next throwable. The last call to getCause() returns null. (Example: a -> b -> null) I've tried to use Stream.iterable() which results in a NullPointerException, since the elements in the iterable can't be null. Here is a short demonstration of the problem: public void process() { Throwable b = new Throwable(); Throwable a = new Throwable(b); Stream.iterate(a, Throwable::getCause).forEach

Java 8/9: Can a character in a String be mapped to its indices (using streams)?

巧了我就是萌 提交于 2019-12-04 15:53:42
问题 Given a String s and a char c , I'm curious if there exists some method of producing a List<Integer> list from s (where the elements within list represent the indices of c within s ). A close, but incorrect approach would be: public static List<Integer> getIndexList(String s, char c) { return s.chars() .mapToObj(i -> (char) i) .filter(ch -> ch == c) .map(s::indexOf) // Will obviously return the first index every time. .collect(Collectors.toList()); } The following inputs should yield the

How to make a Stream from a DirectoryStream

久未见 提交于 2019-12-04 15:47:15
问题 When reading the API for DirectoryStream I miss a lot of functions. First of all it suggests using a for loop to go from stream to List . And I miss the fact that it a DirectoryStream is not a Stream . How can I make a Stream<Path> from a DirectoryStream in Java 8? 回答1: DirectoryStream is not a Stream (it's been there since Java 7, before the streams api was introduced in Java 8) but it implements the Iterable<Path> interface so you could write: try (DirectoryStream<Path> ds = ...) { Stream