java-stream

Java 8 turn on parallel() stream with boolean? [closed]

為{幸葍}努か 提交于 2019-12-08 01:04:22
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . I am wondering how I could design methods that could either be run concurrently or single threaded. For example I have a method like this: /** * Produces the norm of the two vector {@code v1}. * * @param v1 * The first vector. * * @param v2 * The second vector * * @throws

Java8 stream lines and aggregate with action on terminal line

不羁的心 提交于 2019-12-08 00:44:17
问题 Question How to execute an action after the last item of an ordered stream is processed but before it's closed ? This Action should be able to inject zero or more items in the stream pipe. Context I've got a very large file of the form : MASTER_REF1 SUBREF1 SUBREF2 SUBREF3 MASTER_REF2 MASTER_REF3 SUBREF1 ... Where SUBREF (if any) is applicable to MASTER_REF and both are complex objects (you can imagine it somewhat like JSON). On first look I tried something like : public void process(Path

How do you build an infinite repeating stream from a finite stream in Java 8?

一个人想着一个人 提交于 2019-12-08 00:13:19
问题 How can I turn a finite Stream of things Stream<Thing> into an infinite repeating stream of things? 回答1: Boris the Spider is right: a Stream can only be traversed once, so you need a Supplier<Stream<Thing>> or you need a Collection. <T> Stream<T> repeat(Supplier<Stream<T>> stream) { return Stream.generate(stream).flatMap(s -> s); } <T> Stream<T> repeat(Collection<T> collection) { return Stream.generate(() -> collection.stream()).flatMap(s -> s); } Example invocations: Supplier<Stream<Thing>>

Java 8 Stream (based on resource) .iterator() that auto-closes the resource?

余生颓废 提交于 2019-12-07 23:05:28
问题 Does Java 8 Stream.iterator() auto-close the stream when it's done? I suppose not... I have something like this: class Provider implements Serializable { Iterator<String> iterator() { Stream<String> stream = new BufferedReader(...).lines(); return stream.iterator(); } } This iterator is used by some other class that doesn't know the iterator is based on a file-reading resource. That is class Consumer { void f() { Iterator<String> iterator = provider.iterator(); // code that calls iterator

Java 8 Stream Filter - Sort based pdate

与世无争的帅哥 提交于 2019-12-07 22:59:59
问题 Am trying to sort the filed in filter. Input Document / Sample Record: DocumentList: [ Document{ { _id=5975ff00a213745b5e1a8ed9, u_id=, mailboxcontent_id=5975ff00a213745b5e1a8ed8, idmapping=Document{ {ptype=PDF, cid=00988, normalizedcid=00988, systeminstanceid=, sourceschemaname=, pid=0244810006} }, batchid=null, pdate=Tue Jul 11 17:52:25 IST 2017, locale=en_US } }, Document{ { _id=597608aba213742554f537a6, u_id=, mailboxcontent_id=597608aba213742554f537a3, idmapping=Document{ {platformtype

Inversion sum is not correct using Stream.reduce

淺唱寂寞╮ 提交于 2019-12-07 21:24:56
问题 Inversion sum is not correct using Stream.reduce, what is going wrong here ? double[] array = {1.0, 2.0}; double iunversionSum = Arrays.stream(array).reduce(0.0, (a, b) -> Double.sum(1.0 / a, 1.0 / b)); output is .5 but expected is 1.5 (1/1 + 1/2) 回答1: I think using map() it could be simplier. double inversionSum = Arrays.stream(arr).map(val -> 1 / val).sum(); 回答2: The error in your reduce is: Double.sum(1.0 / a, 1.0 / b), starting the series with 0.0. Now check why your outcome is .5. Use

How to include elements from unmodified list in modified list with Java Streams?

匆匆过客 提交于 2019-12-07 21:13:55
问题 I am trying to do a map operation on a List<Integer> : list.stream().map(i -> i - 2).collect(Collectors.toList()); Instead of performing the operation on the last element of the list, I would like it to just be passed through. ...Collectors.toList()).add(i) doesn't work, of course, because i is out of scope. For example, the input list [1, 2, 3, 4] should output [-1, 0, 1, 4] 回答1: You could stream the original list and limit the stream to exclude the last element, do the mapping and collect

Performing an operation over the elements of one collection and iterating over the result to perform some other operation

Deadly 提交于 2019-12-07 20:45:01
问题 If I have 2 collections, List<String> domainArr; List<Person> personArr; I would like to make a minor transformation on each of the elements in the String and then iterate over the personArr to List<String> urlArr = strArr.stream() .map(str -> "https://" + strArr) .collect(Collectors.toList()); I have a method like List<Person> getPersons(String url){ /*makes a restful call to the url and gets a List of objects for each URL.*/ } I would like to iterate over each of the urls from urlArr and

Construct a Map using object reference as key using Streams API and Collectors.toMap()

删除回忆录丶 提交于 2019-12-07 20:03:45
问题 I want to construct a Map<Item, List<String>> , i.e. using the Item reference as key, with some arbitrary List<String> as value. I've attempted the following, but it will show type inference error in IntelliJ (however, it will still compile ); List<Item> items = getItems(...); Map<Item, List<String>> itemMap = items.stream().collect(Collectors.toMap(i->i, new ArrayList<>())); I then created a workaround through a helper method in Item , get() , which just returns its own instance; public Item

How can I find the largest M numbers from N numbers in Java 8?

℡╲_俬逩灬. 提交于 2019-12-07 18:12:07
问题 IntStream may be a the easiest way but I can only pick up smallest M numbers as below: public class Test { private static final int[] arr = {5, 3, 4, 2, 9, 1, 7, 8, 6}; public static void main(String[] args) throws Exception { System.out.println(Arrays.asList(IntStream.of(arr).sorted().limit(5).boxed().toArray())); } } btw, considering algorithm complexity and assuming N >> M, a "sorted + limit" approach just have a complexity of O(N log(N)). I think the best complexity may reach to O(N log(M