java-8

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);

How to parse date string EEE MMM dd to yyyy-MM-dd in java [duplicate]

妖精的绣舞 提交于 2020-05-23 11:57:46
问题 This question already has answers here : Change date format in a Java string (18 answers) how to parse output of new Date().toString() (3 answers) Closed 8 months ago . I'm having the date in this pattern EEEEE MMMMM yyyy HH:mm:ss.SSSZ , I would like to convert this to yyyy-MM-dd format in java, I tried below approach but I'm getting this exception java.text.ParseException: Unparseable date: SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy"); String dateInString = "Sun Oct 01 00

How to parse date string EEE MMM dd to yyyy-MM-dd in java [duplicate]

不羁的心 提交于 2020-05-23 11:57:30
问题 This question already has answers here : Change date format in a Java string (18 answers) how to parse output of new Date().toString() (3 answers) Closed 8 months ago . I'm having the date in this pattern EEEEE MMMMM yyyy HH:mm:ss.SSSZ , I would like to convert this to yyyy-MM-dd format in java, I tried below approach but I'm getting this exception java.text.ParseException: Unparseable date: SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy"); String dateInString = "Sun Oct 01 00

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

IdentityHashCode in HashMap's bucket

巧了我就是萌 提交于 2020-05-23 06:02:33
问题 In the implementation details of HashMap , I can read: When using comparators on insertion, to keep a * total ordering (or as close as is required here) across * rebalancings, we compare classes and identityHashCodes as * tie-breakers. If I have constant hashCode and fine equals and my class doesn't implement Comparable how exactly it will break the ties and how the tree will be constructed? I mean - bucket will transform to a tree and will use System.identityHashCode to break a tie. Then I

Get the next LocalDateTime for a given day of week

℡╲_俬逩灬. 提交于 2020-05-23 06:01:24
问题 I want to create instance of LocalDateTime at the date/time of the next (for example) Monday. Is there any method in Java Time API, or should I make calculations how many days are between current and destination dates and then use LocalDateTime.of() method? 回答1: There is no need to do any calculations by hand. You can adjust a given date with an adjuster with the method LocalDateTime.with(adjuster). There is a built-in adjuster for the next day of the week: TemporalAdjusters.next(dayOfWeek):

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