阿里云
发表主题 回复主题
  • 842阅读
  • 0回复

[交流乐园]java中对ArrayList进行排序

级别: 新人
发帖
7
云币
14
[t]X/O3<  
ava如何对ArrayList中对象按照该对象某属性排序 s=I'e/"7  
(从小到大) Ma-\^S=  
两种方法: ?|;yVew  
@{ ;XZb^  
}}{!u0N},V  
[tr=none]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
方法一:<br data-filtered="filtered">Comparator<KNNNode> comparator = new Comparator<KNNNode>() {         @Override        public int compare(KNNNode o1, KNNNode o2) {            System.out.println("进入 compare function");            // TODO Auto-generated method stub            if (o1.getDistance() <= o2.getDistance()) {//o1属性小于等于o2属性时,返回1                return 1;            } else {                return -1;            }        }     }; {.e=qQ%P5)  
}S_oH9A  
调用方法: Zc& &[g  
~rbIMF4T`]  
SJ' % ^  
[tr=none]
1
2
3
List<KNNNode> nodelist = new ArrayList<KNNNode>();//此处需要填充nodelist.add();Collections.sort(nodelist, comparator);// 调用 6`01EIk  
u?+bW-D'd  
Vsq8H}K  
dpZ;l 9  
[V# r7a  
3. WF}8  
[tr=none]
1
2
3
4
5
6
7
8
9
10
11
方法二:<br>class SortBy implements Comparator {        public int compare(Object obj1, Object obj2) {            KNNNode point1 = (KNNNode) obj1;            KNNNode point2 = (KNNNode) obj2;            if (point1.getDistance() <= point2.getDistance()) {                return 1;            } else {                return 0;            }        }    } VU|dV\>  
wz8PtfZ  
调用方法: :Gqy>)CxX  
aiCFH_H4;L  
g{s'GyV8t  
[tr=none]
1
Collections.sort(nodelist, new SortBy()); .?C%1a&_l  
KNNNode结点内的属性: W\*-xf|"d  
Zwt;d5U  
ihL/n  
[tr=none]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package newKNN; /** * KNN结点类,用来存储最近邻的k个元组相关的信息 */public class KNNNode {    private int index; // 元组标号    private double distance; // 与测试元组的距离    private String seq;//滑动窗口motif序列片段         public KNNNode(int index, double distance, String seq) {        super();        this.index = index;        this.distance = distance;        this.seq = seq;             }    public int getIndex() {        return index;    }    public void setIndex(int index) {        this.index = index;    }    public double getDistance() {        return distance;    }    public void setDistance(double distance) {        this.distance = distance;    }    public String getSeq() {        return seq;    }    public void setSeq(String seq) {        this.seq = seq;    }     } @!=\R^#p  
我想要按照KNNNode的distance属性排序。 dBC bL.!  
6V9doP]i  
RH)EB<PV  
若要按照从大到小的顺序排序,如果只是将 1tzV8(7  
>=返回值改为-1 H( -Y  
<返回值改为1 会出现异常。 6" T['6:j  
Comparison method violates its general contract! w [>;a.$  
原因是是实现接口Comparator 的类有问题在compare中没有处理好两个比较的对象相等的情况 RH0>ZZR  
public int compare(KNNNode o1, KNNNode o2);即 o1.getDistance() == o2.getDistance() 时,返回值不是0 [ Mg8/Oy  
我最终的实现代码是:(从大到小排序) +y+"Fyl  
|8_JY2 R  
 /RZR}  
[tr=none]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Comparator<KNNNode> comparator = new Comparator<KNNNode>() {         @Override        public int compare(KNNNode o1, KNNNode o2) {            // TODO Auto-generated method stub            if (o1.getDistance() < o2.getDistance()) {                return 1;            } else if(o1.getDistance() > o2.getDistance()){                return -1;            }else{                return 0;            }        }     };
关键词: java
发表主题 回复主题
« 返回列表上一主题下一主题

限100 字节
如果您在写长篇帖子又不马上发表,建议存为草稿
 
验证问题: 阿里云官网域名是什么? 正确答案:www.aliyun.com
上一个 下一个