java-stream

How to sort an IntStream in reverse order

此生再无相见时 提交于 2019-12-09 05:25:19
问题 I'm reading in numbers from a .txt file using BufferedReader . I want to reverse the order of elements in this steam so that when they are collected they will be arranged from the highest to the lowest. I don't want to sort after the array has been built because I have no idea how many elements might be in it, I only need the highest N elements. in = new BufferedReader(reader); int[] arr = in.lines() .mapToInt(Integer::parseInt) .sorted() .limit((long) N) .toArray(); 回答1: Try negating the

Concatenating two int[]

徘徊边缘 提交于 2019-12-09 05:04:24
问题 There are easy solutions for concatenating two String[] or Integer[] in java by Streams . Since int[] is frequently used. Is there any straightforward way for concatenating two int[] ? Here is my thought: int[] c = {1, 34}; int[] d = {3, 1, 5}; Integer[] cc = IntStream.of(c).boxed().toArray(Integer[]::new); Integer[] dd = Arrays.stream(d).boxed().toArray(Integer[]::new); int[] m = Stream.concat(Stream.of(cc), Stream.of(dd)).mapToInt(Integer::intValue).toArray(); System.out.println(Arrays

Why does stream api is not designed for Exception handling?

最后都变了- 提交于 2019-12-09 04:57:20
问题 Fixtures BiConsumer<Exception, Consumer<? super Integer>> NOTHING = (ex, unused) ->{/**/}; When I try to fix the bug that is reported by @Holger in this answer: Stream<Integer> stream = Stream.of(1, 2, 3); // v--- the bug I have already fixed, it will throws RuntimeException exceptionally(stream, NOTHING).collect(ArrayList::new, (l, x) -> { l.add(x); if (x < 4) throw new RuntimeException(); }, List::addAll); Everything is ok but when using Stream.of(T) the map(...) operation will be invoked

Java 8: IntStream to Integer[]

核能气质少年 提交于 2019-12-09 03:05:47
问题 I am writing simple program which will eventually plot the run times of various sorting algorithms written in Java. The general interface of a sorting algorithm is via a method: public void sort(Comparable[] xs) I am attempting to use Java 8's stream mechanism to generate random test cases along the following lines: public static IntStream testCase(int min, int max, int n) { Random generator = new Random(); return generator.ints(min, max).limit(n); } My question is, how can I convert an

How to map RuntimeExceptions in Java streams to “recover” from invalid stream elements

家住魔仙堡 提交于 2019-12-08 23:32:39
问题 Imagine I'm building a library, that will receive a Stream of Integers, and all the library code needs to do is return a stream of Strings with the string representation of the number. public Stream<String> convertToString(Stream<Integer> input) { return input.map(n -> String.valueOf(n)); } However, imagine someone decides to invoke my function with the following code: Stream<Integer> input = Stream.of(1,2,3) .map(n -> { if (n %3 ) { throw new RuntimeException("3s are not welcome here!"); }

Why does Collection.parallelStream() exist when .stream().parallel() does the same thing?

故事扮演 提交于 2019-12-08 22:40:34
问题 In Java 8, the Collection interface was extended with two methods that return Stream<E> : stream() , which returns a sequential stream, and parallelStream() , which returns a possibly-parallel stream. Stream itself also has a parallel() method that returns an equivalent parallel stream (either mutating the current stream to be parallel or creating a new stream). The duplication has obvious disadvantages: It's confusing. A question asks whether calling both parallelStream().parallel() is

How to design a returned stream that may use skip

一笑奈何 提交于 2019-12-08 21:44:35
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 return, e.g. Files.lines(input)**.skip(1)**.parallel().map(r -> createRecord(r)); The parsing library

Does Java groupingBy collector preserve list order?

自闭症网瘾萝莉.ら 提交于 2019-12-08 16:55:24
问题 Consider a list List<People> where the elements are sorted in ascending order of People.getAge() . If we group this list using Collectors.groupingBy(People::getCity) , would the resultant lists for each of the groups/cities remain sorted on age? In practice, it does seem to preserve the order. I'm looking for a guarantee. The Javadoc for the method says: If preservation of the order in which elements appear in the resulting Map collector is not required, using groupingByConcurrent(Function)

iterator() on parallel stream guarantee encounter order?

偶尔善良 提交于 2019-12-08 15:09:39
问题 Stream.of(a, b, c).parallel().map(Object::toString).iterator(); Is the returned iterator guaranteed to provide the values 2, 3, 4 in that order? I'm aware toArray() and collect() guarantee collections with values in the correct order. Also, I'm not asking how to make a stream from an iterator. 回答1: This is an oversight in the specification. If a stream has a defined encounter order, the intent was that its Iterator produce the elements in encounter order. If the stream has no defined

Adding Bigdecimals inside java 8 stream

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 14:06:46
问题 I would like to know if one way is more effecient than the other. Is there a better java 8 way to do the following operation ? java 8 way BigDecimal total = entries.parallelStream() .map(poec -> BigDecimal.valueOf(poec.getQuantity().longValue() * poec.getAdjustedUnitPrice().doubleValue())) .collect(Collectors.toList()).stream() .reduce(BigDecimal.ZERO, BigDecimal::add); Normal Java 7 way for (final EntryConsumed poec : entries) { total = total.add(BigDecimal.valueOf(poec.getQuantity()