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
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class User implements Comparable<User> { private int score; private int age; public User(int score, int age) { super(); this.score = score; this.age = age; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int compareTo(User o) { int i = this.getAge() - o.getAge();//先按照年龄排序 if (i == 0) { return this.score - o.getScore();//如果年龄相等了再用分数进行排序 } return i; } public static void main(String[] args) { List<User> users = new ArrayList<User>(); users.add(new User(78, 26)); users.add(new User(67, 23)); users.add(new User(34, 56)); users.add(new User(55, 23)); Collections.sort(users); for (User user : users) { System.out.println(user.getScore() + "," + user.getAge()); } } }
2 :直接用匿名内部类
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Students { private int age; private int score; public Students(int age, int score) { super(); this.age = age; this.score = score; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public static void main(String[] args) { List<Students> students = new ArrayList<Students>(); students.add(new Students(23, 100)); students.add(new Students(27, 98)); students.add(new Students(29, 99)); students.add(new Students(29, 98)); students.add(new Students(22, 89)); Collections.sort(students, new Comparator<Students>() { public int compare(Students o1, Students o2) { int i = o1.getScore() - o2.getScore(); if (i == 0) { return o1.getAge() - o2.getAge(); } return i; } }); for (Students stu : students) { System.out.println("score:" + stu.getScore() + ":age" + stu.getAge()); } } }

浙公网安备 33010602011771号