java-8

java 8 - stream, map and count distinct

坚强是说给别人听的谎言 提交于 2019-12-28 04:12:05
问题 My first attempt with java 8 streams... I have an object Bid, which represents a bid of a user for an item in an auction. i have a list of bids, and i want to make a map that counts in how many (distinct) auctions the user made a bid. this is my take on it: bids.stream() .collect( Collectors.groupingBy( bid -> Bid::getBidderUserId, mapping(Bid::getAuctionId, Collectors.toSet()) ) ).entrySet().stream().collect(Collectors.toMap( e-> e.getKey(),e -> e.getValue().size()) ); It works, but i feel

What is the difference between year and year-of-era?

馋奶兔 提交于 2019-12-28 03:52:26
问题 The DateTimeFormatter class documentation defines separate symbols u for year and y year-of-era: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns What is the difference between year and year-of-era? 回答1: The answer lies in the documentation for IsoChronology era - There are two eras, 'Current Era' (CE) and 'Before Current Era' (BCE). year-of-era - The year-of-era is the same as the proleptic-year for the current CE era. For the BCE era before the ISO

What is the difference between year and year-of-era?

大憨熊 提交于 2019-12-28 03:52:09
问题 The DateTimeFormatter class documentation defines separate symbols u for year and y year-of-era: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns What is the difference between year and year-of-era? 回答1: The answer lies in the documentation for IsoChronology era - There are two eras, 'Current Era' (CE) and 'Before Current Era' (BCE). year-of-era - The year-of-era is the same as the proleptic-year for the current CE era. For the BCE era before the ISO

java 8, Sort list of objects by attribute without custom comparator

别说谁变了你拦得住时间么 提交于 2019-12-28 03:47:09
问题 What is the cleanest short way to get this done ? class AnObject{ Long attr; } List<AnObject> list; I know it can be done with custom comparator for AnObject. Isn't there something ready out of the box for such case ? kind of Collections.sort(list, X.attr ) ; 回答1: Assuming you actually have a List<AnObject> , all you need is list.sort(Comparator.comparing(a -> a.attr)); If you make you code clean by not using public fields, but accessor methods, it becomes even cleaner: list.sort(Comparator

Java 8 Stream and operation on arrays

不打扰是莪最后的温柔 提交于 2019-12-28 03:23:29
问题 I have just discovered the new Java 8 stream capabilities. Coming from Python, I was wondering if there was now a neat way to do operations on arrays like summing, multiplying two arrays in a "one line pythonic" way ? Thanks 回答1: There are new methods added to java.util.Arrays to convert an array into a Java 8 stream which can then be used for summing etc. int sum = Arrays.stream(myIntArray) .sum(); Multiplying two arrays is a little more difficult because I can't think of a way to get the

Filter values only if not null using lambda in Java8

断了今生、忘了曾经 提交于 2019-12-28 03:21:25
问题 I have a list of objects say car . I want to filter this list based on some parameter using Java 8. But if the parameter is null , it throws NullPointerException . How to filter out null values? Current code is as follows requiredCars = cars.stream().filter(c -> c.getName().startsWith("M")); This throws NullPointerException if getName() returns null . 回答1: In this particular example I think @Tagir is 100% correct get it into one filter and do the two checks. I wouldn't use Optional.ofNullable

How to work around the stricter Java 8 Javadoc when using Maven

丶灬走出姿态 提交于 2019-12-28 03:18:16
问题 You'll quickly realize that JDK8 is a lot more strict (by default) when it comes to Javadoc. (link - see last bullet point) If you never generate any Javadoc then of course you'll not experience any problems but things like Maven release process and possibly your CI builds will suddenly fail where they worked just fine with JDK7. Anything that checks the exit value of the Javadoc tool will now fail. JDK8 Javadoc is probably also more verbose in terms of warnings compared to JDK7 but that's

Difference Between static and default methods in interface

本小妞迷上赌 提交于 2019-12-28 03:18:14
问题 I was learning through interfaces when I noticed that you can now define static and default methods in an interface. public interface interfacesample2 { public static void method() { System.out.println("hello world"); } public default void menthod3() { System.out.println("default print"); } } Kindly explain the difference of the two and also if there's an example of when we would use this would be nice. A little confused on Interfaces. 回答1: Differences between static and default methods in

Get original pattern String given a JDK 8 DateTimeFormatter?

旧街凉风 提交于 2019-12-28 03:04:28
问题 Related to my question here - how do I get the original pattern String given a DateTimeFormatter ? 回答1: It's been asked on the mailing list and the answer is that it is not possible because the original pattern is not retained. The same thread suggests using a DateTimeFormatterBuilder which does have the information. 回答2: It may not be a direct answer to your question but it may help. If you know the parameters of how it the formatter was constructed you can call the static method:

Is there a good way to extract chunks of data from a java 8 stream?

空扰寡人 提交于 2019-12-28 03:04:18
问题 I an ETL process I'm retrieving a lot of entities from a Spring Data Repository. I'm then using a parallel stream to map the entities to different ones. I can either use a consumer to store those new entities in another repository one by one or collect them into a List and store that in a single bulk operation. The first is costly while the later might exceed the available memory. Is there a good way to collect a certain amount of elements in the stream (like limit does), consume that chunk,