java中的比较器不工作

java中的比较器不工作,java,collections,comparator,Java,Collections,Comparator,当我运行代码时,出现以下错误 我是否应该在编写此方法的类中创建Mycomp类的对象 Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments 步骤1:将比较器输入为比较器: 当您使用Collections.sort(l)时您的产品类必须实现您应该使用Collections.sort(l),其中l是您正在编写集合的列表对象。sort

当我运行代码时,出现以下错误 我是否应该在编写此方法的类中创建Mycomp类的对象

Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments 

步骤1:将比较器输入为
比较器


当您使用
Collections.sort(l)时您的产品类必须实现

您应该使用Collections.sort(l),其中l是您正在编写集合的列表对象。sort((pr)是错误的第一个错误是您没有闭合括号,另一个错误是它不是sort方法的有效参数它应该是实现集合的任何对象
    public List<Product> displaySortedShoppingCart(String userName) throws ShoppingCartNotFoundException
{
    getConnection();
    ResultSet rs;
    boolean flag=false;
    List<Product> l=new ArrayList<Product>();
    String sql="select *from scart where username='"+userName+"'";
    try {
        rs=stmt.executeQuery(sql);
        while(rs.next())
        {
            flag=true;
            Product pr=new Product();
            pr.setname(rs.getString(2));
            l.add(pr);
            //System.out.println(rs.getString(2));
        }
        if(flag==false)
            throw new ShoppingCartNotFoundException();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //SortProduct sp=new SortProduct(new Product());

    Collections.sort(l);
    return l;
}
import java.util.Comparator;

import com.bean.Product;


public class SortProduct implements Comparator {

    @Override
    public int compare(Object o1,Object o2)
    {

    Product p1=(Product)o1;

    Product p2=(Product)o2;

    int temp=p1.getName().compareTo(p2.getName());
    return temp;
    }

}
public class SortProduct implements Comparator<Product> {
    @Override
    public int compare(Product p1, Product p2) {
        return p1.getName().compareTo(p2.getName());
    }
}
Collections.sort(l, new SortProduct());