java 比较接口,Java中的比较和比较器接口

本文讨论了在Java中创建一个Pair类,要求键和值都实现Comparable接口时遇到的编译警告和错误。作者尝试通过添加类型参数解决警告,但在创建Comparator时遇到了问题。错误提示表明无法确定比较的对象是否属于同一类型。解决方案是明确指定泛型参数的比较类型,以确保比较操作的正确性。
摘要由CSDN通过智能技术生成

I want to write a generic Pair class, which has two members: key and value. The only requirement to this class is that both key and value should implements the Comparable interface, otherwise Pair class will not accept them as type parameter.

First I code it like this:

public class Pair

But the JDK 1.6 compiler will generate warning about this:

Comparable is a raw type. References to generic type Comparable should be parameterized

Then I tried to add type parameters and the code now looks like this:

public class Pair,

T2 extends Comparable extends Object>>

Now everything go well until I tried to generate an Comparator for Pair.(The following code is in Pair class)

public final Comparator> KEY_COMPARATOR = new Comparator>() {

public int compare(Pair first, Pair second) {

*first.getKey().compareTo(second.getKey());*

return 0;

}

};

The code first.getKey().compareTo(second.getKey()); will generate an error saying:

The method compareTo(capture#1-of ? extends Object) in the type Comparable is not applicable for the arguments (T1)

Anyone knows what does this error message mean?

Any hints on this topic are welcome.

UPDATE:

Here is the complete code:

public class Pair, T2 extends Comparable extends Object>> {

private T1 key;

private T2 value;

public static int ascending = 1;

public final Comparator> KEY_COMPARATOR = new Comparator>() {

public int compare(Pair first, Pair second) {

int cmp = first.getKey().compareTo((T1)(second.getKey()));

if (cmp > 0) return ascending;

return -ascending;

}

};

}

@MarvinLabs Can you explain a bit more why the compiler cannot make sure objects are compared to other objects of the same type. In the above code, second.getKey() returns T1 type, which is of the same type as first.getKey()

解决方案

Lets get a look at interface design first.

public interface Comparable {

public int compareTo(T o);

}

It is quite typical we must say. So if our class needs to implement it we do this.

pubilc class ICanComparteWithMyself implements Comparable {

public int compareTo(ICanComparteWithMyselfo)

//code for compration

}

As we see the generic parameter type, determine on what we will operate, so for generics we act in the same way

public class ICanCompareMyGeneric implements Comparable {

public int compareTo(T o)

//code for compration

}

}

In your case whawt we want it to assure that generic parameters implements is own Comparable, for that we need to do this

public class MyGenericCanCompareToItself> {

}

As we can see, this is quite common to use. The limitation expected (or not) is that we can work on classes that implement Comparable for it self type. If we had

public class ICanCompareStrings implements Comparable {

public int compareTo(String o)

//code for compration

}

}

So for class MyGenericCanCompareToItself as generic parameter we can use class public MyGenericCanCompareToItself but not ICanCompareStrings.

EDIT:

So when we covered the basics now we can go to solve, your problem

Your class description looks like this

public class Pair, T2 extends Comparable extends Object>>

This do not have much sensce as is more less the same as >

This description says:

I am a Pair class, that work with two generic parameters, that can use comparison over something that I do not know.

With this code you cann not progress before the generic parameters do not know on that then operate there for you end up with something like this.

first.getKey.compareTo(null);

And this why you code do not compile when you try to cast, the expected type is null.

To change that you need to qualify on what type your generic parameters should be comparable.

For example the can compare on itselft

public class Pair, T2 extends Comparable>

This descriptions says:

I am a Pair class that works with two parameters, that each of them can be compared with itself.

And this is what you probably are looking for, additionally they can be compared on something that can be super class of T1 or T2

public class Pair, T2 extends Comparable super T2>>

This description says:

I am a Pair class that works with two parameters, that each of them can be compared with classes that deliver from them.

I hope this helps you out with generics ;-).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值