Collections

Java使用Collections.reverse()反转一个List

public class Demo { 

public static void main(String[] args) { 
ArrayList<Integer> lists = new ArrayList<Integer>();
 // 初始化10个测试数据 
for (int i = 0; i < 10; i++) { 
lists.add(i); 
} 
// 打印测试数据
 for (Integer n : lists) {
 System.out.print(n + " ");
 } 
// 反转lists 
Collections.reverse(lists); 
// 换行打印 
System.out.println();
 // 打印测试数据 for (Integer n : lists) { 
System.out.print(n + " "); 
} 
} 
}
0 1 2 3 4 5 6 7 8 9 
9 8 7 6 5 4 3 2 1 0

---------------------
作者:张雨明
来源:CSDN
原文:https://blog.csdn.net/yu540135101/article/details/85258494
版权声明:本文为博主原创文章,转载请附上博文链接!

==================================================================================

import java.util.ArrayList;
import java.util.Collections;
 
 
public class TTEST {
    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<String>();
        for(int i=0;i<10;++i){
            arr.add("str"+i);
        }
        for (String string : arr) {
            System.out.print(string+" ");
        }
        System.out.println();
        Collections.reverse(arr);
        for (String string : arr) {
            System.out.print(string+" ");
        }
    }
}


---------------------
作者:呼啸
来源:CSDN
原文:https://blog.csdn.net/howlaa/article/details/34809737
版权声明:本文为博主原创文章,转载请附上博文链接!

==================================================================================

Collections实现List集合排序

//将集合按照时间倒叙排序
Collections.sort(vo, new Comparator<OaDocmanage>() {


@Override
public int compare(OaDocmanage o1, OaDocmanage o2) {
return o2.getDate().compareTo(o1.getDate());
}

});


vo 是要排序的list集合;OaDocmanage是集合中存的对象; getDate是OaDocmanage的date属性的get方法


o1.getDate().compareTo(o2.getDate());    顺序排序


o2.getDate().compareTo(o1.getDate());    倒叙排序
---------------------
作者:鹏鹏博客
来源:CSDN
原文:https://blog.csdn.net/u013782879/article/details/80852818
版权声明:本文为博主原创文章,转载请附上博文链接!

====================================================================================

1:实现comparable

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.List;
  4.  
  5. public class User implements Comparable<User> {
  6.  
  7. private int score;
  8.  
  9. private int age;
  10.  
  11. public User(int score, int age) {
  12. super();
  13. this.score = score;
  14. this.age = age;
  15. }
  16.  
  17. public int getScore() {
  18. return score;
  19. }
  20.  
  21. public void setScore(int score) {
  22. this.score = score;
  23. }
  24.  
  25. public int getAge() {
  26. return age;
  27. }
  28.  
  29. public void setAge(int age) {
  30. this.age = age;
  31. }
  32.  
  33. @Override
  34. public int compareTo(User o) {
  35. int i = this.getAge() - o.getAge();//先按照年龄排序
  36. if (i == 0) {
  37. return this.score - o.getScore();//如果年龄相等了再用分数进行排序
  38. }
  39. return i;
  40. }
  41.  
  42. public static void main(String[] args) {
  43. List<User> users = new ArrayList<User>();
  44. users.add(new User(78, 26));
  45. users.add(new User(67, 23));
  46. users.add(new User(34, 56));
  47. users.add(new User(55, 23));
  48. Collections.sort(users);
  49. for (User user : users) {
  50. System.out.println(user.getScore() + "," + user.getAge());
  51. }
  52. }
  53. }

  2 :直接用匿名内部类

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.List;
  5.  
  6. public class Students {
  7.  
  8. private int age;
  9. private int score;
  10.  
  11. public Students(int age, int score) {
  12. super();
  13. this.age = age;
  14. this.score = score;
  15. }
  16.  
  17. public int getAge() {
  18. return age;
  19. }
  20.  
  21. public void setAge(int age) {
  22. this.age = age;
  23. }
  24.  
  25. public int getScore() {
  26. return score;
  27. }
  28.  
  29. public void setScore(int score) {
  30. this.score = score;
  31. }
  32.  
  33. public static void main(String[] args) {
  34. List<Students> students = new ArrayList<Students>();
  35. students.add(new Students(23, 100));
  36. students.add(new Students(27, 98));
  37. students.add(new Students(29, 99));
  38. students.add(new Students(29, 98));
  39. students.add(new Students(22, 89));
  40. Collections.sort(students, new Comparator<Students>() {
  41.  
  42. @Override
  43. public int compare(Students o1, Students o2) {
  44. int i = o1.getScore() - o2.getScore();
  45. if (i == 0) {
  46. return o1.getAge() - o2.getAge();
  47. }
  48. return i;
  49. }
  50. });
  51. for (Students stu : students) {
  52. System.out.println("score:" + stu.getScore() + ":age" + stu.getAge());
  53. }
  54. }
  55. }
posted @ 2019-07-16 09:34  xiaoshen666  阅读(126)  评论(0)    收藏  举报