看起来您通常在java.lang.Comparable不指定类型参数的情况下实现接口。
public abstract class Area implements Comparable {
@Override
public int compareTo(Object other) {
if (other instanceof Area)
return new Double(getArea()).compareTo(other.getArea());
return -1; // or something else
}
abstract public double getArea();
}
Run Code Online (Sandbox Code Playgroud)
由于我只想将苹果与苹果进行比较,因此我认为指定类型是有意义的。
public abstract class Area implements Comparable<Area> {
@Override
public int compareTo(Area other) {
// ...
Run Code Online (Sandbox Code Playgroud)
如果我想介绍另一个类进行比较Area,我想我可以这样做:
public abstract class Area implements Comparable<Area>, Comparable<Volume> {
@Override
public int compareTo(Area other) {
// ...
}
@Override
public int compareTo(Volume other) {
// ...
} …Run Code Online (Sandbox Code Playgroud) 假设一个对象有一个 int ID 号字段,但没有实现 Comparable。我想按照我自己的排序标准对这些对象使用 Collections.sort() 。是否有可能做到这一点?
我知道 Comparable 是一个接口,但我看到了一些代码,例如ArrayList<Comparable>, public Comparable f(), public void f(Comparable a)。似乎 Comparable 是一个类。上面这些代码是如何实现的?
import java.util.*;
public class MinHeap
{
public MinHeap()
{
elements = new ArrayList<Comparable>();
elements.add(null);
}
....
}
public class BinarySearchTree
{
...
public void add(Comparable obj)
{
Node newNode = new Node();
newNode.data = obj;
newNode.left = null;
newNode.right = null;
if (root == null) root = newNode;
else root.addNode(newNode);
}
...
}
Run Code Online (Sandbox Code Playgroud) 这是我想要获取的代码。
public static final Comparator<Youku> AscDurRevCreationDate =
Comparator.comparing(Youku::getDuration)
.reversed()
.thenComparing(Youku::getDateCreation)
.reversed();
Run Code Online (Sandbox Code Playgroud)
下面的代码是我试图将其转换为的代码。但是,我从下面的代码中得到了一些不同的结果。顺便说一句,我在这里使用 Duration 对象。
@Override
public int compare(Youku obj1, Youku obj2) {
Integer duration = obj1.getDuration().compareTo(obj2.getDuration());
Integer dateCreation = obj2.getDateCreation().compareTo(obj1.getDateCreation());
return duration.compareTo(dateCreation );
}
Run Code Online (Sandbox Code Playgroud) 我需要创建一个参数类,其中我有两个构造函数,第一个有一个比较器作为参数,第二个没有任何参数,但只有当参数实现可比较或否则抛出异常时,我才能使用它。
为了更清楚,我需要做这样的事情:
class Storage<T>{
private Comparator<? super T> comparator = null;
public Storage() {
//T sould implement comparable, but how I can check it?
}
public Storage(Comparator<? super T> t){
//T doesn't implement comparable but i can use comparator!
comparator = t
}
public static void main(String[] args) {
//Just a test
Comparator<prova> comp = (a, b) -> 1;
MinMaxStorage<Integer> uno = new MinMaxStorage<>();
//Should thow an exception
MinMaxStorage<NotComparable> due = new MinMaxStorage<>();
//Should be ok
MinMaxStorage<NotComparable> due = new …Run Code Online (Sandbox Code Playgroud) 创建一个Map可以按值排序的.
代码按预期执行,但不能干净地编译:
public class SortableValueMap<K, V> extends LinkedHashMap<K, V> {
...
public void sortByValue() {
...
Collections.sort( list, new Comparator<Map.Entry>() {
public int compare( Map.Entry entry1, Map.Entry entry2 ) {
return ((Comparable)entry1.getValue()).compareTo( entry2.getValue() );
}
});
...
Run Code Online (Sandbox Code Playgroud)
Comparable作为通用参数传递给Map.Entry<K, V>(V必须是Comparable?)的语法- 以便(Comparable)警告中显示的类型转换可以被删除 - 包括我.
编译器的cantankerous抱怨:
SortableValueMap.java:24:警告:[unchecked] unchecked调用compareTo(T)作为原始类型java.lang.Comparable的成员
Run Code Online (Sandbox Code Playgroud)return ((Comparable)entry1.getValue()).compareTo( entry2.getValue() );
如何在没有任何警告的情况下将代码更改为编译(在编译时不会抑制它们-Xlint:unchecked)?
谢谢!
Comparable接口是如何标记接口的,即使它定义了一个compareTo()方法?请详细说明.
我正在尝试从CLRS实现Dijsktra的算法 - 算法入门书,但是,我在实现带Comparator接口的优先级队列方面遇到了麻烦.这是我的Vertex课程,你可以看到;
public class Vertex {
public boolean explored;
public int vertexID;
public LinkedList<Vertex> adjacencyList;
public LinkedList<Edge> edgeSet;
public int shortestDistance;
public Vertex predecessor;
public Vertex(int vertexID){
this.vertexID = vertexID;
this.explored = false;
this.adjacencyList = new LinkedList<>();
this.edgeSet = new LinkedList<>();
this.shortestDistance = Integer.MAX_VALUE;
this.predecessor = null;
}
}
Run Code Online (Sandbox Code Playgroud)
所以最初shortestDistance属性被声明为Integer.MAX_VALUE.此外,您可以看到从Comparator实现的类用于优先级队列.
public class WeightComparator implements Comparator<Vertex> {
@Override
public int compare(Vertex o1, Vertex o2) {
return Math.min(o1.shortestDistance, o2.shortestDistance);
}
}
Run Code Online (Sandbox Code Playgroud)
我确信整个实现由于我的一些测试没有任何逻辑错误,但是,在某些测试中它失败了.我用这个语句创建了对队列的引用
PriorityQueue<Vertex> queue = …
我正在从我的教科书中复制一个例子,但它拒绝编译.我在某个地方打错了吗?出于某种原因,在客户端代码上,Collections.sort(words)不允许程序编译.任何帮助表示赞赏.代码复制自Stuart Reges和Marty Stepp的"构建Java程序"第2版.我试图通过复制来理解它.
该程序应该创建一个CalendarDate对象以放入ArrayList.通过实现CalendarDate的Comparable接口,我可以使用Collections.sort按顺序在该arraylist中对生日进行排序.但是,这不起作用b/c Collections.sort(日期)将无法运行.
客户端代码(包含问题):
import java.util.*;
// Short program that creates a list of birthdays of the
// first 5 U.S. Presidents and that puts them into sorted order.
// We can now use Collections.sort for ArrayList<CalendarDate> b/c CalendarDate implements the Comparable interface.
public class CalendarDateTest {
public static void main(String[] args) {
ArrayList<CalendarDate> dates = new ArrayList<CalendarDate>(); // Creates a new ArrayList of 'CalendarDate' object type.
// adds a new CalendarDate object with month = 2 and …Run Code Online (Sandbox Code Playgroud) public int compareTo(Person p) {
int res = 1;
String personStr = p.getId();
String thisId = this.getId();
if(thisId.equals(personStr)){
res = 0;
}
else if(thisId.compareTo(personStr)){
res = -1;
}
return res;
}
Run Code Online (Sandbox Code Playgroud)
我已经实现了一个非常简单的compareTo方法,但是我没有收到错误消息.如果statemint中的条件给我一条消息,说它不能从int转换为boolean.我明白了,但问题是我正在使用netiher.我只是想比较两个简单的字符串,为什么会这样呢?
comparable ×10
java ×10
comparator ×4
collections ×3
interface ×3
sorting ×3
generics ×2
arraylist ×1
compare ×1
compareto ×1
object ×1
oop ×1
terminology ×1