java-stream

Register a Stream “completion” hook

别等时光非礼了梦想. 提交于 2019-12-18 01:33:41
问题 Using the Java 8 Stream API, I would like to register a "completion hook", along the lines of: Stream<String> stream = Stream.of("a", "b", "c"); // additional filters / mappings that I don't control stream.onComplete((Completion c) -> { // This is what I'd like to do: closeResources(); // This might also be useful: Optional<Throwable> exception = c.exception(); exception.ifPresent(e -> throw new ExceptionWrapper(e)); }); The reason why I want to do that is because I want to wrap a resource in

terminate or break java 8 stream loop [duplicate]

两盒软妹~` 提交于 2019-12-17 23:42:29
问题 This question already has answers here : Limit a stream by a predicate (19 answers) Closed 5 years ago . I have a java 8 stream loop with the following content: void matchSellOrder(Market market, Order sellOrder) { System.out.println("selling " + market.pair() + " : " + sellOrder); market.buyOrders() .stream() .filter(buyOrder -> buyOrder.price >= sellOrder.price) .sorted(BY_ASCENDING_PRICE) .forEach((buyOrder) -> { double tradeVolume = Math.min(buyOrder.quantity, sellOrder.quantity); double

Stream from two dimensional array in java

烂漫一生 提交于 2019-12-17 23:17:14
问题 I am trying to get an IntStream out of an n dimensional int arrays. Is there a nice API way to do it? I know the concatenate method for two streams. 回答1: Assuming you want to process array of array sequentially in row-major approach, this should work: int[][] arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; IntStream stream = Arrays.stream(arr).flatMapToInt(x -> Arrays.stream(x)); First it invokes the Arrays.stream(T[]) method, where T is inferred as int[] , to get a Stream<int[]> , and then Stream

When should streams be preferred over traditional loops for best performance? Do streams take advantage of branch-prediction?

泪湿孤枕 提交于 2019-12-17 22:35:52
问题 I just read about Branch-Prediction and wanted to try how this works with Java 8 Streams. However the performance with Streams is always turning out to be worse than traditional loops. int totalSize = 32768; int filterValue = 1280; int[] array = new int[totalSize]; Random rnd = new Random(0); int loopCount = 10000; for (int i = 0; i < totalSize; i++) { // array[i] = rnd.nextInt() % 2560; // Unsorted Data array[i] = i; // Sorted Data } long start = System.nanoTime(); long sum = 0; for (int j =

Can I duplicate a Stream in Java 8?

前提是你 提交于 2019-12-17 22:34:47
问题 Sometimes I want to perform a set of operations on a stream, and then process the resulting stream two different ways with other operations. Can I do this without having to specify the common initial operations twice? For example, I am hoping a dup() method such as the following exists: Stream [] desired_streams = IntStream.range(1, 100).filter(n -> n % 2 == 0).dup(); Stream stream14 = desired_streams[0].filter(n -> n % 7 == 0); // multiples of 14 Stream stream10 = desired_streams[1].filter(n

What is the difference between intermediate and terminal operations?

最后都变了- 提交于 2019-12-17 22:21:16
问题 can someone tell me What is the difference between intermediate and terminal operations for Stream ? Stream operations are combined into pipelines to process streams. All operations are either intermediate or terminal ..means?. 回答1: A Stream supports several operations and these operations are divided into intermediate and terminal operations. The distinction between this operations is that an intermediate operation is lazy while a terminal operation is not. When you invoke an intermediate

Java 8 Stream API to find Unique Object matching a property value

瘦欲@ 提交于 2019-12-17 22:13:07
问题 Find the object matching with a Property value from a Collection using Java 8 Stream. List<Person> objects = new ArrayList<>(); Person attributes -> Name, Phone, Email. Iterate through list of Persons and find object matching email. Saw that this can be done through Java 8 stream easily. But that will still return a collection? Ex: List<Person> matchingObjects = objects.stream. filter(p -> p.email().equals("testemail")). collect(Collectors.toList()); But I know that it will always have one

Using a semaphore inside a nested Java 8 parallel stream action may DEADLOCK. Is this a bug?

左心房为你撑大大i 提交于 2019-12-17 22:08:47
问题 Consider the following situation: We are using a Java 8 parallel stream to perform a parallel forEach loop, e.g., IntStream.range(0,20).parallel().forEach(i -> { /* work done here */}) The number of parallel threads is controlled by the system property "java.util.concurrent.ForkJoinPool.common.parallelism" and usually equal to the number of processors. Now assume that we like to limit the number of parallel executions for a specific piece of work - e.g. because that part is memory intensive

How to create Flux from Mono

ε祈祈猫儿з 提交于 2019-12-17 20:05:30
问题 I have a Mono A. The Object A contains two lists. I want to create direct two Flux. Is this possible without block()? Mono<A> a = ... ; Flux<AList1> a1 = Flux.fromIterable(a.block().getList1()); 回答1: Use Mono.flatMapMany() method: Flux flux1 = mono.map(A::getList1).flatMapMany(Flux::fromIterable); Flux flux2 = mono.map(A::getList2).flatMapMany(Flux::fromIterable); 来源: https://stackoverflow.com/questions/49190668/how-to-create-flux-from-mono

Java 8 stream join and return multiple values

孤街浪徒 提交于 2019-12-17 19:39:31
问题 I'm porting a piece of code from .NET to Java and stumbled upon a scenario where I want to use stream to map & reduce. class Content { private String propA, propB, propC; Content(String a, String b, String c) { propA = a; propB = b; propC = c; } public String getA() { return propA; } public String getB() { return propB; } public String getC() { return propC; } } List<Content> contentList = new ArrayList(); contentList.add(new Content("A1", "B1", "C1")); contentList.add(new Content("A2", "B2",