用Java为Pair编写比较器<;字符串,日期>;
我有一个包含字符串和日期列表的对象:用Java为Pair编写比较器<;字符串,日期>;,java,casting,comparator,Java,Casting,Comparator,我有一个包含字符串和日期列表的对象: List<Pair<String, Date>> res; 列表res; 然后我写了一个比较器 Comparator mycomp = new Comparator() { @Override public int compare(Object o1, Object o2) { if ((o1.getClass().equals(ImmutablePair.class))
List<Pair<String, Date>> res;
列表res;
然后我写了一个比较器
Comparator mycomp = new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if ((o1.getClass().equals(ImmutablePair.class))
&& (o2.getClass().equals(ImmutablePair.class))) {
Pair<Integer, Date> p1 = (Pair<Integer, Date>) o1;
Pair<Integer, Date> p2 = (Pair<Integer, Date>) o1;
return comPair(p1, p2);
}
throw new AssertionError("Unknown Types");
}
public int comPair(Pair<Integer, Date> p1, Pair<Integer, Date> p2) {
return p1.getValue().compareTo(p2.getValue());
}
};
Comparator mycop=new Comparator(){
@凌驾
公共整数比较(对象o1、对象o2){
if((o1.getClass().equals(ImmutablePair.class))
&&(o2.getClass().equals(ImmutablePair.class))){
对p1=(对)o1;
对p2=(对)o1;
返回comPair(p1,p2);
}
抛出新的断言错误(“未知类型”);
}
公共int公司(p1对,p2对){
返回p1.getValue().compareTo(p2.getValue());
}
};
这是可行的,但我收到了几个警告
第一行:
比较器是一种原始类型。对泛型比较器的引用应参数化
p1和p2的铸造:
类型安全:从对象
到对
对于铸造,我想我是在用对检查类型。
对于声明,Comparator mycop=new Comparator()
,我尝试放置新的比较器(对)
我得到以下结果:
- 比较器是一种原始类型。对泛型
比较器的引用应参数化
- 标记“>”上出现语法错误,此标记后应为表达式
如果我尝试输入一个对象名
Comparator mycop=新的比较器(对obj)
我得到了各种各样的错误,找不到Pair和String,并且没有导入它们的选项
那么我做错了什么呢?就用这个吧
Comparator<Pair<String, Date>> mycomp = new Comparator<Pair<String, Date>>(){
就用这个
Comparator<Pair<String, Date>> mycomp = new Comparator<Pair<String, Date>>(){
将比较器专用于配对
java.util.Comparator mycomp = new java.util.Comparator<Pair<Integer, Date>>() {
@Override
public int compare(Pair<Integer, Date> pair1, Pair<Integer, Date> pair2) {
// compare here.
}
};
java.util.Comparator mycop=new java.util.Comparator(){
@凌驾
公共整数比较(对pair1,对pair2){
//在这里进行比较。
}
};
Pair可以在应用程序中的许多地方使用,所以您也可以将其作为单独的类来使用,并在其他地方重用对象
public class MyPairComparator implements Comparator<Integer,Date> {
public int compare(Pair<Integer, Date> pair1, Pair<Integer, Date> pair2) {
// compare here.
}
}
// To Use
Collections.sort( pairs, new MyPairComparator());
公共类MyPairComparator实现Comparator{
公共整数比较(对pair1,对pair2){
//在这里进行比较。
}
}
//使用
Collections.sort(pairs,new MyPairComparator());
将比较器专用于配对
java.util.Comparator mycomp = new java.util.Comparator<Pair<Integer, Date>>() {
@Override
public int compare(Pair<Integer, Date> pair1, Pair<Integer, Date> pair2) {
// compare here.
}
};
java.util.Comparator mycop=new java.util.Comparator(){
@凌驾
公共整数比较(对pair1,对pair2){
//在这里进行比较。
}
};
Pair可以在应用程序中的许多地方使用,所以您也可以将其作为单独的类来使用,并在其他地方重用对象
public class MyPairComparator implements Comparator<Integer,Date> {
public int compare(Pair<Integer, Date> pair1, Pair<Integer, Date> pair2) {
// compare here.
}
}
// To Use
Collections.sort( pairs, new MyPairComparator());
公共类MyPairComparator实现Comparator{
公共整数比较(对pair1,对pair2){
//在这里进行比较。
}
}
//使用
Collections.sort(pairs,new MyPairComparator());
我确实了解原始类型是什么(至少在一般情况下),但我不明白我为什么不在这里进行类型检查。当您应该使用比较器时,您正在使用比较器。请您在订购之前和之后提供一些数据示例。是的,但我必须编写一些内容,因为我现在阅读的数据太多了。给我一点时间。谢谢,我刚从每个人身上取了前10个。大约有45000条记录,所以您不会看到相同的记录,只是它是按日期排序的。也,这些评论的格式不正确,所以我提前道歉:1021639:2004-09-1014:31:20.3771992434:2008-04-09 15:30:29.36 3042521:2010-03-24 16:54:26.047 640574:2002-12-13 17:12:59.18 680149:2003-02-06 10:32:45.99390468:1999-10-07 18:05:40.0 1282319:2005-10-25 13:30:30.51 959208:2004-06-03:21:262418554:2008-12-03 15:38:59.7 2545436:2009-03-02 11:49:42.47我确实了解原始类型是什么(至少在一般情况下),但我不明白我为什么不在这里进行类型检查。当您应该使用比较器时,您正在使用比较器。请您在订购前后提供一些数据示例。是的,但我必须写点东西,因为我现在读的数据太多了。给我一点时间。谢谢,我刚从每个人身上取了前10个。大约有45000条记录,所以您不会看到相同的记录,只是它是按日期排序的。也,这些评论的格式不正确,所以我提前道歉:1021639:2004-09-1014:31:20.3771992434:2008-04-09 15:30:29.36 3042521:2010-03-24 16:54:26.047 640574:2002-12-13 17:12:59.18 680149:2003-02-06 10:32:45.99390468:1999-10-07 18:05:40.0 1282319:2005-10-25 13:30:30.51 959208:2004-06-03:21:262418554 : 2008-12-03 15:38:59.7 2545436 : 2009-03-02 11:49:42.47