Sunday, July 26, 2015

Java 8: Creating infinite streams

There are many ways you can build a Stream in Java 8. One of the most common ways, is to get a stream from a collection using the stream method as shown below:

List<String> list = Arrays.asList("Alice", "Bob");
String<String> stream = list.stream();
stream.forEach(System.out::println);

You can also create a stream from values using the static method, Stream.of:

String<String> stream = Stream.of("Alice", "Bob");

Arrays can be converted to streams using the static method, Arrays.stream:

int[] numbers = { 1, 3, 6, 8 };
IntStream stream = Arrays.stream(numbers);
Creating infinite streams:

The Streams API provides two static methods: Stream.iterate and Stream.generate, which allow you to create an infinite stream. Here's an example:

Stream<Integer> evenNumbers = Stream.iterate(0, n -> n + 2);

The example above produces an infinite stream of even numbers. The iterate method takes a starting value and a lambda that is used to generate a new value from the previous one. In this case, the lambda returns the previous value added with 2. The stream is infinite because values are computed on demand and can be computed forever. (Note that you can use the limit method to explicitly limit the size of the stream.)

Similarly, Stream.generate also lets you produce an infinite stream, but the difference is that it takes a lamba of type Supplier<T> to provide new values, rather than a lambda that applies successively to the previous value. Here's an example:

Stream<Integer> randomNumbers = Stream.generate(Math::random);

Sunday, June 28, 2015

Java 8: Composing Comparators

In my last post, I showed how you can sort a list of Person objects by age, using the following statement:

list.sort(comparingInt(Person::getAge));

What if you want to sort the objects in order of decreasing age instead? There's no need to create a new instance of Comparator, because the Comparator interface has a handy default method reversed that reverses its ordering:

list.sort(comparingInt(Person::getAge).reversed());

Now, what if you want to sort people by name if they have the same age. The thenComparing method allows you to do just that, as shown below:

list.sort(comparingInt(Person::getAge)
    .reversed()
    .thenComparing(Person::getName));

Saturday, June 27, 2015

Java 8: Sorting a List using Lambdas and Method References

This post shows how you can use Java 8 lambda expressions and method references to sort a list of Person objects by age. In Java 8, the List interface has a sort method, which expects a Comparator to compare two objects.

Traditionally, you would either sort a list by creating a specific class that implements the Comparator interface, like this:

public class AgeComparator implements Comparator<Person> {
  @Override
  public int compare(Person p1, Person p2) {
    return Integer.compare(p1.getAge(), p2.getAge());
  }
}

list.sort(new AgeComparator());

or, you would use an anonymous class, like this:

list.sort(new Comparator<Person>() {
  @Override
  public int compare(Person p1, Person p2) {
    return Integer.compare(p1.getAge(), p2.getAge());
  }
});

As you can see, this is quite verbose!

Java 8 introduces lambda expressions, which allow you to pass code more concisely. Since Comparator is a functional interface, you can use a lambda expression to sort the list:

list.sort((p1, p2) -> Integer.compare(p1.getAge(), p2.getAge()));

You may have noticed that the Comparator class has a static method called comparingInt that takes a ToIntFunction and returns a Comparator object. So, we can rewrite the code above to:

import static java.util.Comparator.comparingInt;
list.sort(comparingInt(p -> p.getAge()));

Finally, we can improve our code even further by using a method reference, which is just "syntactic sugar" for a lambda expression:

list.sort(comparingInt(Person::getAge));

The final solution is not only shorter but is also easier to read :)

Monday, May 25, 2015

Java 8: Filtering an Optional

In my previous post, I wrote about how you can use optionals to model the absence of a value. Optionals also contain a useful filter method, which takes a predicate as an argument. If a value is present in the Optional object and the predicate is satisfied, the filter method returns that value; otherwise it returns an empty Optional object.

For example, let's suppose that the Phone class of the Person/Phone/Camera/Resolution model has a method to get the operating system of the phone, and we want to get the camera resolution of "Android" phones only. We can modify the getPhoneCameraResolution method to use a filter, as shown below:

public Resolution getPhoneCameraResolution(final Optional<Person> person) {
  return
    person.flatMap(Person::getPhone)  // returns Optional<Phone>
          .filter(phone -> phone.getOS() == OS.Android) // match Android phones only
          .flatMap(Phone::getCamera) // returns Optional<Camera>
          .map(Camera::getResolution) // returns Optional<Resolution>
          .orElse(Resolution.UNKNOWN); // returns Resolution or UNKNOWN if not found
}

