Enhanced for loop compiling fine for JDK 8 but not 7

This should actually compile fine for JDK 7 and 8. Quoting JLS section 14.14.2 (which is the same for the Java 7 specification): The enhanced for statement is equivalent to a basic for statement of the form: for (I #i = Expression.iterator(); #i.hasNext(); ) { {VariableModifier} TargetType Identifier = (TargetType) #i.next(); Statement } Rewriting the … Read more

What is CompletableFuture’s equivalent of flatMap?

There’s a bug in its documentation, but the CompletableFuture#thenCompose family of methods is the equivalent of a flatMap. Its declaration should also give you some clues public <U> CompletableFuture<U> thenCompose(Function<? super T,? extends CompletionStage<U>> fn) thenCompose takes the result of the receiver CompletableFuture (call it 1) and passes it to the Function you provide, which … Read more

Register a Stream “completion” hook

Any solution intercepting the terminal operations except flatMap-based solution (as proposed by @Holger) would be fragile to the following code: Stream<String> stream = getAutoCloseableStream(); if(stream.iterator().hasNext()) { // do something if stream is non-empty } Such usage is absolutely legal by the specification. Do not forget that iterator() and spliterator() are terminal stream operations, but after … Read more

Convert IntStream to Map

Here is my code, it will work for you. Function Reference version public class AppLauncher { public static void main(String a[]){ Map<Integer,Integer> map = IntStream.range(1,10).boxed().collect(Collectors.toMap(Function.identity(),AppLauncher::computeSmth)); System.out.println(map); } public static Integer computeSmth(Integer i){ return i*i; } } Lambda expression version public class AppLauncher { public static void main(String a[]){ Map<Integer,Integer> map = IntStream.range(1,10).boxed().collect(Collectors.toMap(Function.identity(),i->i*i)); System.out.println(map); } }

Java Collectors.groupingBy()—is List ordered?

The documentation for groupingBy() says: Implementation Requirements: This produces a result similar to: groupingBy(classifier, toList()); The documentation for toList() says: Returns: a Collector which collects all the input elements into a List, in encounter order So, to answer your question, as long as your stream has a defined encounter order, you’re guaranteed to get ordered … Read more

In java 8, why cannot call the interface static method that the current class is implementing [duplicate]

Addition of static methods in interface in Java 8 came with 1 restriction – those methods cannot be inherited by the class implementing it. And that makes sense, as a class can implement multiple interface. And if 2 interfaces have same static method, they both would be inherited, and compiler wouldn’t know which one to … Read more

Java 8 Pattern Matching?

I suppose you are not talking about pattern matching in the sense of applying a regular expression on a string, but as applied in Haskell. For instance using wildcards: head (x:_) = x tail (_:xs) = xs Java 8 will not support that natively, with Lambda expression there are, however, ways to do so, like … Read more

JavaFX Project Structure

IMHO, you shouldn’t create package’s depending on your views. My approach towards such applications A package for corresponding controllers of these views Different packages for service(business) and dao(persistence) layer, if exists A directory for resources such as images, css etc A directory for FXML files called view in the resources src/main ├──java ├── controllers ├──Screen1controller.java … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)