对List集合中的元素进行排序
来源:互联网 发布:无法连接steam网络 编辑:程序博客网 时间:2024/05/17 14:27
Collections对List集合中的数据进行排序
有时候需要对集合中的元素按照一定的规则进行排序,这就需要用到
[3, 5, 1, 0]
[0, 1, 3, 5]
55,23
67,23
78,26
34,56
我们会发现sort(List<T>)方法中List中的T必须实现Comparable<T>接口,然后实现
compareTo()方法,该方法的返回值0代表相等,1表示大于,-1表示小于;为什么
在简单例子中没有看到实现Comparable接口呢?是因为Integer类其实自己已经实现
了Comparable接口,Java已经给我们做好了。
Collections提供的第二种排序方法sort(List<T> list, Comparator<? super T> c)
先看例子:
score:89:age22
score:98:age27
score:98:age29
score:99:age29
score:100:age23
从上面的例子我们可以看出Students类没有实现Comparable<T>接口,只是在sort()方法
中多传入一个参数,只不过该参数是一个接口我们需要实现其compare方法。
以上就是是Java中Colelctions工具类为我们提供的两种集合排序方法。
有时候需要对集合中的元素按照一定的规则进行排序,这就需要用到
Java中提供的对集合进行操作的工具类Collections,其中的sort方法
先看一个简单的例子:
- public static void main(String[] args) {
- List<Integer> nums = new ArrayList<Integer>();
- nums.add(3);
- nums.add(5);
- nums.add(1);
- nums.add(0);
- System.out.println(nums);
- Collections.sort(nums);
- System.out.println(nums);
- }
public static void main(String[] args) { List<Integer> nums = new ArrayList<Integer>(); nums.add(3); nums.add(5); nums.add(1); nums.add(0); System.out.println(nums); Collections.sort(nums); System.out.println(nums);}输出结果:
[3, 5, 1, 0]
[0, 1, 3, 5]
稍微复杂的List里面放一个复杂的对象
- package core.java.collection.collections;
- 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;
- }
- @Override
- 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());
- }
- }
package core.java.collection.collections;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; } @Override 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()); }}输出结果:
55,23
67,23
78,26
34,56
我们会发现sort(List<T>)方法中List中的T必须实现Comparable<T>接口,然后实现
compareTo()方法,该方法的返回值0代表相等,1表示大于,-1表示小于;为什么
在简单例子中没有看到实现Comparable接口呢?是因为Integer类其实自己已经实现
了Comparable接口,Java已经给我们做好了。
Collections提供的第二种排序方法sort(List<T> list, Comparator<? super T> c)
先看例子:
- package core.java.collection.collections;
- 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>() {
- @Override
- 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());
- }
- }
package core.java.collection.collections;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>() { @Override 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()); }}输出结果:
score:89:age22
score:98:age27
score:98:age29
score:99:age29
score:100:age23
从上面的例子我们可以看出Students类没有实现Comparable<T>接口,只是在sort()方法
中多传入一个参数,只不过该参数是一个接口我们需要实现其compare方法。
以上就是是Java中Colelctions工具类为我们提供的两种集合排序方法。
阅读全文
0 0
- 对List集合中的元素进行排序
- 对List集合中的元素进行排序
- 对List集合中的元素进行排序
- 对List集合中的元素进行排序
- 对List集合中的元素进行排序
- 对List集合中的元素进行排序
- 对List集合中的元素进行排序
- Collections.sort(nums)--对List集合中的元素进行排序
- 对ArrayList集合中的元素进行排序
- List集合中的元素排序
- Java中的对List中元素进行自定义排序
- 对自定义对象集合List中的数据进行排序
- 对List集合中的对象进行自定义排序
- Collections对List集合中的数据进行排序
- java 对List集合进行排序
- Java中对list集合进行排序
- Java中对List集合内的元素进行顺序、倒序、随机排序的示例代码
- Java中对List集合内的元素进行顺序、倒序、随机排序的示例代码
- C语言基础——循环性结构
- leetcode 216. Combination Sum III
- 13:隐藏文件的两种方法
- Frequency Hopping UVA
- Jquery jsonp跨域与php交互
- 对List集合中的元素进行排序
- memcached--存储命令--add
- 环境变量
- 一个STM8S ADC脚与其它功能复用时的话题
- BFC的理解
- 638. Shopping Offers [LeetCode]
- 在IDEA中使用junit
- Linux Kernel ROP
- 编码