[Java] 集合操作--多条件排序 →→→→→进入此内容的聊天室

来自 , 2019-03-23, 写在 Java, 查看 162 次.
URL http://www.code666.cn/view/7bb06076
  1. 1.Main
  2. package s0223多条件排序;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Date;
  6. import java.util.List;
  7.  
  8. public class Main {
  9.  
  10.         public static void main(String[] args) {
  11.        List<NewsItem> news=new ArrayList<NewsItem>();
  12.        news.add(new NewsItem("中国登上钓鱼岛",100,new Date(System.currentTimeMillis() ) ) );
  13.        news.add(new NewsItem("中国GDP总量",40,new Date(System.currentTimeMillis() ) ) );
  14.        news.add(new NewsItem("美国总统与xx会晤",60, new Date(System.currentTimeMillis() ) ) );
  15.        
  16.        System.out.println("排序前:"+news);
  17.        Collections.sort(news);
  18.        //因为news中的元素都是Comparable的实现类的实例,所以默认使用指定的Comparator
  19.        //如果使用comparator的实现类,那么要指定comparator
  20.        System.out.println("排序后:"+news);
  21.  
  22.         }
  23.  
  24. }
  25.  
  26. 2.NewsItem类,在这个类中定义了排序优先级和排序顺序
  27. package s0223多条件排序;
  28. import java.text.SimpleDateFormat;
  29. //新闻按时间降序,点击量升序,标题降序
  30. import java.util.Date;
  31.  
  32. public class NewsItem implements java.lang.Comparable<NewsItem>{
  33.     private String title;
  34.     private int  hit;
  35.     private Date pubTime;
  36.    
  37.  
  38.     public NewsItem(){}
  39.  
  40.  
  41.         public NewsItem(String title, int hit, Date pubTime)
  42.         {
  43.                 super();
  44.                 this.title = title;
  45.                 this.hit = hit;
  46.                 this.pubTime = pubTime;
  47.         }
  48.        
  49.         //新闻按时间降序,点击量升序,标题降序,3个if嵌套,规定了排序的优先级
  50.         public int compareTo(NewsItem o)
  51.         {
  52.                 int result=0;
  53.                 result=-this.pubTime.compareTo(o.pubTime);//加了- 变成降序
  54.                 if(result==0)
  55.                 {  
  56.                         result=this.hit-o.hit;
  57.                         if(result==0)
  58.                         {
  59.                                 result=this.title.compareTo(o.title);
  60.                         }      
  61.                 }      
  62.                 return result;
  63.         }
  64.        
  65.         public String toString()
  66.         {
  67.                 StringBuilder sb=new StringBuilder();
  68.                 sb.append("  标题:").append(this.title);
  69.                 sb.append("  时间:").append(new SimpleDateFormat("yyyy-MM-dd").format(this.pubTime));
  70.                 sb.append("  点击量:").append(this.hit).append("\n");
  71.                 String str=sb.toString();
  72.                 return str;
  73.  
  74.         }
  75.  
  76.  
  77.         public String getTitle() {
  78.                 return title;
  79.         }
  80.  
  81.         public void setTitle(String title) {
  82.                 this.title = title;
  83.         }
  84.  
  85.         public int getHit() {
  86.                 return hit;
  87.         }
  88.  
  89.         public void setHit(int hit) {
  90.                 this.hit = hit;
  91.         }
  92.  
  93.         public Date getPubTime() {
  94.                 return pubTime;
  95.         }
  96.  
  97.         public void setPubTime(Date pubTime) {
  98.                 this.pubTime = pubTime;
  99.         }
  100.        
  101.        
  102.        
  103. }
  104.  

回复 "集合操作--多条件排序"

这儿你可以回复上面这条便签

captcha