Java 8 Generic compareBy() method signature

Viewed 415

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.

1 Answers
Related