Differences between Java 8 Date Time API (java.time) and Joda-Time

I know there are questions relating to java.util.Date and Joda-Time. But after some digging, I couldn’t find a thread about the differences between the java.time API (new in Java 8, defined by JSR 310) and Joda-Time. I have heard that Java 8’s java.time API is much cleaner and can do much more than Joda-Time. But … Read more

Why use Optional.of over Optional.ofNullable?

When using the Java 8 Optional class, there are two ways in which a value can be wrapped in an optional. String foobar = <value or null>; Optional.of(foobar); // May throw NullPointerException Optional.ofNullable(foobar); // Safe from NullPointerException I understand Optional.ofNullable is the only safe way of using Optional, but why does Optional.of exist at all? … Read more

Java 8 Streams: multiple filters vs. complex condition

Sometimes you want to filter a Stream with more than one condition: myList.stream().filter(x -> x.size() > 10).filter(x -> x.isCool()) … or you could do the same with a complex condition and a single filter: myList.stream().filter(x -> x.size() > 10 && x -> x.isCool()) … My guess is that the second approach has better performance characteristics, … Read more

Is “Java Concurrency In Practice” still valid? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want to improve this question? Update the question so it’s on-topic for Stack Overflow. Closed 5 years ago. Improve this question Is Java Concurrency in Practice still valid? I am wondering whether the ideas, concepts and implementation described in the … Read more

What is difference between Collection.stream().forEach() and Collection.forEach()?

I understand that with .stream(), I can use chain operations like .filter() or use parallel stream. But what is difference between them if I need to execute small operations (for example, printing the elements of the list)? collection.stream().forEach(System.out::println); collection.forEach(System.out::println); 4 Answers 4

Explicitly calling a default method in Java

Java 8 introduces default methods to provide the ability to extend interfaces without the need to modify existing implementations. I wonder if it’s possible to explicitly invoke the default implementation of a method when that method has been overridden or is not available because of conflicting default implementations in different interfaces. interface A { default … Read more

Java 8 lambdas, Function.identity() or t->t

I have a question regarding the usage of the Function.identity() method. Imagine the following code: Arrays.asList(“a”, “b”, “c”) .stream() .map(Function.identity()) // <- This, .map(str -> str) // <- is the same as this. .collect(Collectors.toMap( Function.identity(), // <– And this, str -> str)); // <– is the same as this. Is there any reason why you … Read more

UnsupportedTemporalTypeException when formatting Instant to String

I’m trying to format an Instant to a String using the new Java 8 Date and Time API and the following pattern: Instant instant = …; String out = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”).format(instant); Using the code above I get an exception which complains about an unsupported field: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra at java.time.Instant.getLong(Instant.java:608) at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298) … 7 … Read more