List排序Collections.sort()

方式一: 使用public static <T extends Comparable<? super T>> void sort(List<T> list)方法对list进行排序:

在要排序的list对象中 implements Comparable 并重写compareTo(方法)

public class ClickInfo implements Comparable<ClickInfo>{

private Integer cid;

private String imageId;

private Long clickCount;

private Long updateTime;

public Integer getCid() {

return cid;

}

public void setCid(Integer cid) {

this.cid = cid;

}

public String getImageId() {

return imageId;

}

public void setImageId(String imageId) {

this.imageId = imageId;

}

public Long getClickCount() {

return clickCount;

}

public void setClickCount(Long clickCount) {

this.clickCount = clickCount;

}

public Long getUpdateTime() {

return updateTime;

}

public void setUpdateTime(Long updateTime) {

this.updateTime = updateTime;

}

@Override

public String toString() {

return "ClickInfo{" +

"cid=" + cid +

", imageId=‘" + imageId + ‘\‘‘ +

", clickCount=" + clickCount +

", updateTime=" + updateTime +

‘}‘;

}

@Override

public int compareTo(ClickInfo o) {

/*从小到大*/

return this.getClickCount().compareTo(o.getClickCount());

/*从大到小*/

//return o.getClickCount().compareTo(this.getClickCount());

}

}

测试:

输出结果:从小到大:[ClickInfo{cid=1, imageId=‘a‘, clickCount=1, updateTime=null}, ClickInfo{cid=1, imageId=‘b‘, clickCount=3, updateTime=null}]

从大到小: [ClickInfo{cid=1, imageId=‘b‘, clickCount=3, updateTime=null}, ClickInfo{cid=1, imageId=‘a‘, clickCount=1, updateTime=null}]

 方式二:使用重载方法:public static <T> void sort(List<T> list, Comparator<? super T> c)

public class ClickInfo{

private Integer cid;

private String imageId;

private Long clickCount;

private Long updateTime;

public Integer getCid() {

return cid;

}

public void setCid(Integer cid) {

this.cid = cid;

}

public String getImageId() {

return imageId;

}

public void setImageId(String imageId) {

this.imageId = imageId;

}

public Long getClickCount() {

return clickCount;

}

public void setClickCount(Long clickCount) {

this.clickCount = clickCount;

}

public Long getUpdateTime() {

return updateTime;

}

public void setUpdateTime(Long updateTime) {

this.updateTime = updateTime;

}

@Override

public String toString() {

return "ClickInfo{" +

"cid=" + cid +

", imageId=‘" + imageId + ‘\‘‘ +

", clickCount=" + clickCount +

", updateTime=" + updateTime +

‘}‘;

}

}

测试:

public static void main(String[] args){

List<ClickInfo> clickInfos = new ArrayList<>();

ClickInfo clickInfo = new ClickInfo();

clickInfo.setCid(1);

clickInfo.setImageId("b");

clickInfo.setClickCount((long) 3);

ClickInfo clickInfo1 = new ClickInfo();

clickInfo1.setCid(1);

clickInfo1.setImageId("a");

clickInfo1.setClickCount((long) 1);

clickInfos.add(clickInfo);

clickInfos.add(clickInfo1);

Collections.sort(clickInfos, new Comparator<ClickInfo>() {

@Override

public int compare(ClickInfo arg0, ClickInfo arg1) {

/*从小到大*/

return arg0.getClickCount().compareTo(arg1.getClickCount());

/*从大到小*/

//return arg1.getClickCount().compareTo(arg0.getClickCount());

            }

});

System.out.println(clickInfos.toString());

}

输出结果:从小到大: [ClickInfo{cid=1, imageId=‘a‘, clickCount=1, updateTime=null}, ClickInfo{cid=1, imageId=‘b‘, clickCount=3, updateTime=null}]

从大到小: [ClickInfo{cid=1, imageId=‘b‘, clickCount=3, updateTime=null}, ClickInfo{cid=1, imageId=‘a‘, clickCount=1, updateTime=null}]

时间: 2024-10-10 10:26:20

List排序Collections.sort()的相关文章

列表排序 --- Collections.sort()

Collections.sort()能够实现对List等Collection集合排序 Collections.sort(List<Type> list) 这种情况要求Type类型自身实现Comparable接口(类中覆盖compareTo方法),如下所示: 或者下面这种情况 Collections.sort(List<Type> list,new SortComparator()); 这种情况下无需再Type上实现comparable接口,需要单独实现Comparator接口(实现

java List 排序 Collections.sort() 对 List 排序

实体类: class User { String name; String age; public User(String name,String age){ this.name=name; this.age=age; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getName() { return name; } public

java List 排序 Collections.sort()

用Collections.sort方法对list排序有两种方法  第一种是list中的对象实现Comparable接口,如下: [java] view plain copy /** * 根据order对User排序 */ public class User implements Comparable<User>{ private String name; private Integer order; public String getName() { return name; } public

List排序Collections.sort 重写compare

1 static List<Integer> intList = Arrays.asList(2,5,7, 3, 1); 2 3 public static void main(String[] args) { 4 5 System.out.println("before sort:"); 6 PrintUtil.showList(intList); 7 System.out.println("========================="); 8

Java中Collections.sort()排序详解

第一种:Comparable 排序接口 若一个类实现了Comparable接口,就意味着"该类支持排序". 假设"有一个List列表(或数组),里面的元素是实现了Comparable接口的类",则该List列表(或数组)可以通过 Collections.sort(或 Arrays.sort)进行排序. 此外,"实现Comparable接口的类的对象"可以用作"有序映射(如TreeMap)"中的键或"有序集合(Tree

Java语言利用Collections.sort对Map,List排序

1.main方法包含TreeMap排序1,TreeMap排序2,HashMap排序,List<Integer>排序,List<Bean>排序,List<Map>排序 package com.tao.test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Li

Collections.sort()用法简单介绍

在开发过程中,在对list集合的排序中遇到了点小阻碍.记录之,与君共勉. 我们先来看看,Collections.sort()的简单用法,代码: package com.wh.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test {

Collections.sort的两种用法 转

Java代码   /** * @author guwh * @version 创建时间:2011-11-3 上午10:49:36 * 类说明 */ package com.jabberchina.test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class SortTest { public stati

Java—集合框架 Collections.sort()、Comparable接口和Comparator接口

Collentions工具类--java.util.Collections Collentions是Java集合框架中,用来操作集合对象的工具类,也是Java集合框架的成员,与List.Map和Set是并列的. Collections.sort() 排序方法,实现对List对象中的元素进行排序. package com.test.collection; import java.util.ArrayList; import java.util.Collections; import java.ut