Java 1.8 list 排序

package com.bugyun.po;


public class Student {

    private String name;
    private Integer age;
    private Float score ;
    
    
	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;
	}
	public Float getScore() {
		return score;
	}
	public void setScore(Float score) {
		this.score = score;
	}
	
	
	public Student(String name, Integer age, Float score) {
		super();
		this.name = name;
		this.age = age;
		this.score = score;
	}
	
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
	}
      
}

 

 

ListSort.java

package com.bugyun.demo;

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

import com.bugyun.po.Student;
import com.google.common.collect.Lists;

/**
 * 
 * list 排序
 * 1. 通过 Lambda 排序
 * 2. 通过 初始化 方法
 * 
 * @author Administrator
 */
public class ListSort {

	public static void main(String[] args) throws Exception {
		ListSort listSort = new ListSort();
		
		Student one = new Student("liKui", 22, 65.0f);
		Student two = new Student("songJiang", 55, 71.0f);
		Student three = new Student("wuSong", 45, 80.0f);
		Student four = new Student("sunErNiang", 80, 98f);
		Student five = new Student("liGui", 22, 19.0f);

		List<Student> humanList = new ArrayList<>();
		humanList.add(one);
		humanList.add(two);
		humanList.add(three);
		humanList.add(four);
		humanList.add(five);
		
		
		ArrayList<Student> humanListTwo = Lists.newArrayList(one, two, three, four, five);
		
		listSort.sortByAgeWithLambda(humanList);
		listSort.sortByAgeWithInstanceMethod(humanList);
		listSort.sortByAgeScoreWithComparator(humanList);
		
//		listSort.sortByAgeWithLambda(humanListTwo);
//		listSort.sortByAgeWithInstanceMethod(humanListTwo);
//		listSort.sortByAgeScoreWithComparator(humanListTwo);
	}
	
	
	/**
	 * 通过 Lambda 对学生年龄进行排序
	 * @param humanList
	 * @throws Exception
	 */
	public void sortByAgeWithLambda(List<Student> humanList) throws Exception {

		System.out.println("\n----------美丽的分割线----------");
		
		humanList.sort((Student h1, Student h2) -> h1.getAge().compareTo(h2.getAge()));
		
		printList(humanList);

	}

	/**
	 * 通过 默认方法  对学生年龄进行排序
	 * @param humanList
	 * @throws Exception
	 */
	public void sortByAgeWithInstanceMethod(List<Student> humanList) throws Exception {

		System.out.println("\n----------美丽的分割线----------");
		
		Collections.sort(humanList, Comparator.comparing(Student::getAge));
		printList(humanList);
	}
	

	public void sortByAgeScoreWithComparator(List<Student> humanList) throws Exception {
		System.out.println("\n----------美丽的分割线----------");
		
		Comparator<Student> comparator = (h1, h2) -> {
			if (h1.getAge().equals(h2.getAge())) {
				return Float.compare(h1.getScore(), h2.getScore());
			}
			return h1.getAge().compareTo(h2.getAge());
		};
		
		humanList.sort(comparator);
		printList(humanList);
	}

	
	private void printList(List<Student> humanList) {
		for (Student human : humanList) {
			System.out.println(human);
		}
		/**
		 *  对集合洗牌,打乱顺序
		 */
		Collections.shuffle(humanList);
	}
	
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 Java 1.8 JDK 中 List 分组排序的示例代码: ```java import java.util.*; import java.util.stream.Collectors; public class ListGroupingAndSortingExample { public static void main(String[] args) { // Sample data List<Employee> employees = Arrays.asList( new Employee("John", "Doe", 1000), new Employee("Jane", "Doe", 2000), new Employee("John", "Smith", 1500), new Employee("Jane", "Smith", 2500), new Employee("Bob", "Johnson", 3000), new Employee("Alice", "Johnson", 4000) ); // Grouping by last name and sorting by salary Map<String, List<Employee>> groupedAndSorted = employees.stream() .sorted(Comparator.comparingInt(Employee::getSalary)) .collect(Collectors.groupingBy(Employee::getLastName)); // Printing results groupedAndSorted.forEach((lastName, employeeList) -> { System.out.println("Employees with last name " + lastName + ":"); employeeList.forEach(System.out::println); }); } } class Employee { private String firstName; private String lastName; private int salary; public Employee(String firstName, String lastName, int salary) { this.firstName = firstName; this.lastName = lastName; this.salary = salary; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getSalary() { return salary; } @Override public String toString() { return firstName + " " + lastName + " (salary: " + salary + ")"; } } ``` 这个示例代码将员工列表按照姓氏进行分组,并且按照薪水进行排序。最后,它将每个分组中的员工列表打印出来。你可以根据自己的需求修改这个示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值