Questions tagged [java8]

Java 8 refers to the version of the Java platform released in March 2014.

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

Java 8 was released in March 2014. 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 and it is the single largest update ever.

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
73 questions
128
votes
11 answers

Why use Optional in Java 8+ instead of traditional null pointer checks?

We have recently moved to Java 8. Now, I see applications flooded with Optional objects. Before Java 8 (Style 1) Employee employee = employeeServive.getEmployee(); if(employee!=null){ System.out.println(employee.getId()); } After Java 8 (Style…
user3198603
  • 1,896
  • 2
  • 16
  • 21
111
votes
3 answers

What is the name of a function that takes no argument and returns nothing?

In Java 8's java.util.function package, we have: Function: Takes one argument, produces one result. Consumer: Takes one argument, produces nothing. Supplier: Takes no argument, produces one result. ...: Other cases handling primitives, 2 arguments,…
superbob
  • 1,282
  • 2
  • 8
  • 11
111
votes
5 answers

Why were default and static methods added to interfaces in Java 8 when we already had abstract classes?

In Java 8, interfaces can contain implemented methods, static methods, and the so-called "default" methods (which the implementing classes do not need to override). In my (probably naive) view, there was no need to violate interfaces like this.…
Mister Smith
  • 2,867
  • 4
  • 21
  • 17
71
votes
3 answers

Is there a performance benefit to using the method reference syntax instead of lambda syntax in Java 8?

Do method references skip the overhead of the lambda wrapper? Might they in the future? According to the Java Tutorial on Method References: Sometimes... a lambda expression does nothing but call an existing method. In those cases, it's often…
GlenPeterson
  • 14,890
  • 6
  • 47
  • 75
69
votes
3 answers

Why are the Java 8 java.time classes missing a getMillis() method?

Java 8 has a whole new library for dates and times in the package java.time which is very welcome thing to anyone who has had to use JodaTime before or hassle with making it's own date processing helper methods. Many classes in this package…
Tarmo
  • 793
  • 1
  • 5
  • 8
57
votes
3 answers

Is it an antipattern to use peek() to modify a stream element?

Suppose I have a stream of Things and I want to "enrich" them mid stream, I can use peek() to do this, eg: streamOfThings.peek(this::thingMutator).forEach(this::someConsumer); Assume that mutating the Things at this point in the code is correct…
Bohemian
  • 1,956
  • 2
  • 17
  • 24
50
votes
4 answers

Why should I use "functional operations" instead of a for loop?

for (Canvas canvas : list) { } NetBeans suggests me to use "functional operations": list.stream().forEach((canvas) -> { }); But why is this preferred? If anything, it is harder to read and understand. You are calling stream(), then forEach() using…
Saturn
  • 3,887
  • 9
  • 30
  • 40
50
votes
7 answers

Workaround for Java checked exceptions

I appreciate a lot the new Java 8 features about lambdas and default methods interfaces. Yet, I still get bored with checked exceptions. For instance, if I just want to list all the visible fields of an object I would like to simply write this: …
31
votes
1 answer

Type inference in Java 8

Is the introduction of the new lambda notation (see e.g. this article) in Java 8 going to require some kind of type inference? If so, how will the new type system impact the Java language as a whole?
Giorgio
  • 19,486
  • 16
  • 84
  • 135
28
votes
9 answers

Why is using an optional preferential to null-checking the variable?

Take the two code examples: if(optional.isPresent()) { //do your thing } if(variable != null) { //do your thing } As far as I can tell the most obvious difference is that the Optional requires creating an additional object. However, many…
Will
  • 819
  • 2
  • 8
  • 11
23
votes
3 answers

Why is Optional.get() without calling isPresent() bad, but not iterator.next()?

When using the new Java8 streams api to find one specific element in a collection, I write code like this: String theFirstString = myCollection.stream() .findFirst() .get(); Here IntelliJ warns that get() is called…
TV's Frank
  • 357
  • 1
  • 2
  • 8
20
votes
3 answers

Is it a sane thing to return Streams wherever we would normally return Collections?

While developing my API that is not tied to any legacy code, I often find myself writing methods that are purely Streams pipeline terminated by collecting the results. Like this one: ImmutableSet deriveSomethingMeaningfulFromPrivateState() { …
jojman
  • 303
  • 1
  • 7
20
votes
1 answer

Passing a Scala function to a Java 8 method

The following Scala code works and can be passed to a Java method expecting a function. Is there a cleaner way to do this? Here's my first pass: val plusOne = new java.util.function.Function[Int,Int] { override def apply(t:Int):Int = t + 1 …
19
votes
1 answer

Does it make sense to measure conditional coverage for Java 8 code?

I'm wondering whether measuring conditional code coverage by current tools for Java are not obsolete since Java 8 came up. With Java 8's Optional and Stream we can often avoid code branches/loops, which makes it easy to get very high conditional…
16
votes
1 answer

Is it good practice to implement two Java 8 default methods in terms of each other?

I'm designing an interface with two related methods, similar to this: public interface ThingComputer { default Thing computeFirstThing() { return computeAllThings().get(0); } default List computeAllThings() { …
Tavian Barnes
  • 263
  • 2
  • 6
1
2 3 4 5