java-stream

JUnit assertions within in Java 8 stream

喜夏-厌秋 提交于 2019-12-23 09:57:13
问题 Say I have three objects that I save to the database and set the db generated ID into. I don't know the order of the objects returned from the method saveToDb . But I want to junit test that those generated IDs are there. How do I do that within in stream? I want to do something like this: List<MyObject> myObjects = getObjects(); numRecords = saveToDb(myObjects); // numRecords=3 List<Integer> intArray = Arrays.asList(1, 2, 3); intArray.stream() .forEach(it -> myObjects.stream() .filter(it2 ->

Java lambda sublist

旧街凉风 提交于 2019-12-23 09:42:36
问题 What is the shortest way to express "get new List B from List A where condition" via a Java 8 lambda? Say I have List<Integer> a = Arrays.asList(1, 2, 3, 4, 5) and I want a new List, B, where the value is > 3. I've read through the new Collections Streams API, but I'm not convinced I have found the best way to do this, and don't want to taint the question with what is probably my less than perfect solution. 回答1: a.stream().filter(x -> x > 3).collect(Collectors.toList()); 来源: https:/

Quickly degrading stream throughput with chained operations?

ε祈祈猫儿з 提交于 2019-12-23 09:25:57
问题 I expected that simple intermediate stream operations, such as limit() , have very little overhead. But the difference in throughput between these examples is actually significant: final long MAX = 5_000_000_000L; LongStream.rangeClosed(0, MAX) .count(); // throughput: 1.7 bn values/second LongStream.rangeClosed(0, MAX) .limit(MAX) .count(); // throughput: 780m values/second LongStream.rangeClosed(0, MAX) .limit(MAX) .limit(MAX) .count(); // throughput: 130m values/second LongStream

Difference between parallel stream and CompletableFuture

こ雲淡風輕ζ 提交于 2019-12-23 09:03:40
问题 In the book "Java 8 in action" (by Urma, Fusco and Mycroft) they highlight that parallel streams internally use the common fork join pool and that whilst this can be configured globally, e.g. using System.setProperty(...), that it is not possibly to specify a value for a single parallel stream. I have since seen the workaround that involves running the parallel stream inside a custom made ForkJoinPool. Later on in the book, they have an entire chapter dedicated to CompletableFuture, during

java 8 nested streams

╄→尐↘猪︶ㄣ 提交于 2019-12-23 07:00:09
问题 Suppose you have structure classes like this: public class Review{ private Integer idReview; private String description; private ArrayList<RelReviewImage> images; } public class RelReviewImage{ private Integer idRelReviewImage; private Integer idImage; private String name; } With Java 8 and Stream s we want to do a filter for idImage and return Review objects. Is it possible? One level is easy, but 2 levels we can't find any example or documentation. 回答1: Guess what you need: (Assume getters

No methods matching the name(s) stream in hierarchy of class java.util.Arrays$ArrayList

早过忘川 提交于 2019-12-23 05:27:37
问题 So I am using java 8 and trying to write some tests with PowerMock and Mockito. I am getting a MethodNotFoundException with the message: No methods matching the name(s) stream were found in the class hierarchy of class java.util.Arrays$ArrayList. I double checked the ArrayList documentation and it definitely looks like it inherits stream from Collections. Is this a problem with PowerMockito or am I missing something? Line in question PowerMockito.when(thing.call("services", "things"))

Parsing a CSV file for a multiple row rows using new Java 8 Streams API

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 04:51:47
问题 I asked a question some time back about parsing CSV files for a single matching row. In the example shown below, I use a bufferedreader to read the header row as the first step. Using this row, I parse the column names and then proceed to search for matching rows. The filter criteria I need to search for the matching row should be based on 2 column values, instead the code shown below only returns the 1 row - presumably because I use .findFirst().get(); Instead I need something along the

How to design a returned stream that may use skip

假如想象 提交于 2019-12-23 04:13:16
问题 I have created a parsing library that accepts a provided input and returns a stream of Records. A program then calls this library and processes the results. In my case, my program is using something like recordStream.forEach(r -> insertIntoDB(r)); One of the types of input that can be provided to the parsing library is a flat file, which may have a header row. As such, the parsing library can be configured to skip a header row. If a header row is configured, it adds a skip(n) element to the

Java-8 stream expression to 'OR' several enum values together

我只是一个虾纸丫 提交于 2019-12-23 03:22:08
问题 I am aggregating a bunch of enum values (different from the ordinal values) in a foreach loop. int output = 0; for (TestEnum testEnum: setOfEnums) { output |= testEnum.getValue(); } Is there a way to do this in streams API? If I use a lambda like this in a Stream<TestEnum> : setOfEnums.stream().forEach(testEnum -> (output |= testEnum.getValue()); I get a compile time error that says, 'variable used in lambda should be effectively final'. 回答1: Predicate represents a boolean valued function,

Why does IntStream.range(0, 100000).parallel().foreach take longer then normal for loop

二次信任 提交于 2019-12-23 01:12:20
问题 I am just starting to learn about the Streams and parallel in Java and I was wondering why a normal for loop takes less time than IntStream paralleled at adding items to an array. package parallel; import java.util.stream.IntStream; public class Parallel { public static void main(String[] args) { final int[] intArray = new int[100000]; long startTime = System.currentTimeMillis(); IntStream.range(0, 100000).parallel().forEach(i -> intArray[i]=i); long endTime = System.currentTimeMillis();