user1677663
user1677663

Reputation: 1461

Java 8 Generic compareBy() method signature

I've embedded my question in the code; I'm trying to eliminate code duplication by writing a method that requires a generic Function signature (I think):

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

class Sortable {
  private String name;
  private int id;
  private long data;
  Sortable(String name, int id, long data) {
    this.name = name;
    this.id = id;
    this.data = data;
  }
  public String name() { return name; }
  public int id() { return id; }
  public long data() { return data; }
  @Override
  public String toString() {
    return String.format("%-7s %-5d  %d", name, id, data);
  }
}

public class SortByMethod {
  // Here's the method I want to create, in order
  // to eliminate repetition. But what signature to use?
  /*
  public void sortBy(String by, List<Sortable> list, Function<???, ???> keyExtractor) {
    System.out.println("--- Sorted By " + by + " ---");
    list.stream()
      .sorted(Comparator.comparing(keyExtractor))
      .forEach(System.out::println);
  }
  */
  public static void main(String[] args) {
    List<Sortable> list = Arrays.asList(
      new Sortable("Foo", 11, 991876),
      new Sortable("Bar", 1, 991875),
      new Sortable("Baz", 7, 991874),
      new Sortable("Bingo", 19, 991873)
    );
    System.out.println("--- Sorted By Name ---");
    list.stream()
      .sorted(Comparator.comparing(Sortable::name))
      .forEach(System.out::println);
    System.out.println("--- Sorted By ID ---");
    list.stream()
      .sorted(Comparator.comparing(Sortable::id))
      .forEach(System.out::println);
    System.out.println("--- Sorted By Data ---");
    list.stream()
      .sorted(Comparator.comparing(Sortable::data))
      .forEach(System.out::println);
    // Instead of the above repetitive code, I'd like to say:
    /*
    sortBy("Name", list, Sortable::name);
    sortBy("ID", list, Sortable::id);
    sortBy("Data", list, Sortable::data);
    */
  }
}

I've got everything worked out except for the signature for the keyExtractor.

Upvotes: 3

Views: 605

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221106

Your method should look like this:

static <T, U extends Comparable<? super U>> void sortBy(
    String column, 
    List<? extends T> list, 
    Function<? super T, ? extends U> keyExtractor) {

    System.out.println("--- Sorted By " + column + " ---");
    list.stream()
        .sorted(Comparator.comparing(keyExtractor))
        .forEach(System.out::println);
}

The quickest way to get this right is to simply go to Comparator.comparing(), and copy paste the generics from their signature, as you have to fulfil their API constraints:

static <T,U extends Comparable<? super U>> Comparator<T> comparing(
    Function<? super T,? extends U> keyExtractor)

Upvotes: 4

Related Questions