Java 如何将此代码转换为泛型?

Java 如何将此代码转换为泛型?,java,generics,Java,Generics,我在这里急需帮助。我要将现有代码迁移到泛型,我真的遇到了麻烦。任何帮助都将不胜感激 现有的代码是一个算法库,并附带一些带有车辆构造函数的类,如Bike.Java 我试过很多不同的方法,但我就是想不出来。我想了解一些情况 public class Algo { /** * Copies all objects from src to tgt, for which the predicate pred holds. * * @param src source list *

我在这里急需帮助。我要将现有代码迁移到泛型,我真的遇到了麻烦。任何帮助都将不胜感激

现有的代码是一个算法库,并附带一些带有车辆构造函数的类,如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);
    }
  }
}

由于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));
}
有关泛型的更多信息,请参阅:

有关流的更多信息,请参见。

我尝试了很多不同的方法,比如……我对这个问题投了否决票,因为您在询问一个bug时没有向我们显示代码。如果没有具体的代码,我们只能猜测问题可能是什么,这对您或未来的读者都没有用处。如果您可以通过您的问题向我们展示您的问题,此否决票可能会被撤回。请从启用所有编译器警告开始。然后注意它们,特别是那些关于“原始类型”的。嘿,这很有帮助!我能猜出那部分。不过我有一个问题:我们必须为泛型代码实现一个测试仪,但我们也要为上面的原始代码实现一个测试仪,其中有一些新的泛型代码无法使用的示例。你能给我举个例子说明这些东西可能是什么吗?他们都工作,这难道不合理吗?我希望这是有意义的。上述代码的测试可以通过casting think完成,但如果我理解正确,我不是舒尔。您想为第一个示例编写一些测试代码吗?