我正在寻找C#IComparable的Scala对应物,我找到了可比较的特性.我的意思是 - 提到可比较,但是当我在http://www.scala-lang.org/api/current/scala/搜索它时,我得到0次点击.由于这个名字,使用Google我得到了很多"Scala如何与......相比"的结果.
有什么资源我可以读到这个神秘的可比较的东西吗?在所有Ordered延伸之后,它必须存在.
如果我有一个类Person实现Comparable(比较personA.height到personB.height,例如),是有可能使用
personA < personB
Run Code Online (Sandbox Code Playgroud)
作为替代品
personA.compareTo(personB) == -1?
Run Code Online (Sandbox Code Playgroud)
这样做有什么问题,还是我需要重载操作符?
我喜欢新的静态工厂方法Comparator,因为它们允许以非常简洁且不易出错的方式实现比较器.
但推荐的实施方式是Comparable什么?我们应该在Comparable实现中使用Comparators吗?
public MyClass implements Comparable<MyClass>{
...
public int compareTo(MyClass other){
Comparator<MyClass> naturalOrderComparator =
Comparator.comparing(MyClass::getFoo)
.thenComparing(MyClass::getBar);
return naturalOrderComparator.compare(this, other);
}
}
Run Code Online (Sandbox Code Playgroud)
甚至在对大型集合进行排序时使用静态比较器来减少大量对象的创建:
public MyClass implements Comparable<MyClass>{
private static final Comparator<MyClass> NATURAL_ORDER_COMPARATOR =
Comparator.comparing(MyClass::getFoo)
.thenComparing(MyClass::getBar);
...
public int compareTo(MyClass other){
return NATURAL_ORDER_COMPARATOR.compare(this, other);
}
}
Run Code Online (Sandbox Code Playgroud)
或者是否有另一种推荐的方法来实现Comparable与Java SE 8?
我想要实现的是通过字符串值对对象的集合进行排序.但是,使用collator以依赖于语言环境的方式.由于性能原因,我不想使用Collator compare()方法(如下面的代码中)而不是CollationKey类,因为java API声明使用CollationKey要快得多.
但是如何使用CollationKey实现compareTo()方法?据我所知,如果我将使用CollationKey,我必须自己完全编写所有的比较方法.所以我甚至不再能够使用Collections.sort()方法......我非常感谢一个易于理解的示例,以及使用CollationKey对Person对象的Collection进行排序的最有效实现.
谢谢!
public class Person implements Comparable<Person> {
String lastname;
public int compareTo(Person person) {
//This works but it is not the best implementation for a good performance
Collator instance = Collator.getInstance(Locale.ITALY);
return instance.compare(lastname, person.lastname);
}
}
...
ArrayList list = new ArrayList();
Person person1 = new Person("foo");
list.add(person1);
Person person2 = new Person("bar");
list.add(person2);
Collections.sort(list);
...
Run Code Online (Sandbox Code Playgroud) 假设我有一个名为foo的方法,将2个Object作为参数.两个对象属于同一类型,并且都实现了可比较的接口.
void foo(Object first, Object second){
if (!first.getClass().isInstance(second)) //first and second of the same type
return;
Comparable firstComparable = (Comparable)first; //WARNING
Comparable secondComparable = (Comparable)second; //WARNING
int diff = firstComparable.compareTo(secondComparable); //WARNING
}
Run Code Online (Sandbox Code Playgroud)
前2个警告是:
可比较是原始类型.应参数化对泛型类型Comparable的引用
最后警告:
类型安全:方法compareTo(Object)属于原始类型Comparable.应参数化对泛型类型Comparable的引用
我怎么能重构我的代码才能删除这些警告?
编辑:我可以不改变foo方法的签名吗?
我上课了
public class StudentVO {
int age;
String name;
}
Run Code Online (Sandbox Code Playgroud)
我在两个不同的领域使用了同一个班级.在一个地方,我需要根据年龄排序.在另一个地方,我需要根据名称进行排序,在另一个地方,我可能需要根据年龄和名称进行排序.我怎样才能做到这一点?如果我可以覆盖一个字段compareTo().
是否有可能做到这一点?
我刚刚学习了优先级队列,并且认为我会尝试使用类似的界面来表现它.
代码片段:
import java.util.PriorityQueue;
class kinga implements Comparable<Double> {
double time=909.909;
double d;
public kinga(double a) {
this.d=a;
}
public int compareTo(Double d) {
return Double.compare(d, time);
}
public static void main(String arg[]) {
PriorityQueue<kinga> r=new PriorityQueue<kinga>();
r.add( new kinga(4545.45));
r.add( new kinga(45.4));
r.add( new kinga(1235.45));
System.out.println(r.poll()+" "+r.poll()+" "+r.poll());
}
}
Run Code Online (Sandbox Code Playgroud)
它编译但在线程"main"中 给出了Exception java.lang.ClassCastException: kinga cannot be cast to java.lang.Double.
这里有什么问题.有人能告诉我可比性和优先级队列的工作原理吗?
它很详细,但我想知道为什么会这样.
示例代码:
Class klasa = Enum.class;
for(Type t : klasa.getGenericInterfaces())
System.out.println(t);
Run Code Online (Sandbox Code Playgroud)
计划的输出:
java.lang.Comparable<E>
interface java.io.Serializable
Run Code Online (Sandbox Code Playgroud)
为什么输出之前没有接口字java.lang.Comparable<E>.这是界面,是吗?
在我看来,输出应该是:
**interface** java.lang.Comparable<E>
interface java.io.Serializable
Run Code Online (Sandbox Code Playgroud)
可比较是特别对待?
我正在尝试创建一个符合Comparable协议的简单通用节点类,以便我可以轻松地比较节点而无需访问其密钥.但是,当我尝试编写<和==函数时,编译器似乎并不喜欢它.<和==函数在定义Node参数时需要一种类型.这在Java中很简单,您在其中定义了相等性并且<在内部到类.斯威夫特在全球范围内要求它.有什么想法吗 ?
例:
func < (lhs:Node<E:Comparable>, rhs:Node<E:Comparable>) -> Bool {
return lhs.key < rhs.key
}
func == (lhs:Node<E:Comparable>, rhs:Node<E:Comparable>) -> Bool {
return lhs.key == rhs.key
}
class Node<D:Comparable>: Comparable {
var key: D!
var next:Node?
var prev:Node?
init( key:D ) {
self.key = key
}
}
Run Code Online (Sandbox Code Playgroud) 编辑:方法签名
public Comparable[][] findCommonElements(Comparable[][] collections)
Run Code Online (Sandbox Code Playgroud)
是错的.它应该是
public Comparable[] findCommonElements(Comparable[][] collections)
Run Code Online (Sandbox Code Playgroud)
但在我的IDE中更改它会让一切都变得混乱.我几乎觉得自己已经超出了我的知识范围,因为我并不完全理解Set,而2D数组让我很糟糕.
我需要编写一个带有两个Comparable数组的算法,以线性时间效率迭代它们,并显示公共元素.我已经读过使用HashSet会给我最快的时间效率,但我已陷入僵局.原因如下:
我们得到了指令和一行代码,这是方法签名
public Comparable[][] findCommonElements(Comparable[][] collections)
Run Code Online (Sandbox Code Playgroud)
这意味着我必须返回2d数组,"集合".我通过电子邮件发送了我的教授使用HashSets,我得到了批准,除了我有这个问题:
"你可以在你的findCommonElements方法中使用HashSet,但是你需要能够计算执行的比较次数.尽管散列通常非常有效,但是在发生碰撞时会进行一些比较.为此,你需要可以访问您使用的HashSet的源代码.您还需要在CommonElements类中使用"getComparisons()"方法来返回比较次数."
在两个学期的编程中,我没有学习HashSets,Maps,Tables等.我正在尝试自己学习,我并不完全理解碰撞.
我的代码确实采用了两个数组并返回了公共元素,但是我的返回语句很复杂,因为我基本上编写它所以它会编译(2d Comparable数组是参数).
我在正确的道路上吗?这是代码:
public class CommonElements {
static Comparable[] collection1 = {"A", "B", "C", "D", "E"}; //first array
static Comparable[] collection2 = {"A", "B", "C", "D", "E", "F", "G"}; //second array
static Comparable[][] collections = {collection1, collection2}; //array to store common elements.
static Set<Comparable> commonStuff = new HashSet<>(); //instance of Set containing common elements
public …Run Code Online (Sandbox Code Playgroud) comparable ×10
java ×8
comparator ×2
arrays ×1
compareto ×1
generics ×1
hashset ×1
iequatable ×1
java-8 ×1
operators ×1
raw-types ×1
scala ×1
swift ×1
traits ×1
warnings ×1