我有一些抽象类,每个类都超级分为三个或四个具体的类和形式:
public abstract class TypeOfMapObject extends IrrelevantClass implements Serializable, MapObject, Comparable<MapObject>
{
//irrelevant stuff
@Override
public int compareTo(MapObject m)
{
//specific algorithm for natural ordering
}
}
Run Code Online (Sandbox Code Playgroud)
其他地方在我的代码我有一个ArrayList<MapObject>(其被正确填充,我已经检查了)叫tempMapObjectsArray 我希望那种ArrayList使用Collections.sort(tempMapObjectsArray)(或者更确切地说,我想那种ArrayList,似乎Collections.sort()是做到这一点的最好办法,具体办法它排序并不重要).
它没有编译和给出消息(在Netbeans中):
no suitable method found for sort(java.util.ArrayList<Model.MapObject>)
method java.util.Collections.<T>sort(java.util.List<T>,java.util.Comparator<? super T>) is not applicable
(cannot instantiate from arguments because actual and formal argument lists differ in length)
method java.util.Collections.<T>sort(java.util.List<T>) is not applicable
(inferred type does not conform to declared bound(s) …Run Code Online (Sandbox Code Playgroud) 我一直在尝试以这种方式覆盖compareTo:这是原始的:
@Override
public int compareTo(ProductPart6 s)
{
return this.getproductName().compareTo(s.getproductName());
}
Run Code Online (Sandbox Code Playgroud)
这就是我试图重写它的方法:它抛出一个错误 SubClass 类型的方法compareTo(SubClass) 必须重写或实现超类型方法。
@Override
public int compareTo(SubClass s)
{
return this.getTitle().compareTo(s.getTitle());
}
Run Code Online (Sandbox Code Playgroud)
我认为这是非常错误的。我的 ProductPart6 没有 getTitle() ,这会导致它
@Override
public int compareTo(ProductPart6 s)
{
return this.getTitle().compareTo(s.getTitle());
}
Run Code Online (Sandbox Code Playgroud)
抛出错误(ProductPart6 类型的 getTitle() 未定义) - 如果我在那里定义了它,则没有必要覆盖它。我究竟做错了什么?我有扩展 ProductPart6 的子类,并且 ProductPart6 实现了 Comparable - 我以为我可以在子类上实现它,但没有。那是不行的。
想象一下包括类似这样的类:
class Element
include Comparable
attr_accessor :name, :pos_x, :pos_y
def initialize(name, pos_x, pos_y)
@name = name
@pos_x = pos_x
@pos_y = pos_y
end
def <=>(other)
if (@pos_x == other.pos_x) and (@pos_y == other.pos_y)
return 0
else
return @name <=> other.name
end
end
def eql?(other)
self == other
end
end
Run Code Online (Sandbox Code Playgroud)
在这种情况下hash,你将如何实现这个功能a.hash == b.hash?一般来说,我会这样做:
def hash
@name.hash
end
Run Code Online (Sandbox Code Playgroud)
但这不包括pos_x和pos_y.
据我所知,Java Bean 应该始终:
但是,我想知道 Java bean 实现接口的约定是什么 Comparable?我可以保留 java bean 纯,这意味着绝对没有行为,只有数据,并编写自定义比较器类。不过,实现可比较更容易。
在实现 Comparable to Java Beans 之类的简单通用接口时,是否有任何约定?我自己找不到任何后果,但感觉我可能会违反一些规则,并且可能有一些我没有想到的事情。
假设我想对一个Employee对象列表进行排序:
Employee emp1 = new Employee("Abhijit", 10);
Employee emp2 = new Employee("Aniket", 5);
Employee emp3 = new Employee("Chirag", 15);
List<Employee> employees = new ArrayList<Employee>();
employees.add(emp1);
employees.add(emp2);
employees.add(emp3);
Collections.sort(employees);
System.out.println("sorted List is: "+employees);
Run Code Online (Sandbox Code Playgroud)
我的Employee类实现了Comparable,因此它必须覆盖该compareTo()方法。
而且我必须在compareTo()方法中编写我的排序逻辑。
class Employee implements Comparable<Employee>
{
String name;
int empId;
public Employee(String name, int empId)
{
this.name= name;
this.empId = empId;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name; …Run Code Online (Sandbox Code Playgroud) 我在Comparator练习时遇到了一种新的创作方式。任何人都可以解释一下吗?
class Checker{
public Comparator<Player> desc = new Comparator<Player>() {
public int compare(Player A, Player B){
if(A.score < B.score){
return 1;
}
else if(A.score == B.score){
return B.name.compareTo(A.name);
}
else{
return -1;
}
}
};
}
Run Code Online (Sandbox Code Playgroud)
过去,我只看到人们这样做:
class Checker implements Comparator{
@Override
public int compare(Player A, Player B){
..........
..........
}
}
Run Code Online (Sandbox Code Playgroud)
所以第一个例子对我来说真的很新颖(因为我是初学者?)。这是有道理的:desc可以是类的属性/实例变量,Checker它指向Comparator类/接口的新实例。然而,这两种不同的做事方式背后还有更多的故事吗?它们都需要创建一个不同的类,所以我看不出如何更有条理。
// Original class Dog
class Dog{
String name;
int age;
}
//Case 1
class Dog implements Comparable<Dog>{
//compareTo() implementation
}
//Case2
class Dog implements Comparator<Dog>{
// compare() implementation
}
//Case 3
class DogNameComparator implements Comparator<Dog>{
// compare() implementation
}
Collection.sort(dogList);
Collectios.sort(dogList,new DogNameComparator());
Collection.sort(dogList,new Dog());
Run Code Online (Sandbox Code Playgroud)
在情况 2 中,即使他们说 Comparator 没有修改原始类,原始类实际上也被修改了,这不是真的吗?
可能如果我没有正确理解这个概念,请启发我。
我需要一个 Kotlin 中的比较器来对进入我的回收器视图中的每个对象进行排序(使用快速适配器)。
我需要按整数对对象进行排序,其中最大的排在前面。
上面的代码对进入列表的对象进行排序,但最大的对象位于列表的末尾。有办法颠倒顺序吗?
playersFastAdapter.itemAdapter.withComparator(compareBy({ it.player.goals }, {it.player.assists}), true)
Run Code Online (Sandbox Code Playgroud) 我有以下片段:
Comparable<C> a = ...;
Comparable<C> b = ...;
Comparable<C> min = a.compareTo(b) <= 0 ? a : b;
Run Code Online (Sandbox Code Playgroud)
这类似于Math.min(a, b),但在上定义Comparable。
我知道三元运算符已经很短了,但是我无法内联aand 的表达式,b因此我认为是min(a, b)。max(a, b)更容易理解。
我知道有几个流响应。收集一组值的函数,例如:
Stream.of(a, b).min(Comparator.naturalOrder())
Run Code Online (Sandbox Code Playgroud)
这将有助于内联表达式,但是对于这样的小任务,我仍然觉得很难阅读并且开销太大。
目前,我正在使用自己的实用程序功能,但我很感兴趣:
如何以一种可读性和独立于库的方式找到两个可比对象中的最小值,而又没有太多的性能开销?
IComparable在大多数地方,我读到从类和类中继承是一个好主意,IComparable<T>以提供与非泛型集合的兼容性。我的问题是为什么默认情况下IComparable<T>不继承呢?IComparable通过这种方式可以实现向后兼容性。
例如,IEnumerable<T>是否继承自IEnumerable,那么为什么不使用 做同样的事情呢IComparable?我读到了有关差异的内容,但我仍然不太明白为什么要这样设计。我尝试创建一个具有相同架构的接口的小示例,并且它按预期工作。
编辑:此外,Microsoft 文档指出以下内容:如果泛型接口是逆变的,则泛型接口可以从非泛型接口继承,这意味着它仅使用其类型参数作为返回值。在 .NET Framework 类库中,IEnumerable<T>继承自IEnumerable,因为仅在属性 getterIEnumerable<T>的返回值GetEnumerator和属性 getter 中使用 T。Current
但尽管如此,我们仍然可以很好地继承IComparable(IComparable<T>如果您创建具有相同架构的接口)。
comparable ×10
java ×7
comparator ×2
compareto ×2
interface ×2
sorting ×2
android ×1
c# ×1
collections ×1
conventions ×1
extends ×1
generics ×1
hash ×1
icomparable ×1
javabeans ×1
kotlin ×1
ruby ×1