Java泛型的使用及List排序



import java.util.ArrayList;

public class ItemList<S extends Comparable, T> extends ArrayList<T> implements
    Comparable<ItemList<S, T>> {

  private static final long serialVersionUID = 3752285817293910325L;

  private S key;

  public ItemList(S key) {
    this.key = key;
  }

  public S getKey() {
    return key;
  }

  public int compareTo(ItemList<S, T> o) {
    return key.compareTo(o.getKey());
  }

}




private void generateReport(List<DbRow> productList) {
    Map<Date, ItemList<Date, String>> productMap = new Hashtable<Date, ItemList<Date, String>>();
    for (DbRow row : productList) {
      DbProduct product = (DbProduct) row;
      Date releaseDate = product.getProductReleaseDate();
      ItemList<Date, String> list = productMap.get(releaseDate);
      if (list == null) {
        list = new ItemList<Date, String>(releaseDate);
        productMap.put(releaseDate, list);
      }
      list.add(product.getProNumber() + " " + product.getProName());
    }

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    String tempGoLiveDate = df.format(goLiveDate);

    StringWriter tempWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(tempWriter);
    List<ItemList<Date, String>> releaseList = new ArrayList<ItemList<Date, String>>(productMap.values());
    Collections.sort(releaseList);
    for (ItemList<Date, String> release : releaseList) {
      writer.println(df.format(release.getKey()));
      for (String line : release) {
        writer.println(line);
      }
      writer.println();
    }
    writer.close();
  }

你可能感兴趣的:(java泛型)