我如何将此代码转换为Generics?

时间:2017-11-12 21:08:29

标签: java generics

我在这里迫切需要帮助。我将这个现有代码迁移到泛型,我真的碰壁了。任何帮助将不胜感激。

现有代码是一个算法库,并附有一些带有车辆构造函数的类(即Bike.Java)。

我尝试了很多不同的东西,但我似乎无法弄明白。我喜欢一些见解。

public class Algo
{
  /**
   * Copies all objects from src to tgt, for which the predicate pred holds.
   *
   * @param src source list
   * @param tgt target list
   * @param pred unary predicate
   */
  static public
  void copyIf(List src, List tgt, UnaryPredicate pred)
  {
    for (Object obj : src)
    {
      if (pred.test(obj)) tgt.add(obj);
    }
  }

/**
   * Copies all objects from src to tgt that are greater than yardstick.
   *
   * @param src source
   * @param tgt target
   * @param yardstick determines if objects in src should be copied to tgt.
   */
  static public
  void copyIfGreaterThan(List src, List tgt, final Comparable yardstick)
  {
    copyIf(src, tgt, new UnaryPredicate() {
      public boolean test(Object o)
      {
        return yardstick.compareTo(o) < 0;
      }
    });
  }

  /**
   * Finds a maximum object in lst.
   *
   * @param lst a list containing non-null references
   * @return a maximum object in lst
   */
  static public
  Comparable findMax(List lst)
  {
    assert lst != null;

    Comparable max  = null;
    Iterator   iter = lst.iterator();

    // handle first element
    if (iter.hasNext())
      max = (Comparable) iter.next();

    // handle remaining elements
    while (iter.hasNext())
    {
      assert max != null;

      Comparable cand = (Comparable) iter.next();

      if (max.compareTo(cand) < 0)
        max = cand;
    }

    return max;
  }

  /**
   * Adds the smaller of lhs and rhs to dst.
   *
   * @param lhs left hand side object
   * @param rhs right hand side object
   * @param dst destination list
   */
  static public
  void storeMin(Comparable lhs, Comparable rhs, List dst)
  {
    Comparable min = lhs;

    if (min.compareTo(rhs) > 0) min = rhs;

    dst.add(min);
  }

  /**
   * swaps the elements at a and b in lst.
   *
   * @param lst a list
   * @param a first location in lst
   * @param b second location in lst
   */
  static private
  void swap(ArrayList objs, int a, int b)
  {
    Object tmp = objs.get(a);

    objs.set(a, objs.get(b));
    objs.set(b, tmp);
  }

  /**
   * Sorts the elements in lst.
   *
   * @param lst an array list containing non-null references
   */
  static public
  void selectionSort(ArrayList lst)
  {
    for (int i = 0; i < lst.size(); ++i)
    {
      int        min = i;
      Comparable minobj = (Comparable) lst.get(min);

      for (int j = i+1; j < lst.size(); ++j)
      {
        if (minobj.compareTo(lst.get(j)) > 0)
        {
          min = j;
          minobj = (Comparable) lst.get(min);
        }
      }

      swap(lst, min, i);
    }
  }
}

1 个答案:

答案 0 :(得分:-2)

从Java 8开始,你就可以这样做:

public static <T> List<T> copyIf(List<T> src, Predicate<T> predicate){
    return src.stream().filter(predicate).collect(Collectors.toList());
}

public static <T> List<T> copyIfGreaterThan(List<T> src, Comparable<T> yardstick) {
    return copyIf(src, t -> (yardstick.compareTo(t) < 0));
}

有关泛型的更多信息,请参阅: https://docs.oracle.com/javase/tutorial/java/generics/types.html

有关流的更多信息,请参阅例如 https://www.tutorialspoint.com/java8/java8_streams.htm