Skip to main content
125 votes

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

Style 2 isn't going Java 8 enough to see the full benefit. You don't want the if ... use at all. See Oracle's examples. Taking their advice, we get: Style 3 // Changed EmployeeServive to return an ...
Caleth's user avatar
  • 11.5k
50 votes

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

If you're using Optional as a "compatibility" layer between an older API that may still return null, it may be helpful to create the (non-empty) Optional at the latest stage that you're sure that you ...
Joshua Taylor's user avatar
24 votes

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

There is next to no added value in having an Optional of a single value. As you've seen, that just replaces a check for null with a check for presence. There is huge added value in having a ...
Kilian Foth's user avatar
20 votes

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

As long as you use Optional just like a fancy API for isNotNull(), then yes, you won't find any differences with just checking for null. Things you should NOT use Optional for Using Optional just to ...
walen's user avatar
  • 355
18 votes

Parse both en_US and en-US as locale in Java

Rather than replacing without understanding I'd like to explain why there appear to be two forms one underscored and another hyphened and why one should care. tl;dr it is not simple as a single char ...
bric3's user avatar
  • 281
13 votes

How does sorting with java 8 stream work under the hood?

You can use grepcode.com to search through the Java standard library code (and some other libraries). Unfortunately, the stream implementation code is rather abstract. A good starting point is the ...
amon's user avatar
  • 135k
13 votes
Accepted

The use (or abuse) of Java 8, Mapper function

If you actually mean something like public <S super Company, T> Optional<T> getCompany(int companyId, Mapper<S,T> mapper) and the only function of the mapper is to transform the ...
Dr. Hans-Peter Störr's user avatar
12 votes

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

The key point for me in using Optional is keep clear when developer need check if function return value or not. //service 1 Optional<Employee> employeeOptional = employeeService.getEmployee(); ...
Rodrigo Menezes's user avatar
10 votes
Accepted

What's the complexity of Java's string split function?

The complexity will depend on the regex that you use to do the splitting. (Yes, the argument you supply to String.split(...) is a regex!) For your example, it will be O(N) where N is the number of ...
Stephen C's user avatar
  • 25.2k
8 votes

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

Your main mistake is that you're still thinking in more procedural terms. This is not meant as a criticism of you as a person, it's merely an observation. Thinking in more functional terms comes with ...
Haakon Løtveit's user avatar
8 votes
Accepted

Database agnostic DAO (NoSQL + SQL)

The best way to guarantee that you stay reasonably decoupled from the database, but at the same time remain free to use any feature of it, is to not create an abstraction layer for the database. (Well,...
Robert Bräutigam's user avatar
7 votes
Accepted

When should I create my own @FunctionalInterface rather than reuse the interfaces defined in java.util.function?

First off this is largely a matter of style and API design, because callers can always convert from one equivalent functional interface to another via method references. So if you have a Consumer<T&...
dimo414's user avatar
  • 393
7 votes
Accepted

Java8: why two composition methods: andThen and compose?

compose is a traditional operation. Its order was decided by mathematicians. However, like a lot of things originally decided by mathematicians, the order isn't a very convenient convention for ...
Karl Bielefeldt's user avatar
7 votes

Is the Java Stream API intended to replace loops?

Is Stream is intended as a basic programming paradigm every Java programmer should use often, or as a performance feature for senior software enhancers, that should be avoided? I think there's ...
JimmyJames's user avatar
  • 28.5k
6 votes
Accepted

Parsing Multiple Files and Their Contents in Java using Multithreading without ExecutorService

Great question! I've written a small example (it only uses 6 threads, but can easily be expanded) to illustrate how you could read multiple files (one thread to read each file) and process the data ...
D.B.'s user avatar
  • 176
5 votes
Accepted

Should DTO have same structure as payload?

Let me reword your question in a more general fashion: there are two different representations of the same block of data (here a DTO representation and a JSON representation) there are different ...
Doc Brown's user avatar
  • 212k
5 votes

Java 8: What does a blank return does in a constructor?

Return in a void function is used to escape the control flow before the natural end of a function. As such, it needs to be a valid statement. Going out of your way to explicitly forbid it as the ...
Lars Viklund's user avatar
  • 2,146
5 votes
Accepted

Why does Java source code contain so many single-letter variables?

Identifiers should describe the semantics of the things they are identifying in the domain language such that they can be easily grasped by domain experts. I am not an expert in the domain of low-...
Jörg W Mittag's user avatar
4 votes

Store conditional expression in database

You could also create a really simple expression language, and then evaluate it on the fly. Expression are trees, so it can be serialized as XML, JSON, s-expressions or whatever you prefer. Eg. a + b *...
JacquesB's user avatar
  • 60k
4 votes

Java Design Philosophy

It utilizes code signing certificates. Or simplified there's essentially a checksum added to the file together with some more information on who generated said checksum. It's a bit more complicated ...
Mario's user avatar
  • 1,509
4 votes
Accepted

Parse both en_US and en-US as locale in Java

Replace every occurence of "_" by "-" in the locale string when you receive the parameter and let the logic expect only "-".
Tulains Córdova's user avatar
4 votes

What's the complexity of Java's string split function?

Its O(n) in your particular cases, where you're splitting by 1/0 character length separators. In general, it's O (n + k) with a k-character separator, can be implemented using the KMP algorithm. Java ...
VinyleEm's user avatar
  • 251
4 votes

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

Are there any tools that measure logical branches that can be created in Java 8? I'm not aware of any. I tried running the code you have through JaCoCo (aka EclEmma) just to be sure, but it shows 0 ...
Shaz's user avatar
  • 2,613
4 votes

Are there any problems with using class variables in Java that can be accessed by any method?

The advantage of 1 is lower Arity, it has fewer arguments. The fewer there are the easier they are to remember. The advantage of 2 is that it's effect and dependency are explicit. Where it's used it'...
candied_orange's user avatar
3 votes
Accepted

Writing decision statement on controller layer

It really depends on how you're doing MVC. MVC does not dictate how M, V, and C communicate. Your code makes sense if you're doing this: But not if you're doing this: Which you should do largely ...
candied_orange's user avatar
3 votes

Usage of For-each loop vs functional operation vs Java8 streams

Performance In your example, I can hardly imagine any significant performance difference between the different styles, as in both cases, the work is done in N parallel Threads. Creating and managing ...
Ralf Kleberhoff's user avatar
3 votes
Accepted

Java version: When to migrate?

To even begin an answer to this topic and others like it, we have to answer the following questions: What's the risk if I don't upgrade? Working software is king, so if the risk is low enough it may ...
Berin Loritsch's user avatar
3 votes

Is the Java Stream API intended to replace loops?

A reason to prefer Streams over for is that for does everything. You get to use different names for different operations, rather than having to recognise a pattern spread across tens of lines. The ...
Caleth's user avatar
  • 11.5k
3 votes

Is the Java Stream API intended to replace loops?

Streams are a fine addition to the Java ecosystem, but they cannot replace the traditional for loop. Limited access to enclosing variables The main limitation is that code inside lambdas cannot ...
Ralf Kleberhoff's user avatar
3 votes
Accepted

How to remove unused code from a jar file?

This really is an engineering problem, a common way this is handles is keeping the libraries and your own application separate, and giving out updates to your program as partial updates, allowing ...
Ferrybig's user avatar
  • 188

Only top scored, non community-wiki answers of a minimum length are eligible