Uses for Optional

The main design goal of Optional is to provide a means for a function returning a value to indicate the absence of a return value. See this discussion. This allows the caller to continue a chain of fluent method calls. This most closely matches use case #1 in the OP’s question. Although, absence of a … Read more

javafx 8 compatibility issues – FXML static fields

It sounds like you are trying to inject a TextField into a static field. Something like @FXML private static TextField myTextField ; This apparently worked in JavaFX 2.2. It doesn’t work in JavaFX 8. Since no official documentation ever supported this use, it’s doesn’t really violate backward compatibility, though in fairness the documentation on exactly … Read more

:: (double colon) operator in Java 8

Usually, one would call the reduce method using Math.max(int, int) as follows: reduce(new IntBinaryOperator() { int applyAsInt(int left, int right) { return Math.max(left, right); } }); That requires a lot of syntax for just calling Math.max. That’s where lambda expressions come into play. Since Java 8 it is allowed to do the same thing in … Read more

Improve condition check

You can just combine all four conditions into one using logical operators. For example by using logical and && and logical or ||. It could then look like: if ((first && second) || (third && fourth)) { return true; } Or with all conditions substituted: if ((Obj.getSource().equals(“abc”) && Obj.getDest().equals(“bcd”)) || (Obj.getSource().equals(“abd”) && Obj.getDest().equals(“gdc”))) { return … Read more

convert String list in List of object [duplicate]

This might work ,I just took the arrays initially instead of List ,you can convert that into list import java.util.*; import java.util.stream.Collectors; class Employee { Integer id; String name; public Employee setName(String name) { this.name=name; return this; } public String getName() { return this.name; } } class Main { public static void main(String[] args) { … Read more

Word count in descending order using java lambdas

This is one way (it does create two streams and does merge the two lines of code): Map<String, Long> map = Arrays.stream(“some text some spaces”.split(” “)) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) .entrySet() .stream() .sorted(Map.Entry.<String, Long>comparingByValue().reversed()) .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue(), (v1, v2) -> v2, LinkedHashMap::new)); System.out.println(map); // This prints: {some=2, spaces=1, text=1}