How to convert an Iterator to a Stream?

One way is to create a Spliterator from the Iterator and use that as a basis for your stream: Iterator<String> sourceIterator = Arrays.asList(“A”, “B”, “C”).iterator(); Stream<String> targetStream = StreamSupport.stream( Spliterators.spliteratorUnknownSize(sourceIterator, Spliterator.ORDERED), false); An alternative which is maybe more readable is to use an Iterable – and creating an Iterable from an Iterator is very easy … Read more

Why should Java 8’s Optional not be used in arguments

Oh, those coding styles are to be taken with a bit of salt. (+) Passing an Optional result to another method, without any semantic analysis; leaving that to the method, is quite alright. (-) Using Optional parameters causing conditional logic inside the methods is literally contra-productive. (-) Needing to pack an argument in an Optional, … Read more

Converting between java.time.LocalDateTime and java.util.Date

Short answer: Date in = new Date(); LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault()); Date out = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant()); Explanation: (based on this question about LocalDate) Despite its name, java.util.Date represents an instant on the time-line, not a “date”. The actual data stored within the object is a long count of milliseconds since 1970-01-01T00:00Z (midnight at the start … Read more

Java 8 Lambda function that throws exception?

You’ll need to do one of the following. If it’s your code, then define your own functional interface that declares the checked exception: @FunctionalInterface public interface CheckedFunction<T, R> { R apply(T t) throws IOException; } and use it: void foo (CheckedFunction f) { … } Otherwise, wrap Integer myMethod(String s) in a method that doesn’t … Read more

Java 8 Distinct by property

Consider distinct to be a stateful filter. Here is a function that returns a predicate that maintains state about what it’s seen previously, and that returns whether the given element was seen for the first time: public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) { Set<Object> seen = ConcurrentHashMap.newKeySet(); return t -> seen.add(keyExtractor.apply(t)); } … Read more

When to use: Java 8+ interface default method, vs. abstract method

There’s a lot more to abstract classes than default method implementations (such as private state), but as of Java 8, whenever you have the choice of either, you should go with the defender (aka. default) method in the interface. The constraint on the default method is that it can be implemented only in the terms … Read more

Should I always use a parallel stream when possible?

A parallel stream has a much higher overhead compared to a sequential one. Coordinating the threads takes a significant amount of time. I would use sequential streams by default and only consider parallel ones if I have a massive amount of items to process (or the processing of each item takes time and is parallelizable) … Read more

Is it possible to use Java 8 for Android development?

UPDATE 2017/11/04 – Android Studio 3.0 now has native support for Java 8. gradle-retrolambda is now no longer needed. See https://developer.android.com/studio/write/java8-support.html The above link also includes migration instructions if you are using gradle-retrolambda. Original answer below: Android does not support Java 8. It only supports up to Java 7 (if you have kitkat) and still … Read more

Find first element by predicate

No, filter does not scan the whole stream. It’s an intermediate operation, which returns a lazy stream (actually all intermediate operations return a lazy stream). To convince you, you can simply do the following test: List<Integer> list = Arrays.asList(1, 10, 3, 7, 5); int a = list.stream() .peek(num -> System.out.println(“will filter ” + num)) .filter(x … Read more

tech