Today I experimented with the “new” CompletableFuture from Java 8 and found myself confused when I didn’t find a runAsync(Callable) method. I can do it myself like shown below, but why is this (to me very obvious and useful utility method) missing? Am I missing something? Answer You are supp…
Tag: java-8
Java 8 filter with method call
I am learning Java 8 lambda and streams and trying some examples. But facing problem with it. here is my code Am getting compilation error in .map addr(second argument) field. what wrong in it and could you please provide links to learn java 8 streams. FYI am following this link Java 8 lambda Answer The first…
What’s the difference between ZonedDateTime and OffsetDateTime?
I’ve read the documentation, but I still can’t get when I should use one or the other: OffsetDateTime ZonedDateTime According to documentation OffsetDateTime should be used when writing date to database, but I don’t get why. Answer Q: What’s the difference between java 8 ZonedDateTime …
Why does Stream.allMatch() return true for an empty stream?
My colleague and I had a bug that was due to our assumption that an empty stream calling allMatch() would return false. Of course, it is kind of our fault for assuming and not reading documentation. But what I don’t understand is why the default allMatch() behavior for an empty stream returns true. What…
How to sum a list of integers with java streams?
I want to sum a list of Integers. It works as follows, but the syntax does not feel right. Could the code be optimized? Answer This will work, but the i -> i is doing some automatic unboxing which is why it “feels” strange. mapToInt converts the stream to an IntStream “of primitive int-va…
Error:java: javacTask: source release 8 requires target release 1.8
Using IntelliJ IDE can’t compile any projects. Screenshots of settings below: Used JDK: Project SDK and Language level: Language Level: Anybody have any ideas? Answer Go to File > Settings > Build, Execution, Deployment > Compiler > Java Compiler If on a Mac, it’s under Intellij IDEA &…
Hashmap with Streams in Java 8 Streams to collect value of Map
Let consider a hashmap I inserted some values into both hashmap. For Example, Q1) Now I want to apply a filter condition on the key in hashmap and retrieve the corresponding value(List). Eg: Here My query is key=1, and output should be ‘list1’ I wrote But I don’t know how to retrieve as a li…
Idiomatic way to create a Stream from a Nullable object
What is the best/idiomatic way of performing a null-check before generating a stream? I have a method that receives a List that might be null. So I can’t just call stream() on the value that is passed. Is there some static helper in that would give me an empty stream if a value is null? Answer I agree w…
Any way to stream a map like “(k,v)” instead of working with (entry)?
Basically I look for a way to avoid working with and similar to what Map.forEach() does. If only I could get a way to work as map.stream().filter((k,v) -> )… and so on It seems the interface is called BiConsumer. Perhaps with a converter to BiConsumer or a Stream.generate() someway Answer Since this …
How do I keep the iteration order of a List when using Collections.toMap() on a stream?
I am creating a Map from a List as follows: I want to keep the same iteration order as was in the List. How can I create a LinkedHashMap using the Collectors.toMap() methods? Answer The 2-parameter version of Collectors.toMap() uses a HashMap: To use the 4-parameter version, you can replace: with: Or to make …