• java中对List中对象排序实现


    package com.test;  
      
    import java.util.ArrayList;  
    import java.util.Collections;  
    import java.util.Comparator;  
    import java.util.List;  
      
      
      
    public class NewsManager {  
      
        /** 
         * @param args 
         */  
        public static void main(String[] args) {  
      
            List newss=getNewsList();  
              
            for(int i=0;i<newss.size();i++)  
            {  
                News news=(News)newss.get(i);  
                  
                System.out.println("id:"+news.getId());  
                System.out.println("title:"+news.getTitle());  
                System.out.println("hits:"+news.getHits());  
                  
            }  
      
        }  
          
          
        public static List getNewsList()  
        {  
              
            List list=new ArrayList();  
              
            News news1=new News();  
            news1.setHits(1);  
            news1.setId(1);  
            news1.setTitle("test1");  
            list.add(news1);  
              
            News news2=new News();  
            news2.setHits(7);  
            news2.setId(2);  
            news2.setTitle("test2");  
            list.add(news2);  
              
            News news3=new News();  
            news3.setHits(3);  
            news3.setId(3);  
            news3.setTitle("test3");  
            list.add(news3);  
              
            News news4=new News();  
            news4.setHits(5);  
            news4.setId(4);  
            news4.setTitle("test4");  
            list.add(news4);  
              
              
            // 按点击数倒序  
            Collections.sort(list, new Comparator<News>() {  
                public int compare(News arg0, News arg1) {  
                    int hits0 = arg0.getHits();  
                    int hits1 = arg1.getHits();  
                    if (hits1 > hits0) {  
                        return 1;  
                    } else if (hits1 == hits0) {  
                        return 0;  
                    } else {  
                        return -1;  
                    }  
                }  
            });  
              
            return list;  
        }  
      
    }  

    原博地址:https://blog.csdn.net/5iasp/article/details/8990531

  • 相关阅读:
    ubuntu多版本java并切换环境
    ssh 免密登陆
    linux系统的文件和目录
    docker-compose简介及使用
    vue3.0 + html2canvas 一键截图
    vue3.0 无法获取对象值
    算法——回溯算法 (Back Tracking) (转)
    VMware——CentOS-64 7——桥接模式无法连接网络的问题
    groovy 字符串和闭包
    https://blog.didispace.com/
  • 原文地址:https://www.cnblogs.com/coder-lzh/p/9197167.html
Copyright © 2020-2023  润新知