java-stream

Removing overloaded method in Java

旧城冷巷雨未停 提交于 2019-12-10 18:22:33
问题 There are 2 overloaded methods. Each of these methods converts a list of one type to a list of a different type. But the first method uses a comparator. class SomeClass { public static <T, G> List<G> toListOfNewType(List<T> inputList, Function<T, G> mapperFunction, Comparator<? super G> comparator) { return Stream.ofNullable(inputList) .flatMap(List::stream) .map(mapperFunction) .sorted(comparator) .collect(Collectors.toList()); } public static <T, G> List<G> toListOfNewType(List<T> inputList

java8 Collectors.toMap() limitation?

筅森魡賤 提交于 2019-12-10 17:33:55
问题 I am trying to use java8's Collectors.toMap on a Stream of ZipEntry . It may not be the best idea because of possible exceptions occuring during the processing, but I guess it ought to be possible. I am now getting a compile error (type inference engine I guess) which I don't understand. Here's some extracted demo code: import java.io.IOException; import java.util.Map; import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class

java 8 local variable in stream.foreach [duplicate]

China☆狼群 提交于 2019-12-10 17:29:31
问题 This question already has answers here : Variable used in lambda expression should be final or effectively final (6 answers) Closed last year . I would like to use local variables in lambda functions but I get error: Please see the 1. and 2. points in the code. class Foo { int d = 0; // 1. It compiles, but ugly, 2. doesnt compile public void findMax(List<List<Route>> routeLists) { int d = 0; // 2.Error : Local variable dd defined in an enclosing scope must be final or effectively final

Primitive stream vs object stream and actual boxing that occurs

故事扮演 提交于 2019-12-10 17:23:23
问题 So I understand you can have object streams, i.e. Stream<T> and specialist primitive streams, e.g. IntStream , DoubleStream , etc. One of the benefits of the latter is to avoid autoboxing. Also if we take IntStream as an example, it has specialised operations such as filter that accepts an IntPredicate . I wondered if I had an IntStream vs a Stream<Integer> at which point you're saving on boxing, e.g. intstream.filter(x -> x >= 50).forEach(x -> System.out.println(x)); vs stream.filter(x -> x

A simple list.parallelStream() in java 8 stream does not seem to do work stealing?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 16:43:35
问题 From this question " Will inner parallel streams be processed fully in parallel before considering parallelizing outer stream?", I understood that streams perform work-stealing. However, I've noticed that it often doesn't seem to occur. For example, if I have a List of say 100,000 elements and I attempt to process it in parallelStream() fashion, I often notice towards the end that most of my CPU cores are sitting idle in the "waiting" state. (Note: Of the 100,000 elements in the list, some

Ending an “infinite” stream when certain conditions are met

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 16:09:53
问题 I'm trying to pull data from a REST-style web-service which delivers content in pages. The only way I can know that I've reached the end is when I ask for a page and there are no results. I'd like to terminate the stream at that time. I've written the following Java code. The first function pulls a single page from the web-service and returns it as a stream. The second function flatmaps the streams together into a single stream. public Stream<ApplicationResponse> getApplications(String token,

Is Android N Stream API backported to lower versions?

情到浓时终转凉″ 提交于 2019-12-10 16:08:49
问题 Latest version of Android N has stream package but when i'm trying to use it is saying min Sdk version which i should use is 24 (N) and my min SDK is 16. I enabled JackOptions with which Lambda expressions , Method References are working fine ( even for versions less than N minsdk) but still has problem is only with Stream package . The link below https://developer.android.com/guide/platform/j8-jack.html says lambda, method references are available to 23 n lower also but no clarity on Stream

Java 8 Filter Map<String,List<Employee>>

夙愿已清 提交于 2019-12-10 13:58:06
问题 How to filter a Map<String, List<Employee>> using Java 8 Filter? I have to filter only when any of employee in the list having a field value Gender = "M" . Input: Map<String,List<Employee>> Output: Map<String,List<Employee>> Filter criteria: Employee.genter = "M" Also i have to filter out the key in the output map (or filter map [new map we get after filter]) if the List<> is empty on the map value 回答1: To filter out entries where a list contains an employee who is not of the "M" gender: Map

Getting two different outputs from a Stream

我只是一个虾纸丫 提交于 2019-12-10 13:49:17
问题 I am testing out the new Stream API in java-8 and want to check the outcome of 10000 random coinflips. So far I have: public static void main(String[] args) { Random r = new Random(); IntStream randomStream = r.ints(10000,0, 2); System.out.println("Heads: " + randomStream.filter(x -> x==1).count()); System.out.println("Tails: " + randomStream.filter(x -> x==0).count()); } but this throws the exception: java.lang.IllegalStateException: stream has already been operated upon or closed I

java streams: straightforward reduce

跟風遠走 提交于 2019-12-10 13:16:35
问题 I've a stream of MetricGroup , where: public class MetricGroup { private int uploadedDocs; private long uploadedKbs; // getters and setters } I need to sumarize all metrics in one single metric. I mean, I need to add all metric.uploadedDocs into a sumMetric.uploadedDocs and metric.uploadedKds into a sumMetric.uploadedKbs . I figure out I need some kind of reduce Stream.of(new MetricGroup(1,100), new MetricGroup(1,200)) .reduce(????); Any ideas? 回答1: You can use this overload of reduce: T