Archive for category intellij

Java 8: Converting Optional Collection to the Streams API

Although Java 9 has already been released, this post is about converting an optional collection to the Streams API introduced in Java 8.

Suppose some person could have zero, one or more cars and it is represented by the Person class below (some code omitted).

public class Person {

    private String name;

    .
    .
    .

    public Optional> getCars() {
        return Optional.ofNullable(cars);
    }

    .
    .
    .

}

Now we create a list of people and we want to get Mark’s cars.

Person mark = new Person("Mark");

List people = ...

How can we do that using the Streams API, since the getCars() method return an Optional?

One possibility is to filter people’s list by Mark’s name, filter the Optional if it is present or not and map its wrapped value (our cars list):

Collection markCars = people
                .stream()
                .filter(person -> "Mark".equals(person.getName()))
                .findFirst()
                .map(Person::getCars)
                .filter(Optional::isPresent)
                .map(Optional::get)
                .orElse(Collections.emptyList());

At this moment we reached the reason of this blog post. And how can we get all people’s cars? The idea here is to use the flatMap() operation unwrapping the Optional to the collection’s stream when it is present or getting an empty stream when it isn’t present:

Collection allPeopleCars = people
                .stream()
                .map(Person::getCars)
                .flatMap(mayHaveCars -> mayHaveCars.isPresent() ? mayHaveCars.get().stream() : Stream.empty())
                .collect(Collectors.toList());

We can do better and replace the above solution to be more functional using method references:

Collection allPeopleCars = people
                .stream()
                .map(Person::getCars)
                .flatMap(mayHaveCars -> mayHaveCars.map(Collection::stream).orElse(Stream.empty()))
                .collect(Collectors.toList());

If you use IntelliJ IDEA as your favorite IDE, it has an inspection that helps replacing Optional.isPresent() with a functional expression:

Collection allPeopleCars = people
                .stream()
                .map(Person::getCars)
                .flatMap(mayHaveCars -> mayHaveCars.map(Collection::stream).orElseGet(Stream::empty))
                .collect(Collectors.toList());

P.S. In Java 9, the stream() method was added to the Optional API, so we can rewrite the above stream pipeline into the following one:

Collection allPeopleCars = people
                .stream()
                .map(Person::getCars)
                .flatMap(Optional::stream)
                .flatMap(Collection::stream)
                .collect(Collectors.toList());

In case you are interested, this post on the IntelliJ IDEA blog has some good tips when working with Java 8.

Tags: , , , , , , , , , ,

Importing and debugging Eclipse projects in IntelliJ IDEA

Last week I faced a situation to import some Eclipse projects to IntelliJ IDEA, my default Java IDE. IntelliJ IDEA supports this integration, just go to File > New > Project from Existing Sources… and select a directory where Eclipse .project or .classpath files are located.

The project was imported successfully, it had some test compilation errors and it was all done for that moment. But, after running the project, I noted that I couldn’t debug some classes as well as I got used at Eclipse.

It was because, by default, IntelliJ IDEA uses the javac compiler and Eclipse has its own Java compiler that is part of JDT core. IntelliJ IDEA doesn’t proceed on code compilation when it finds the first error, even for test code or code that isn’t part of the build. The Eclipse compiler is able to proceed on code compilation even if it has compilation errors, so it is possible to run / debug code that doesn’t compile at all.

The solution, in this case, is to switch IntelliJ IDEA to use the Eclipse compiler. Just go to File > Settings > Build, Execution, Deployment > Compiler > Java compiler and change the drop down box "Use compiler:" to Eclipse and that is done.

I did that and now I am able to run / debug the Eclipse project using IntelliJ IDEA very well.

I have found the solution here:

Enable Partial Compile IntelliJ
What is the difference between javac and the Eclipse compiler?

Have you faced a situation like this? Have you done another solution than mine? Drop your comments here! 🙂

Tags: , , , ,