java-8

convert list of map to map using flatMap

落爺英雄遲暮 提交于 2020-06-11 06:56:29
问题 How I can merge List<Map<String,String>> to Map<String,String> using flatMap ? Here's what I've tried: final Map<String, String> result = response .stream() .collect(Collectors.toMap( s -> (String) s.get("key"), s -> (String) s.get("value"))); result .entrySet() .forEach(e -> System.out.println(e.getKey() + " -> " + e.getValue())); This does not work. 回答1: Assuming that there are no conflicting keys in the maps contained in your list, try following: Map<String, String> maps = list.stream()

`Java 8 in Action` is wrong about the demo it provided?

僤鯓⒐⒋嵵緔 提交于 2020-06-11 05:59:07
问题 This code is a quoted from Java 8 in Action, which is also in the book 11.4.3. public Stream<CompletableFuture<String>> findPricesStream(String product) { return shops.stream() .map(shop -> CompletableFuture.supplyAsync(() -> shop.getPrice(product), executor)) .map(future -> future.thenApply(Quote::parse)) .map(future -> future.thenCompose(quote -> CompletableFuture.supplyAsync(() -> Discount.applyDiscount(quote), executor))); } Along the code, the writer enclose a figure as follows

DateTimeFormatter weekday seems off by one

こ雲淡風輕ζ 提交于 2020-06-10 02:35:08
问题 I'm porting an existing application from Joda-Time to Java 8 java.time . I ran into a problem where parsing a date/time string that contains a 'day of week' value triggered an exception in my unit tests. When parsing: 2016-12-21 20:50:25 Wednesday December +0000 3 using format: yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'EEEE' 'MMMM' 'ZZ' 'e I get: java.time.format.DateTimeParseException: Text '2016-12-21 20:50:25 Wednesday December +0000 3' could not be parsed: Conflict found: Field DayOfWeek 3 differs

Nested lists with streams in Java8

一曲冷凌霜 提交于 2020-06-09 16:51:41
问题 I have a list of objects A. Each object A in this list contains list of object B and the object B contains list of Object C. The object C contains an attribute name that i want to use to filter using java 8. how to write the code below in java 8 using streams to avoid nested loop : C c1 = null; String name = "name1" for (A a: listOfAObjects) { for (B b: a.getList()) { for (C c: b.getPr()) { if (c.getName().equalsIgnoreCase(name)) { c1= c; break; } } } } 回答1: You can use two flatMap then a

Using Streams instead of for loop in java 8

让人想犯罪 __ 提交于 2020-06-09 10:15:48
问题 int [] numbers = {1,2,3,4,5,6,7,8}; int [] doubleNumbers = new int[numbers.length]; int [] tripleNumbers = new int[numbers.length]; for(int index = 0; index < numbers.length; index++) { doubleNumbers[index] = numbers[index] * 2; tripleNumbers[index] = numbers[index] * 3; } System.out.println("Double Numbers"); Arrays.stream(doubleNumbers).forEach(System.out::println); System.out.println("Triple Numbers"); Arrays.stream(tripleNumbers).forEach(System.out::println); I have above code where I

Creating an immutable list from an existing list using streams

泪湿孤枕 提交于 2020-06-08 07:08:49
问题 There is a list of Person objects. List<Person> persons = generatePersons(); An unmodifiableList is created with it. List<Person> unmodifiableList = Collections.unmodifiableList(persons); I understand that unmodifiableList doesn't support add/remove/set operations. At the same time it is not immutable since it has a reference to an existing modifiable list persons and whenever changes are made to the persons list, the changes are reflected in unmodifiableList too. An immutable list is created

Need to get correct output for number of weeks in a month irrespective of what date format being used

六月ゝ 毕业季﹏ 提交于 2020-06-01 06:24:05
问题 I have this code which returns Number of week correctly. package org.test.Calendar; import java.util.Calendar; import java.util.GregorianCalendar; public class GetDaysInMonth { public static void main(String[] args) { Calendar calendar = GregorianCalendar.getInstance(); int year = 2020; int month = Calendar.MAY; int date = 1; calendar.set(year, month, date); int numOfDaysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); int numOfWeeksInMonth = calendar.getActualMaximum(Calendar.WEEK

Simple spring-boot jar doesn't work except build/target folder

天涯浪子 提交于 2020-06-01 04:48:51
问题 I have a simple spring-boot application but it doesn't work except run from target/build folder. Here are all my config and files. Please help me to find out why it is not working and what is the wrong I am doing. It works perfectly fine while I run from target folder java -jar target\demo-0.0.1-SNAPSHOT and by running from IDE(Eclipse). and my weburl is http://localhost:8082/demo/edit.html . I just copied this jar to different machine/folder it is not longer working. package com.example.demo

Java - exact meaning http.maxConnections

时光总嘲笑我的痴心妄想 提交于 2020-05-30 19:28:14
问题 Recently I came across 2 slightly different definitions of the java property http.maxConnections provided by Oracle. here it's defined as If HTTP keepalive is enabled this value determines the maximum number of idle connections that will be simultaneously kept alive, per destination. whereas here it's defined as Indicates the maximum number of connections per destination to be kept alive at any given time What confuses me is the word idle mentioned in the first definition above. Considering

Java - exact meaning http.maxConnections

a 夏天 提交于 2020-05-30 19:27:12
问题 Recently I came across 2 slightly different definitions of the java property http.maxConnections provided by Oracle. here it's defined as If HTTP keepalive is enabled this value determines the maximum number of idle connections that will be simultaneously kept alive, per destination. whereas here it's defined as Indicates the maximum number of connections per destination to be kept alive at any given time What confuses me is the word idle mentioned in the first definition above. Considering