Difference between CompletableFuture, Future and RxJava’s Observable

I would like to know the difference between CompletableFuture,Future and Observable RxJava. What I know is all are asynchronous but Future.get() blocks the thread CompletableFuture gives the callback methods RxJava Observable — similar to CompletableFuture with other benefits(not sure) For example: if client needs to make multiple service calls and when we use Futures (Java) … Read more

Java8: HashMap to HashMap using Stream / Map-Reduce / Collector

I know how to “transform” a simple Java List from Y -> Z, i.e.: List<String> x; List<Integer> y = x.stream() .map(s -> Integer.parseInt(s)) .collect(Collectors.toList()); Now I’d like to do basically the same with a Map, i.e.: INPUT: { “key1” -> “41”, // “41” and “42” “key2” -> “42” // are Strings } OUTPUT: { “key1” … Read more

Why does Iterable not provide stream() and parallelStream() methods?

I am wondering why the Iterable interface does not provide the stream() and parallelStream() methods. Consider the following class: public class Hand implements Iterable<Card> { private final List<Card> list = new ArrayList<>(); private final int capacity; //… @Override public Iterator<Card> iterator() { return list.iterator(); } } It is an implementation of a Hand as you … Read more

Difference between `Optional.orElse()` and `Optional.orElseGet()`

I am trying to understand the difference between the Optional<T>.orElse() and Optional<T>.orElseGet() methods. The description for the orElse() method is “Return the value if present, otherwise return other.” While, the description for the orElseGet() method is “Return the value if present, otherwise invoke other and return the result of that invocation.” The orElseGet() method takes … Read more

Using Java 8’s Optional with Stream::flatMap

The new Java 8 stream framework and friends make for some very concise Java code, but I have come across a seemingly-simple situation that is tricky to do concisely. Consider a List<Thing> things and method Optional<Other> resolve(Thing thing). I want to map the Things to Optional<Other>s and get the first Other. The obvious solution would … Read more

Filter Java Stream to 1 and only 1 element

I am trying to use Java 8 Streams to find elements in a LinkedList. I want to guarantee, however, that there is one and only one match to the filter criteria. Take this code: public static void main(String[] args) { LinkedList<User> users = new LinkedList<>(); users.add(new User(1, “User1”)); users.add(new User(2, “User2”)); users.add(new User(3, “User3”)); User … Read more

How can I create a Java 8 LocalDate from a long Epoch time in Milliseconds?

I have an external API that returns me dates as longs, represented as milliseconds since the beginning of the Epoch. With the old style Java API, I would simply construct a Date from it with Date myDate = new Date(startDateLong) What is the equivalent in Java 8’s LocalDate/LocalDateTime classes? I am interested in converting the … Read more