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
17
votes
2 answers

Arrow (->) operator precedence/priority is lowest, or priority of assignment/combined assignment is lowest?

JLS: The lowest precedence operator is the arrow of a lambda expression (->), followed by the assignment operators. Followed in which direction (increasing priority, decreasing priority)? - "followed" means assignment has higher priority or…
17
votes
7 answers

How to map elements to their index using streams?

I get a stream of some custom objects and I would like to create a map Map with index of each object as key. To give you a simple example: Stream myStream = Arrays.asList("one","two","three").stream(); Integer i =…
nopens
  • 721
  • 1
  • 4
  • 20
17
votes
2 answers

Does the perfomance of "filter then map" and "map then filter" differ in a Stream?

I would like to know what is faster: to filter a custom object by field and then map by its field or vice-versa (map and then filter). At the end, I usually want to collect the mapped field into some Collection. For example, the simplest Person…
keyzj
  • 321
  • 3
  • 13
17
votes
5 answers

Java 8 - Convert LocalDate to OffsetDateTime

I like to know if it is possible to convert in Java 8 from LocalDate to OffsetDateTime. For example assume that I have got this LocalDate: 1992-12-28 Then I would like to have it converted to this OffsetDateTime: 1992-12-28T00:00-03:00 Assume that…
Sandro Rey
  • 2,429
  • 13
  • 36
  • 80
17
votes
3 answers

Different behavior of WeekFields on JVM 8 and JVM 10

I have really simple program here: public static void main(String[] args) { LocalDate year = LocalDate.ofYearDay(2022, 100); System.out.println(year); System.out.println(WeekFields.of(Locale.GERMAN).weekOfYear()); …
Lukas Forst
  • 802
  • 1
  • 8
  • 25
17
votes
2 answers

Java8 Hashmap rehashing in case of returning constant hashcode

As per below code, hashmap initial dafault capacity is 16 and LF is 0.75, so when I enter 13th element then rehashing should occur and because I have provided a constant hashcode, it internally maintain a linked list to maintain an insertion order.…
Vi_Code
  • 181
  • 8
17
votes
2 answers

Java lambda only throwing expression-style instead of statement-style

In Java (using Java 8 currently), I can write this and all will compile nice and well: Supplier asd = () -> { throw new RuntimeException(); }; Yet, I cannot write this: Supplier asd = () -> throw new RuntimeException(); // This…
Tamir Nauman
  • 279
  • 1
  • 4
17
votes
3 answers

Add prefix and suffix to Collectors.joining() only if there are multiple items present

I have a stream of strings: Stream stream = ...; I want to construct a string which concatenates these items with , as a separator. I do this as following: stream.collect(Collectors.joining(",")); Now I want add a prefix [ and a suffix ]…
John Doe
  • 215
  • 3
  • 7
17
votes
4 answers

NULL safe object checking in JAVA 8

So i want to do a null safe check on a value contained within a value. So I have 3 objects contained within each other: Person has a clothes object which has a country object which has a capital So a person may not have clothes so a check like…
Blawless
  • 1,229
  • 2
  • 16
  • 26
17
votes
3 answers

Generic method reference type specifying before/after :: operator

What is the difference between the following method references, BiPredicate,String> contains1 = List::contains; BiPredicate,String> contains2 = List::contains; BiPredicate,String> contains3 =…
17
votes
4 answers

How to convert LocalDateTime to com.google.protobuf.Timestamp?

I have an instance of LocalDateTime, which I get from the repository layer, and I need to convert it to a Timestamp (Protocol Buffer) instance. I have used to following approach for the conversion: LocalDateTime localDateTime =…
uneq95
  • 2,158
  • 2
  • 17
  • 28
17
votes
2 answers

Why Oracle JDK 9 download ends so early?

I wanted to install JDK 9 on my machine, visited JDK official download page, and was surprised to see, Java SE 9 has reached end of support. Users of Java SE 9 should switch to Java SE 10. Please visit our Java SE Downloads page to get the current…
Red Boy
  • 5,429
  • 3
  • 28
  • 41
17
votes
5 answers

Why is Predicate not applicable to Object?

Assume we have a predicate declared as Predicate. I would naively expect it to be applicable to any superclass of SomeClass up the hierarchy, including Object. However this predicate is not applicable to Object. I get the…
lexicore
  • 42,748
  • 17
  • 132
  • 221
17
votes
7 answers

Java 8 Stream API - Select the lowest key after group by

I have a stream of Foo objects. class Foo { private int variableCount; public Foo(int vars) { this.variableCount = vars; } public Integer getVariableCount() { return variableCount; } } I want a list of Foo's…
James Kleeh
  • 12,094
  • 5
  • 34
  • 61
17
votes
2 answers

stream().collect(Collectors.toSet()) vs stream().distinct().collect(Collectors.toList())

If i have a list (~200 elements) of objects, with only few unique objects (~20 elements). I want to have only unique values. Between list.stream().collect(Collectors.toSet()) and list.stream().distinct().collect(Collectors.toList()) which is more…
Laxmikant
  • 1,551
  • 1
  • 13
  • 30