java-stream

How to make stream pipeline simpler

你。 提交于 2019-12-04 22:05:02
I think my code needs improvement. I use object allSummaryTSTLog both in the stream's filter() and map() stages, so I have to call File.listFiles twice: public static List<Test> ParserPath(List<String> allLogPath) { FilenameFilter filter = new MyFilter("Summary_TSTLog"); return allLogPath.parallelStream().filter(path -> { File testPath = new File(path); if (!testPath.isDirectory()) { MyLog.log.info("test path : [" + path + "] is not exist, continue"); return false; } File[] allSummaryTSTLog = testPath.listFiles(filter); if (allSummaryTSTLog == null || allSummaryTSTLog.length == 0) { MyLog.log

Java 8 adding values of multiple property of an Object List

爷,独闯天下 提交于 2019-12-04 20:52:33
问题 Lets say I have a class below with getters and setters but with only default constructor. Note: I'm not allowed to change the structure of this class. class Target { private String year; private String month; private String name; private double target; private double achieved; public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getMonth() { return month; } public void setMonth(String month) { this.month = month; } public String getName

Getting only required objects from a list using Java 8 Streams

徘徊边缘 提交于 2019-12-04 20:10:15
问题 Consider a Parent class with the attributes attrib1 , attrib2 and List<Child> child with its corresponding getters and setters. The Child is another class with five attributes attrib1 - attrib5 with its corresponding getters and setters. Now I created a List<Parent> parent. Then I want to filter out a List<Parent> with following condition:- Child.Attrib1 > 10 ; So I created the following query by Java 8 streams. parent.stream().filter(e -> e.getChild().stream().anyMatch(c -> c.getAttrib1() >

Group, Collectors, Map (Int to String), Map (Map to Object)

喜你入骨 提交于 2019-12-04 19:53:10
This is a continuation of my previous question at Group, Sum byType then get diff using Java streams . As suggested, I should post as a separate thread instead of updating the original one. So with my previous set of question, I have achieved that, and now, with the continuation. Background: I have the following dataset 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

How to short-circuit reduce on Stream?

孤街醉人 提交于 2019-12-04 19:37:19
问题 Suppose I have a stream of boolean values and the reduce operation that I am writing is || (OR). Can I write it in a way such that the evaluation of at least some of the elements is abandoned if a true value is encountered? I am looking for some amount of optimization (perhaps if it is a parallel stream), not necessarily full optimization although the latter would be awesome. 回答1: I suspect you want this type of construct. // stop when any element evaluates to true boolean any = stream

Closing mapped streams - what's the idea?

吃可爱长大的小学妹 提交于 2019-12-04 19:31:11
问题 It's well known that Javadoc says about Stream interface: Streams have a BaseStream.close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned by Files.lines(Path, Charset)) will require closing . Most streams are backed by collections, arrays, or generating functions, which require no special resource management. (If a stream does require closing, it can

How collectors are used when turning the stream in parallel

两盒软妹~` 提交于 2019-12-04 18:57:44
问题 I actually tried to answer this question How to skip even lines of a Stream<String> obtained from the Files.lines. So I though this collector wouldn't work well in parallel: private static Collector<String, ?, List<String>> oddLines() { int[] counter = {1}; return Collector.of(ArrayList::new, (l, line) -> { if (counter[0] % 2 == 1) l.add(line); counter[0]++; }, (l1, l2) -> { l1.addAll(l2); return l1; }); } but it works. EDIT: It didn't actually work; I got fooled by the fact that my input set

Why do we have to cast the List returned by Collectors.toList() to List<Integer> even though the elements of the Stream are already mapped to Integer? [duplicate]

落爺英雄遲暮 提交于 2019-12-04 18:56:33
This question already has an answer here: What is a raw type and why shouldn't we use it? 15 answers Why does the Streams API need a hint for generic type in this case? 1 answer Why does javac complain about generics unrelated to the class' type arguments? [duplicate] 1 answer I'm mapping a raw Stream to a Stream<Integer> and then collect the elements to a List<Integer> . Why do I have to cast the result of collect(Collectors.toList()) to List<Integer> if my mapper - .map(str -> ((String)str).length()) - already maps to Integer ? My code: List list = Arrays.asList("test", "test2"); List

Grouping by List of Map in Java 8

允我心安 提交于 2019-12-04 18:07:29
问题 I have a List like this: List<Map<String, Long>> Is there a way, using lambda, to convert this list to: Map<String, List<Long>> Example: Map<String, Long> m1 = new HashMap<>(); m1.put("A", 1); m1.put("B", 100); Map<String, Long> m2 = new HashMap<>(); m2.put("A", 10); m2.put("B", 20); m2.put("C", 100); List<Map<String, Long>> beforeFormatting = new ArrayList<>(); beforeFormatting.add(m1); beforeFormatting.add(m2); After formatting: Map<String, List<Long>> afterFormatting; which would look like

Java Stream Collectors.toMap value is a Set

倾然丶 夕夏残阳落幕 提交于 2019-12-04 17:44:16
问题 I want to use a Java Stream to run over a List of POJOs, such as the list List<A> below, and transform it into a Map Map<String, Set<String>> . For example, class A is: class A { public String name; public String property; } I wrote the code below that collects the values into a map Map<String, String> : final List<A> as = new ArrayList<>(); // the list as is populated ... // works if there are no duplicates for name final Map<String, String> m = as.stream().collect(Collectors.toMap(x -> x