List排序

一、 根据list中的对象中的某个int类型属性进行排序

创建一个对象实体类

package com.sm;

public class User {
	private String name;
	private Integer age;
	public User() {
		super();
	}
	public User(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "TestA [name=" + name + ", age=" + age + "]";
	}
}

编写逻辑代码

方式一

package com.list.sort;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public class ListSortTest {

	public static void main(String[] args) {
		List<User> users = new ArrayList<User>();
		User user01 = new User("张三", 18);
		User user02 = new User("李四", 12);
		User user03 = new User("王五", 26);
		User user04 = new User("赵六", 50);
		User user05 = new User("钱七", 37);
		users.add(user01);
		users.add(user02);
		users.add(user03);
		users.add(user04);
		users.add(user05);
		
		//java8遍历
		users.forEach(p -> System.out.println(p.getAge()));
		
		System.out.println("===排序前如下===");
		users.stream().forEach(item -> System.out.println(item));

        //根据年龄升序排序方式一
		users.sort((a, b) -> a.getAge().compareTo(b.getAge()));
		System.out.println("===根据年龄升序排序后如下方式一===");
        users.stream().forEach(item -> System.out.println(item));
        
		//根据年龄倒序排序方式一
		users.sort((a, b) -> b.getAge().compareTo(a.getAge()));
		System.out.println("===根据年龄降序排序后如下方式一===");
        users.stream().forEach(item -> System.out.println(item));
        

        //根据年龄升序排序(方式二简写)
		users.sort(Comparator.comparing(User::getAge));
		System.out.println("===根据年龄升序排序后如下方式二===");
        users.stream().forEach(item -> System.out.println(item));

        //根据年龄降序排序(方式二简写)
		users.sort(Comparator.comparing(User::getAge).reversed());
        System.out.println("===根据年龄降序排序后如下方式二===");
        users.stream().forEach(item -> System.out.println(item));
		
		
		//根据其中的某个对象的某个属性的值删除某个对象
		System.out.print("删除前:");
		users.stream().forEach(item -> System.out.print(item));

        Iterator<User> it = users.iterator();
        while (it.hasNext()) {
        	User s = it.next();
            if (s.getName().equals("李四")) {
                it.remove();
            }
        }

        System.out.print("\n删除后:");
        users.stream().forEach(item -> System.out.print(item));
	}
}

方式二

package com.sm;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Demo {
	public static void main(String[] args) throws ParseException {
		//创建多个对象
		User user01 = new User("张三", 3);
		User user02 = new User("李四", 1);
		User user03 = new User("王五", 2);
		User user04 = new User("赵六", 4);
		User user05 = new User("钱七", 0);
		User user06 = new User("孙八", 9);
        //创建一个集合。并把多个对象存入到集合中。
		List<User> list  = new ArrayList<>();
		list.add(user01);
		list.add(user02);
		list.add(user03);
		list.add(user04);
		list.add(user05);
		list.add(user06);
        
		System.out.println("排序前--:"+list.toString());
        
		Collections.sort(list, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                //升序
                return o1.getAge().compareTo(o2.getAge());
            }
        });
		System.out.println("升序排序后--:"+list.toString());
		
		Collections.sort(list, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                //升序
                return o2.getAge().compareTo(o1.getAge());
            }
        });
		System.out.println("降序排序后--:"+list.toString());
        
    }

你可能感兴趣的:(List排序)