Java8排序

时间:2025-03-10 18:25:10

       在这个页面上我们将提供java 8 Stream sorted()示例。我们可以按照自然排序以及Comparator提供的排序对流进行排序。在java 8中Comparator可以使用lambda表达式进行实例化。我们还可以反转自然排序以及提供的排序Comparator。自然排序使用提供的顺序Comparable,必须由其实例是流元素的类实现。在这个页面上我们将排序ListMapSet使用java 8流sorted()方法。

()方法的语法示例。 

1.1sorted():它使用自然顺序对流的元素进行排序。元素类必须实现Comparable接口。 

按自然升序对集合进行排序

().sorted() .stream().sorted();

自然序降序使用Comparator提供reverseOrder()方法

().sorted(()) .stream().sorted(());

1.2 sorted(Comparator<? super T> comparator):这里我们创建一个Comparator使用lambda表达式的实例。我们可以按升序和降序对流元素进行排序。 

使用Comparator来对列表进行自定义升序。 

().sorted((Student::getAge)) .stream().sorted((Student::getAge));

使用Comparator提供reversed()方法对列表进行自定义降序。 。 

().sorted((Student::getAge).reversed()) .stream().sorted((Student::getAge).reversed());

2.使用List流排序()

package ;

import ;
import ;
import ;
import ;

public class StreamListDemo {
	public static void main(String[] args) {
		List<Student> list = new ArrayList<>();
		(new Student(1, "Mahesh", 12));
		(new Student(2, "Suresh", 15));
		(new Student(3, "Nilesh", 10));

		("---Natural Sorting by Name---");
		List<Student> slist = ().sorted().collect(());
		(e -> ("Id:" + () + ", Name: " + () + ", Age:" + ()));

		("---Natural Sorting by Name in reverse order---");
		slist = ().sorted(()).collect(());
		(e -> ("Id:" + () + ", Name: " + () + ", Age:" + ()));

		("---Sorting using Comparator by Age---");
		slist = ().sorted((Student::getAge)).collect(());
		(e -> ("Id:" + () + ", Name: " + () + ", Age:" + ()));

		("---Sorting using Comparator by Age with reverse order---");
		slist = ().sorted((Student::getAge).reversed()).collect(());
		(e -> ("Id:" + () + ", Name: " + () + ", Age:" + ()));
	}
}

package ;

public class Student implements Comparable<Student> {
	private int id;
	private String name;
	private int age;

	public Student(int id, String name, int age) {
		 = id;
		 = name;
		 = age;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	@Override
	public int compareTo(Student ob) {
		return (());
	}

	@Override
	public boolean equals(final Object obj) {
		if (obj == null) {
			return false;
		}
		final Student std = (Student) obj;
		if (this == std) {
			return true;
		} else {
			return (() && ( == ));
		}
	}

	@Override
	public int hashCode() {
		int hashno = 7;
		hashno = 13 * hashno + (name == null ? 0 : ());
		return hashno;
	}
}

执行结果

---Natural Sorting by Name---
Id:1, Name: Mahesh, Age:12
Id:3, Name: Nilesh, Age:10
Id:2, Name: Suresh, Age:15
---Natural Sorting by Name in reverse order---
Id:2, Name: Suresh, Age:15
Id:3, Name: Nilesh, Age:10
Id:1, Name: Mahesh, Age:12
---Sorting using Comparator by Age---
Id:3, Name: Nilesh, Age:10
Id:1, Name: Mahesh, Age:12
Id:2, Name: Suresh, Age:15
---Sorting using Comparator by Age with reverse order---
Id:2, Name: Suresh, Age:15
Id:1, Name: Mahesh, Age:12
Id:3, Name: Nilesh, Age:10

3.使用set流排序

package ;

import ;
import ;
import ;

public class StreamSetDemo {
	public static void main(String[] args) {
		Set<Student> set = new HashSet<>();
		(new Student(1, "Mahesh", 12));
		(new Student(2, "Suresh", 15));
		(new Student(3, "Nilesh", 10));

		("---Natural Sorting by Name---");
		("---Natural Sorting by Name---");
		().sorted().forEach(e -> ("Id:"
						+ () + ", Name: " + () + ", Age:" + ()));

		("---Natural Sorting by Name in reverse order---");
		().sorted(()).forEach(e -> ("Id:"
						+ () + ", Name: " + () + ", Age:" + ()));

		("---Sorting using Comparator by Age---");
		().sorted((Student::getAge))
						.forEach(e -> ("Id:" + () + ", Name: " + () + ", Age:" + ()));

		("---Sorting using Comparator by Age in reverse order---");
		().sorted((Student::getAge).reversed())
						.forEach(e -> ("Id:" + () + ", Name: " + () + ", Age:" + ()));
	}
}

4.使用Map流排序

package ;

import ;
import ;
import ;

public class StreamMapDemo {
	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		(15, "Mahesh");
		(10, "Suresh");
		(30, "Nilesh");

		("---Sort by Map Value---");
		().stream().sorted((::getValue))
						.forEach(e -> ("Key: "+ () +", Value: "+ ()));

		("---Sort by Map Key---");("---Sort by Map Key---");
		().stream().sorted((::getKey))
						.forEach(e -> ("Key: "+ () +", Value: "+ ()));
	}
}

这是在英文网站看到的示例,觉得还不错就翻译过来了。

原文链接:/java/jdk-8/java-8-stream-sorted-example