Java中Collections.sort排序用法

Java中Collections.sort排序用法

第一种是list中的对象实现Comparable接口

/**
 * 根据年龄对User进行排序
 */
public class User implements Comparable<User>{
    private Long id;
    private String name;
    private Integer age;

    public User() {
    }
    public User(Long id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public int compareTo(User arg0) {
    	//排序条件
        return this.getAge().compareTo(arg0.getAge());
    }
}

测试1

public class Test{

    public static void main(String[] args) {
        User yxs1 = new User((long) 1, "yxs1", 21);
        User yxs2 = new User((long) 2, "yxs2", 22);
        User yxs3 = new User((long) 3, "yxs3", 23);
        User yxs4 = new User((long) 4, "yxs4", 24);
        User yxs5 = new User((long) 5, "yxs5", 25);
        User yxs6 = new User((long) 6, "yxs6", 26);
        List<User> list = new ArrayList<User>();
        list.add(yxs1);
        list.add(yxs2);
        list.add(yxs5);
        list.add(yxs3);
        list.add(yxs4);
        list.add(yxs6);
        Collections.sort(list);
        for(User u : list){
            System.out.println(u.getName());
        }
    }
}

第二种方法是根据Collections.sort重载方法来实现

public class User { //此处无需实现Comparable接口
    private Long id;
    private String name;
    private Integer age;

    public User() {
    }
    public User(Long id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

主类中这样写即可:

public class Test{
    public static void main(String[] args) {
		User yxs1 = new User((long) 1, "yxs1", 21);
        User yxs2 = new User((long) 2, "yxs2", 22);
        User yxs3 = new User((long) 3, "yxs3", 23);
        User yxs4 = new User((long) 4, "yxs4", 24);
        User yxs5 = new User((long) 5, "yxs5", 25);
        User yxs6 = new User((long) 6, "yxs6", 26);
        List<User> list = new ArrayList<User>();
        list.add(yxs1);
        list.add(yxs2);
        list.add(yxs5);
        list.add(yxs3);
        list.add(yxs4);
        list.add(yxs6);
       
        Collections.sort(list, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                /**
           * 升序排的话就是第一个参数.compareTo(第二个参数);
           * 降序排的话就是第二个参数.compareTo(第一个参数);
           */
                return o1.getAge().compareTo(o2.getAge());
            }
        });
        for (User user : list) {
			System.out.println(user);
            //System.out.println(user.getName());
        }
        System.out.println(list);
    }
}

附加Map集合遍历方法:

//遍历map集合并排序的方法
public static Object mapSort(Map<String,String> resultMap){
    Set<Map.Entry<String, String>> entries = resultMap.entrySet();
    List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(entries);
    LinkedHashMap<String, Long> result = new LinkedHashMap<String, Long>(list.size());
    long total = 0;
    try {
        Collections.sort(list, new Comparator<Map.Entry<String,String>>(){
            public int compare(Map.Entry<String,String> map1,Map.Entry<String,String> map2){
                //降序排列
                //return Long.valueOf(map2.getValue()).compareTo(Long.valueOf(map1.getValue()));
                //升序排列
                return Long.valueOf(map1.getValue()).compareTo(Long.valueOf(map2.getValue()));
            }
        });
        for(Map.Entry<String,String> entry : list){
            result.put(entry.getKey(), Long.valueOf(entry.getValue()));
            total= total + Long.valueOf(entry.getValue());
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return result;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山雨木公

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值