java-stream

Why is Java 8 Stream class AutoCloseable?

a 夏天 提交于 2019-12-19 05:22:48
问题 In Java 8, the Stream class implements AutoCloseable. This means that a stream instance should be closed explicitly. I understand why file handlers and DB connections are closeable. But why streams? 回答1: I think the current documentation/javadoc of Stream is pretty clear: 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

How to Split odd and even numbers and sum of both in collection using Stream

那年仲夏 提交于 2019-12-19 04:54:09
问题 how can I Split odd and even numbers and sum both in collection using Stream method of java-8 ?? public class SplitAndSumOddEven { public static void main(String[] args) { // Read the input try (Scanner scanner = new Scanner(System.in)) { // Read the number of inputs needs to read. int length = scanner.nextInt(); // Fillup the list of inputs List<Integer> inputList = new ArrayList<>(); for (int i = 0; i < length; i++) { inputList.add(scanner.nextInt()); } // TODO:: operate on inputs and

Java count occurrence of each element in an integer array

折月煮酒 提交于 2019-12-19 03:42:12
问题 I've written the following snippet to count the number of occurrences of each element. Is it possible to achieve this in a much shorter way? int[] arr = {1, 6, 2, 8, 5, 4, 7, 7, 5, 7}; Arrays.stream(arr) .collect(ArrayList::new, ArrayList::add, ArrayList::addAll) .stream() .collect(Collectors.groupingBy(s -> s)) .forEach((k, v) -> System.out.println(k+" "+v.size())); Also I would like to display only the elements which occur more than 1 time. So I tried modifying as below which resulted in an

Passing a collection using a reduce (3 parameters) function - streams java 8

China☆狼群 提交于 2019-12-19 03:11:08
问题 I am trying to calculate the multiplication of a value using the previous two values using java 8's stream. I want to call a function that will return an array/list/collection. I am creating a List and adding 1,2 to it. Let's say the list name is result. public static void main (String[] args) { List<Integer> result = new ArrayList<Integer>(); result.add(1); result.add(2); int n = 5; //n can be anything, choosing 5 for this example res(n, result); //print result which should be [1, 2, 2, 4, 8

Java 8 equivalent to getLineNumber() for Streams

喜夏-厌秋 提交于 2019-12-18 22:35:19
问题 Is there an equivalent to getLineNumber() for Streams in Java 8? I want to search for a word in a textfile and return the line number as Integer. This is my search Method: result = Files.lines(Paths.get(fileName)) .filter(w -> w.contains(word)) .collect(Collectors.<String> toList()); 回答1: I don't think there is, because streams are not designed to provide an access to their elements, not like collections. One workaround would be to read the file in the list, then use an IntStream to generate

Java stream “forEach” but not consuming stream

时光毁灭记忆、已成空白 提交于 2019-12-18 22:33:41
问题 Sometimes it would be handy do "something" (e.g. print) with every element in a stream in between steps of processing the stream, e.g. for debugging. A simple example could look like this, unfortunately this does not work as forEach consumes the stream: List<String> list = new ArrayList<>(); list.add("one"); list.add("two"); list.add("three"); list.add("four"); List<String> filteredList = list.stream() .filter(s -> s.startsWith("t")) .forEach(System.out::println) .collect(Collectors.toList())

Counting elements of a Stream

让人想犯罪 __ 提交于 2019-12-18 21:22:28
问题 I want to count the different elements of a stream and am wondering why Stream<String> stream = Stream.of("a", "b", "a", "c", "c", "a", "a", "d"); Map<String, Integer> counter1 = stream.collect(Collectors.toMap(s -> s, 1, Integer::sum)); doesn't work. Eclipse tells me The method toMap(Function, Function, BinaryOperator) in the type Collectors is not applicable for the arguments (( s) -> {}, int, Integer::sum) By the way, I know about that solution: Map<String, Long> counter2 = stream.collect

Arrays.stream(array) vs Arrays.asList(array).stream()

本秂侑毒 提交于 2019-12-18 21:20:15
问题 In this question it has already been answered that both expressions are equal, but in this case they produce different results. For a given int[] scores , why does this work: Arrays.stream(scores) .forEach(System.out::println); ...but this does not: Arrays.asList(scores).stream() .forEach(System.out::println); As far as I know .stream() can be called on any Collection, which Lists definitely are. The second code snippet just returns a stream containing the array as a whole instead of the

Java 8 streams serial vs parallel performance

元气小坏坏 提交于 2019-12-18 21:15:53
问题 On my machine, the program below prints: OptionalLong[134043] PARALLEL took 127869 ms OptionalLong[134043] SERIAL took 60594 ms It's not clear to my why executing the program in serial is faster than executing it in parallel. I've given both programs -Xms2g -Xmx2g on an 8gb box thats relatively quiet. Can someone clarify whats going on? import java.util.stream.LongStream; import java.util.stream.LongStream.Builder; public class Problem47 { public static void main(String[] args) { final long

Arrays.asList vs. Arrays.stream to use forEach()

こ雲淡風輕ζ 提交于 2019-12-18 18:59:39
问题 If you have an Array and you want to use the Java8 forEach() method, which approach is better or more efficient: Arrays.asList(new String[]{"hallo","hi"}).forEach(System.out::println); or Arrays.stream(new String[]{"hallo","hi"}).forEach(System.out::println); Is the difference significant or are there any better solutions to solve this? 回答1: Neither. If you already had an array, String[] array; I would use: Arrays.stream(array).forEach(System.out::println); because you leave the conversion of