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
611
votes
9 answers

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

Java 8 has a completely new API for date and time. One of the most useful classes in this API is LocalDateTime, for holding a timezone-independent date-with-time value. There are probably millions of lines of code using the legacy class…
Knut Arne Vedaa
  • 15,372
  • 11
  • 48
  • 59
608
votes
11 answers

How to convert an Iterator to a Stream?

I am looking for a concise way to convert an Iterator to a Stream or more specifically to "view" the iterator as a stream. For performance reason, I would like to avoid a copy of the iterator in a new list: Iterator sourceIterator =…
gontard
  • 28,720
  • 11
  • 94
  • 117
601
votes
14 answers

Convert java.util.Date to java.time.LocalDate

What is the best way to convert a java.util.Date object to the new JDK 8/JSR-310 java.time.LocalDate? Date input = new Date(); LocalDate date = ???
JodaStephen
  • 60,927
  • 15
  • 95
  • 117
548
votes
8 answers

Java 8 Iterable.forEach() vs foreach loop

Which of the following is better practice in Java 8? Java 8: joins.forEach(join -> mIrc.join(mSession, join)); Java 7: for (String join : joins) { mIrc.join(mSession, join); } I have lots of for loops that could be "simplified" with lambdas,…
nebkat
  • 8,445
  • 9
  • 41
  • 60
527
votes
4 answers

What's the difference between Instant and LocalDateTime?

I know that: Instant is rather a "technical" timestamp representation (nanoseconds) for computing. LocalDateTime is rather date/clock representation including time-zones for humans. Still in the end IMO both can be taken as types for most…
manuel aldana
  • 15,650
  • 9
  • 43
  • 50
511
votes
26 answers

Is there a concise way to iterate over a stream with indices in Java 8?

Is there a concise way to iterate over a stream whilst having access to the index in the stream? String[] names = {"Sam","Pamela", "Dave", "Pascal", "Erik"}; List nameList; Stream indices = intRange(1,…
Graeme Moss
  • 7,995
  • 4
  • 29
  • 42
509
votes
8 answers

Convert Iterable to Stream using Java 8 JDK

I have an interface which returns java.lang.Iterable. I would like to manipulate that result using the Java 8 Stream API. However Iterable can't "stream". Any idea how to use the Iterable as a Stream without converting it to List?
rayman
  • 20,786
  • 45
  • 148
  • 246
493
votes
15 answers

Retrieving a List from a java.util.stream.Stream in Java 8

I was playing around with Java 8 lambdas to easily filter collections. But I did not find a concise way to retrieve the result as a new list within the same statement. Here is my most concise approach so far: List sourceLongList =…
Daniel K.
  • 5,747
  • 3
  • 19
  • 22
482
votes
16 answers

How do I define a method which takes a lambda as a parameter in Java 8?

In Java 8, methods can be created as Lambda expressions and can be passed by reference (with a little work under the hood). There are plenty of examples online with lambdas being created and used with methods, but no examples of how to make a method…
Marius
  • 57,995
  • 32
  • 132
  • 151
480
votes
16 answers

Custom thread pool in Java 8 parallel stream

Is it possible to specify a custom thread pool for Java 8 parallel stream? I can not find it anywhere. Imagine that I have a server application and I would like to use parallel streams. But the application is large and multi-threaded so I want to…
Lukas
  • 13,606
  • 9
  • 31
  • 40
478
votes
13 answers

How to sum a list of integers with java streams?

I want to sum a list of Integers. It works as follows, but the syntax does not feel right. Could the code be optimized? Map integers; integers.values().stream().mapToInt(i -> i).sum();
membersound
  • 81,582
  • 193
  • 585
  • 1,120
472
votes
11 answers

How can I parse/format dates with LocalDateTime? (Java 8)

Java 8 added a new java.time API for working with dates and times (JSR 310). I have date and time as string (e.g., "2014-04-08 12:30"). How can I obtain a LocalDateTime instance from the given string? After I finished working with the LocalDateTime…
micha
  • 47,774
  • 16
  • 73
  • 80
469
votes
14 answers

How to negate a method reference predicate

In Java 8, you can use a method reference to filter a stream, for example: Stream s = ...; long emptyStrings = s.filter(String::isEmpty).count(); Is there a way to create a method reference that is the negation of an existing one, i.e.…
assylias
  • 321,522
  • 82
  • 660
  • 783
453
votes
13 answers

Convert java.time.LocalDate into java.util.Date type

I want to convert java.time.LocalDate into java.util.Date type. Because I want to set the date into JDateChooser. Or is there any date chooser that supports java.time dates?
Kavinda Gehan
  • 4,668
  • 2
  • 17
  • 20
428
votes
12 answers

Why should one use Objects.requireNonNull()?

I have noted that many Java 8 methods in Oracle JDK use Objects.requireNonNull(), which internally throws NullPointerException if the given object (argument) is null. public static T requireNonNull(T obj) { if (obj == null) throw new…
user4686046
  • 4,371
  • 2
  • 11
  • 7