Related posts:
Java 8: Using Optional Objects

Saturday, May 23, 2015

Java 8: Using Optional Objects

Consider the following nested object structure for a person with a camera phone:

class Person {
  private Phone phone;

  public Phone getPhone() {
    return phone;
  }
}

class Phone {
  private Camera camera;

  public Camera getCamera() {
    return camera;
  }
}

class Camera {
  private Resolution resolution;

  public Resolution getResolution() {
    return resolution;
  }
}

class Resolution {
  private int width;
  private int height;
}

Now, let's say that you want to get the resolution of a person's camera phone.

You might be tempted to do this:

public Resolution getPhoneCameraResolution(Person person) {
  return person.getPhone().getCamera().getResolution();
}

However, not everyone owns a phone, so person.getPhone() might return null and consequently person.getPhone().getCamera() will result in a NullPointerException at run-time! Similarly, a person might own a phone, but the phone might not have a camera.

One way to avoid NullPointerExceptions is to check for nulls, as shown below:

public Resolution getPhoneCameraResolution(Person person) {
  if (person != null) {
    Phone phone = person.getPhone();
    if (phone != null) {
      Camera camera = phone.getCamera();
      if (camera != null) {
        return camera.getResolution();
      }
    }
  }
  return Resolution.UNKNOWN;
}

This code doesn't scale very well and makes your code harder to read and maintain. Every time a variable could be null, you have to add another nested if statement.

Java 8: Optional class

Java 8 introduced a new class called java.util.Optional<T> to model potentially absent values. It forces you to actively unwrap an optional and deal with the absence of a value. It also leads to better APIs because, just by reading the signature of a method, you can tell whether to expect an optional value.

We can re-write the original object model using the Optional class as shown below:

import java.util.Optional;

class Person {
  private Optional<Phone> phone;

  public Optional<Phone> getPhone() {
    return phone;
  }
}

class Phone {
  private Optional<Camera> camera;

  public Optional<Camera> getCamera() {
    return camera;
  }
}

class Camera {
  // resolution is not optional - all cameras must have it
  private Resolution resolution;

  public Resolution getResolution() {
    return resolution;
  }
}

Creating Optional objects:

  • Empty Optional: You can create an empty optional object use Optional.empty:
    Optional<Phone> optPhone = Optional.empty();
    
  • Optional from a non-null value: To create an optional from a non-null value use Optional.of. Note that if the value is null a NullPointerException is thrown immediately:
    Optional<Phone> optPhone = Optional.of(phone);
    
  • Optional from null: Use Optional.ofNullable to create an optional that may hold a null value. If the value is null, an empty optional is created.
    Optional<Phone> optPhone = Optional.ofNullable(phone);
    

Extracting values from Optional objects:

Optional supports a map method, which can be used to extract information from an object. For example, to get the Resolution of a Camera:

Optional<Camera> optCamera = Optional.ofNullable(camera);
Optional<Resolution> resolution = optCamera.map(Camera::getResolution);

What this means is that if the camera optional contains a value, getResolutions is called, otherwise nothing happens and an empty optional is returned.

Optional also contains a flatMap method in order to "flatten" nested optionals. For example, if you were to call map with a function that returns an Optional, you will get an Optional containing an Optional, as illustrated below:

Optional<Phone> optPhone = Optional.ofNullable(phone);

// calling map returns a two-level optional
Optional<Optional<Camera>> optOptCamera = optPhone.map(Phone::getCamera);

// but flatMap, returns a single-level optional
Optional<Camera> optCamera = optPhone.flatMap(Phone::getCamera);

Chaining Optional objects:

Now let's get back to our original problem of getting the resolution of a person's camera phone and re-write those ugly, nested if-statements using optionals instead:

public Resolution getPhoneCameraResolution(final Optional<Person> person) {
  return
    person.flatMap(Person::getPhone)  // returns Optional<Phone>
          .flatMap(Phone::getCamera) // returns Optional<Camera>
          .map(Camera::getResolution) // returns Optional<Resolution>
          .orElse(Resolution.UNKNOWN); // returns Resolution or UNKNOWN if not found
}

This code returns the resolution of the person's camera phone. If the person is null, doesn't have a phone, or the phone doesn't have a camera, an UNKNOWN resolution is returned.