Questions tagged [java-8]

Use this tag for questions specific to Java 8 which is version 8 (internal number 1.8) of the Java platform, released on 18 March 2014. In most cases, you should also specify the java tag.

Java 8 features the most changes ever to be introduced in a new Java version.

The most important change is the support for functional paradigm through lambda expressions and method references, under Project Lambda. This is the driving new feature of Java 8.

Other features include:

  • A new Date & Time API
  • A new Stream API integrated into the Collections API
  • Project Nashorn, a JavaScript runtime which allows developers to embed JavaScript code within applications
  • A standard API for performing Base64 encoding and decoding
  • Stronger integration with JavaFX
  • Annotations on Java types

Although it was initially advertised to be launched in September 2013, the release was pushed to March 2014 due to security issues. The GA release has been available for download from Oracle's download page since 18 March 2014.

More details on how Java 8 continues to evolve can be found on the official website and on the JDK8 mailing list.

Please keep in mind that the General Availability version of Java 8 doesn't run on Windows XP.

Reference Material

##Frequently Asked Questions

23031 questions
420
votes
14 answers

How to get milliseconds from LocalDateTime in Java 8

I am wondering if there is a way to get current milliseconds since 1-1-1970 (epoch) using the new LocalDate, LocalTime or LocalDateTime classes of Java 8. The known way is below: long currentMilliseconds = new Date().getTime(); or long…
Georgios Syngouroglou
  • 18,813
  • 9
  • 90
  • 92
417
votes
15 answers

Break or return from Java 8 stream forEach?

When using external iteration over an Iterable we use break or return from enhanced for-each loop as: for (SomeObject obj : someObjects) { if (some_condition_met) { break; // or return obj } } How can we break or return using the…
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
399
votes
5 answers

How do I convert a Java 8 IntStream to a List?

I'm looking at the docs for the IntStream, and I see an toArray method, but no way to go directly to a List Surely there is a way to convert a Stream to a List?
Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
387
votes
12 answers

Ignore duplicates when producing map using streams

Map phoneBook = people.stream() .collect(toMap(Person::getName, Person::getAddress)); I get java.lang.IllegalStateException: Duplicate key…
Patan
  • 17,073
  • 36
  • 124
  • 198
386
votes
15 answers

Difference between final and effectively final

I'm playing with lambdas in Java 8 and I came across warning local variables referenced from a lambda expression must be final or effectively final. I know that when I use variables inside anonymous class they must be final in outer class, but still…
alex
  • 10,900
  • 15
  • 70
  • 100
385
votes
12 answers

Java 8: Difference between two LocalDateTime in multiple units

I am trying to calculate the difference between two LocalDateTime. The output needs to be of the format y years m months d days h hours m minutes s seconds. Here is what I have written: import java.time.Duration; import java.time.Instant; import…
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
374
votes
5 answers

Why is "final" not allowed in Java 8 interface methods?

One of the most useful features of Java 8 are the new default methods on interfaces. There are essentially two reasons (there may be others) why they have been introduced: Providing actual default implementations. Example:…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
373
votes
13 answers

Functional style of Java 8's Optional.ifPresent and if-not-Present?

In Java 8, I want to do something to an Optional object if it is present, and do another thing if it is not present. if (opt.isPresent()) { System.out.println("found"); } else { System.out.println("Not found"); } This is not a 'functional…
smallufo
  • 11,516
  • 20
  • 73
  • 111
366
votes
14 answers

Calculate days between two Dates in Java 8

I know there are lots of questions on SO about how to get Dates in Java, but I want an example using new Java 8 Date API. I also know about the JodaTime library, but I want a method without relying on external libraries. The function needs to be…
Marcos
  • 4,827
  • 3
  • 20
  • 28
365
votes
18 answers

How can I throw CHECKED exceptions from inside Java 8 lambdas/streams?

How can I throw CHECKED exceptions from inside Java 8 lambda, used in a stream for example? In other words, I want to make code like this compile: public List getClasses() throws ClassNotFoundException { List classes = …
Marcelo Glasberg
  • 29,013
  • 23
  • 109
  • 133
362
votes
7 answers

Should Java 8 getters return optional type?

Optional type introduced in Java 8 is a new thing for many developers. Is a getter method returning Optional type in place of the classic Foo a good practice? Assume that the value can be null.
leonprou
  • 4,638
  • 3
  • 21
  • 27
361
votes
3 answers

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…
user4464654
356
votes
14 answers

Uses for Optional

Having been using Java 8 now for 6+ months or so, I'm pretty happy with the new API changes. One area I'm still not confident in is when to use Optional. I seem to swing between wanting to use it everywhere something may be null, and nowhere at…
Will
  • 6,561
  • 3
  • 30
  • 41
352
votes
7 answers

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…
Dag
  • 10,079
  • 8
  • 51
  • 74
345
votes
19 answers

Maven is not working in Java 8 when Javadoc tags are incomplete

Since I use Maven I have been able to build and install in my local repository projects that have incomplete Javadoc tags (for example, a missing parameter). However, since I migrated to Java 8 (1.8.0-ea-b90) Maven is absolutely strict about…
Sergio
  • 8,532
  • 11
  • 52
  • 94