java-stream

Java Streams .max() and .min() lag in performance?

蓝咒 提交于 2020-05-25 08:46:19
问题 Consider Below 2 examples. 1 With Streams myList.stream().map(this::getInt).max(Integer::compareTo); 2 Old way int max = Integer.MIN_VALUE; for (MyItem item : myList) { max = Math.max(max, getInt(item)); } Above getInt method accepts a MyItem argument and returns an int result. Here, #2 gives me a much lower latency compared to #1. Does anyone have an idea why or anything going wrong for me? 回答1: myList.stream().mapToInt(this::getInt).max() Try mapping to an IntStream. An IntStream works with

Sum up ArrayList<Double> via Java Stream [duplicate]

蹲街弑〆低调 提交于 2020-05-25 06:09:00
问题 This question already has answers here : Is mapToDouble() really necessary for summing a List<Double> with Java 8 streams? (3 answers) Closed 4 years ago . Im trying to Figure out how to Sum up all the elements of an 'ArrayList'. Ive tried already: double totalevent = myList.stream().mapToDouble(f -> f).sum(); while myList is a ArrayList<Double> . is there a way to do it without the useless mapToDouble function? 回答1: The mapToDouble call is not useless: it performs an implicit unboxing.

Sum up ArrayList<Double> via Java Stream [duplicate]

社会主义新天地 提交于 2020-05-25 06:05:02
问题 This question already has answers here : Is mapToDouble() really necessary for summing a List<Double> with Java 8 streams? (3 answers) Closed 4 years ago . Im trying to Figure out how to Sum up all the elements of an 'ArrayList'. Ive tried already: double totalevent = myList.stream().mapToDouble(f -> f).sum(); while myList is a ArrayList<Double> . is there a way to do it without the useless mapToDouble function? 回答1: The mapToDouble call is not useless: it performs an implicit unboxing.

Java 8 Streams: streaming files and MOVING them after read

我是研究僧i 提交于 2020-05-25 04:11:24
问题 I want to stream the lines contained in files but MOVING each file to another folder once it has been processed. The current process is like this: Explanation: I create a Stream of Files I create a BufferedReader for each one of them I flatMap to the lines Stream of the BufferedReader I print each line. Code (omited Exceptions for simplicity): (1) Stream.generate(localFileProvider::getNextFile) (2) .map(file -> new BufferedReader(new InputStreamReader(new FileInputStream(file)))) (3) .flatMap

Java 8 - The method map is not applicable for the arguments (<noType>)

旧巷老猫 提交于 2020-05-24 05:47:52
问题 I wanted to refactor my method which filter AssetLink object, get Content object that is a target of AssetLink, and then set fields of ContentLinkMetadata object basing on Content object. My new method looks like that: private List<ContentLinkMetadata> getAndFillInternalLinks(final Lesson lesson) { List<ContentLinkMetadata> internalLinks = new ArrayList<>(); lesson.getAssetLinks().stream() .filter(linkAsAssetLink -> ALLOWED_INTERNAL_LINK_TYPES.contains(linkAsAssetLink.getTargetType())) .map

Java 8 Streams : get non repeated counts

隐身守侯 提交于 2020-05-23 12:30:24
问题 Here is the SQL version for the input and output : with tab1 as ( select 1 as id from dual union all select 1 as id from dual union all select 2 as id from dual union all select 2 as id from dual union all select 5 as id from dual ) select id from tab1 group by id having count(id)=1; Output is Id=5 and count is 1 As 5 is non repeated.How do i implement it using JAVA 8 streams? I tried below but obviously it is giving wrong result List<Integer> myList = new ArrayList<Integer>(); myList.add(1);

Do Java 8 streams produce slower code than plain imperative loops?

半城伤御伤魂 提交于 2020-05-23 06:42:22
问题 There are too much hype about functional programming and particularly the new Java 8 streams API. It is advertised as good replacement for old good loops and imperative paradigm. Indeed sometimes it could look nice and do the job well. But what about performance? E.g. here is the good article about that: Java 8: No more loops Using the loop you can do all the job with a one iteration. But with a new stream API you will chain multiple loops which make it much slower(is it right?). Look at

Why is CompletableFuture join/get faster in separate streams than using one stream

主宰稳场 提交于 2020-05-23 05:05:29
问题 For the following program I am trying to figure out why using 2 different streams parallelizes the task and using the same stream and calling join/get on the Completable future makes them take longer time equivalent to as if they were sequentially processed). public class HelloConcurrency { private static Integer sleepTask(int number) { System.out.println(String.format("Task with sleep time %d", number)); try { TimeUnit.SECONDS.sleep(number); } catch (InterruptedException e) { e

Convert List of Maps to single Map via streams

南楼画角 提交于 2020-05-23 03:32:07
问题 I query the DB for two columns where the first one is the key to the second one. How can I convert the resulting list to a single map? Is it even possible? I have just seen examples with beans. List<Map<String, Object>> steps = jdbcTemplate.queryForList("SELECT key, value FROM table"); // well this doesn't work Map<String, String> result = steps.stream().collect(Collectors.toMap(s -> s.get("key"), s -> s.get("value"))); 回答1: You forgot to convert your key and value mappings to produce String

Convert List of Maps to single Map via streams

大憨熊 提交于 2020-05-23 03:31:51
问题 I query the DB for two columns where the first one is the key to the second one. How can I convert the resulting list to a single map? Is it even possible? I have just seen examples with beans. List<Map<String, Object>> steps = jdbcTemplate.queryForList("SELECT key, value FROM table"); // well this doesn't work Map<String, String> result = steps.stream().collect(Collectors.toMap(s -> s.get("key"), s -> s.get("value"))); 回答1: You forgot to convert your key and value mappings to produce String