comparable

How to use the Comparable CompareTo on Strings in Java

陌路散爱 提交于 2019-11-30 08:19:17
I can use it to sort by emp id but I'm not sure if it is possible to compare strings. I get an error the operator is undefined for strings. public int compareTo(Emp i) { if (this.getName() == ((Emp ) i).getName()) return 0; else if ((this.getName()) > ((Emp ) i).getName()) return 1; else return -1; What you need to use is the compareTo() method of Strings. return this.getName().compareTo(i.getName()); That should do what you want. Usually when implementing the Comparable interface, you will just aggregate the results of using other Comparable members of the class. Below is a pretty typical

Create a compareTo to a Generic Class that Implements Comparable

人走茶凉 提交于 2019-11-30 08:17:16
I have a Generic Class with two type variables, which implements java.lang.Comparable. public class DoubleKey<K,J> implements Comparable<DoubleKey<K,J>>{ private K key1; private J key2; public DoubleKey(K key1, J key2){ this.key1 = key1; this.key2 = key2; } public K getFirstKey(){ return this.key1; } public J getSecondKey(){ return this.key2; } // need for Comparable interface public int compareTo(DoubleKey<K,J> aThat){ ... } } Becuase i implemeted it with Comparable, I need to write the compareTo() method. Because K, J can be of ANY type, I'm having problems on how to compare them completely.

Comparable 和 Comparator 的区别

雨燕双飞 提交于 2019-11-30 07:16:07
Java 中为我们提供了两种比较机制:Comparable 和 Comparator,他们之间有什么区别呢?今天来了解一下。 Comparable 自然排序 Comparable 在 java.lang 包下,是一个接口,内部只有一个方法 compareTo(): public interface Comparable<T> { public int compareTo(T o); } Comparable 可以让实现它的类的对象进行比较,具体的比较规则是按照 compareTo 方法中的规则进行。这种顺序称为 自然顺序。 compareTo 方法的返回值有三种情况: e1.compareTo(e2) > 0 即 e1 > e2 e1.compareTo(e2) = 0 即 e1 = e2 e1.compareTo(e2) < 0 即 e1 < e2 注意: 1.由于 null 不是一个类,也不是一个对象,因此在重写 compareTo 方法时应该注意 e.compareTo(null) 的情况,即使 e.equals(null) 返回 false,compareTo 方法也应该主动抛出一个空指针异常 NullPointerException。 2.Comparable 实现类重写 compareTo 方法时一般要求 e1.compareTo(e2) == 0 的结果要和 e1

Implementing Comparable with a generic class

て烟熏妆下的殇ゞ 提交于 2019-11-30 04:59:57
I want to define a class that implements the generic Comparable interface. While in my class I also defined a generic type element T . In order to implement the interface, I delegate the comparison to T . Here is my code: public class Item<T extends Comparable<T>> implements Comparable<Item> { private int s; private T t; public T getT() { return t; } @Override public int compareTo(Item o) { return getT().compareTo(o.getT()); } } When I try to compile it, I get the following error information: Item.java:11: error: method compareTo in interface Comparable<T#2> cannot be applied to given types;

Should Comparable ever compare to another type?

删除回忆录丶 提交于 2019-11-30 03:55:09
I'm wondering if there's ever a valid use case for the following: class Base {} class A implements Comparable<Base> { //... } It seems to be a common pattern (see Collections for a number of examples) to accept a collection of type T , where T extends Comparable<? super T> . But it seems technically impossible to fulfill the contract of compareTo() when comparing to a base class, because there's no way to ensure that another class doesn't extend the base with a contradictory comparison. Consider the following example: class Base { final int foo; Base(int foo) { this.foo = foo; } } class A

Does a natural comparator exist in the standard api?

孤街浪徒 提交于 2019-11-30 01:10:09
I need a comparator as part of a strategy pattern that can either use the natural ordering of the objects or some custom ordering. For the natural ordering case, I wrote a simple comparator: private static class NaturalComparator<T extends Comparable<? super T>> implements Comparator<T> { @Override public int compare(T o1, T o2) { return o1.compareTo(o2); } } Seems simple enough, but I was wondering if anyone knew of one in the standard API. I looked at TreeMap, and it does it without such a class, so when that code was written, the apparent answer would be no, but perhaps it was added later.

Generic class that conforms to Comparable in Swift

喜你入骨 提交于 2019-11-29 19:54:10
问题 I'm attempting to create a simple generic node class that conforms to the Comparable protocol so that I can easily compare nodes without accessing their key. When I attempt to write the < and == functions, however, the compiler doesn't seem to like it. The < and == functions expect a type when defining the Node parameters. This was simple in Java where you defined equality and < internally to the class. Swift asks for it globally. Any thoughts ? Example: func < (lhs:Node<E:Comparable>, rhs

How to implement an interface with an enum, where the interface extends Comparable?

萝らか妹 提交于 2019-11-29 18:04:24
问题 Consider this code: public interface Foo extends Comparable<Foo> {} public enum FooImpl implements Foo {} Due to the restrictions of type erasure, I receive the following error: java.lang.Comparable cannot be inherited with different arguments: <Foo> and <FooImpl> I have the following requirements: FooImpl needs to be an enum, because I need to use it as a default value in annotations. The contract of my interface is that it needs to be comparable. I already tried using generic bounds in the

how can I implement Comparable more than once?

一个人想着一个人 提交于 2019-11-29 17:47:49
问题 I'm upgrading some code to Java 5 and am clearly not understanding something with Generics. I have other classes which implement Comparable once, which I've been able to implement. But now I've got a class which, due to inheritance, ends up trying to implement Comparable for 2 types. Here's my situation: I've got the following classes/interfaces: interface Foo extends Comparable<Foo> interface Bar extends Comparable<Bar> abstract class BarDescription implements Bar class FooBar extends

can StringBuffer objects be keys in TreeSet in Java?

送分小仙女□ 提交于 2019-11-29 16:44:01
I have the following code where I am trying to put the StringBuffer objects as keys in a TreeSet. The reason I do this is to see if I can put mutable objects as keys. I do not get any compile error. but when I run this code, I get the error that is below the code. specially, I get this java.lang.StringBuffer cannot be cast to java.lang.Comparable . what does this error indicate? from javadoc I see that StringBuffer class is declared final ( public final class StringBuffer ), doesn't that mean it is immutable and hence hashable? I am a newbie to the hashing and immutable stuff, so kindly help