在Java集合中,对元素进行排序可以使用多种方式和工具,以下是详细介绍:
1. 对List
集合排序
1.1 使用Collections.sort()
方法
如果元素类实现了Comparable
接口,Collections.sort()
方法能直接对List
进行排序。Comparable
接口里的compareTo()
方法定义了元素的自然排序规则。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Student implements Comparable<Student> {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
@Override
public int compareTo(Student other) {
return Integer.compare(this.age, other.age);
}
@Override
public String toString() {
return "Student{name='" + name + "', age=" + age + "}";
}
}
public class SortListWithComparable {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("Alice", 20));
students.add(new Student("Bob", 18));
students.add(new Student("Charlie", 22));
Collections.sort(students);
for (Student student : students) {
System.out.println(student);
}
}
}
上述代码中,Student
类实现了Comparable
接口,按照年龄对学生进行排序。
1.2 使用List.sort()
方法
从Java 8开始,List
接口添加了sort()
方法,它接收一个Comparator
对象,这能让你自定义排序规则。
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
}
public class SortListWithComparator {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 20));
people.add(new Person("Bob", 18));
people.add(new Person("Charlie", 22));
people.sort(Comparator.comparing(Person::getAge));
for (Person person : people) {
System.out.println(person);
}
}
}
此代码里,List.sort()
方法借助Comparator.comparing()
方法依据年龄对Person
对象进行排序。
2. 对数组排序
2.1 使用Arrays.sort()
方法
Arrays.sort()
方法能对基本数据类型数组和对象数组进行排序。对于对象数组,元素类要实现Comparable
接口或者传入一个Comparator
对象。
import java.util.Arrays;
class Book implements Comparable<Book> {
private String title;
private int price;
public Book(String title, int price) {
this.title = title;
this.price = price;
}
public int getPrice() {
return price;
}
public String getTitle() {
return title;
}
@Override
public int compareTo(Book other) {
return Integer.compare(this.price, other.price);
}
@Override
public String toString() {
return "Book{title='" + title + "', price=" + price + "}";
}
}
public class SortArray {
public static void main(String[] args) {
Book[] books = new Book[3];
books[0] = new Book("Java Programming", 50);
books[1] = new Book("Python Basics", 30);
books[2] = new Book("Data Structures", 70);
Arrays.sort(books);
for (Book book : books) {
System.out.println(book);
}
}
}
上述代码中,Arrays.sort()
方法对Book
对象数组按价格进行排序。
3. 对TreeSet
和TreeMap
排序
3.1 TreeSet
TreeSet
会依据元素的自然顺序或者指定的Comparator
对元素进行排序。
import java.util.TreeSet;
class Fruit implements Comparable<Fruit> {
private String name;
private int quantity;
public Fruit(String name, int quantity) {
this.name = name;
this.quantity = quantity;
}
public int getQuantity() {
return quantity;
}
public String getName() {
return name;
}
@Override
public int compareTo(Fruit other) {
return Integer.compare(this.quantity, other.quantity);
}
@Override
public String toString() {
return "Fruit{name='" + name + "', quantity=" + quantity + "}";
}
}
public class SortTreeSet {
public static void main(String[] args) {
TreeSet<Fruit> fruits = new TreeSet<>();
fruits.add(new Fruit("Apple", 5));
fruits.add(new Fruit("Banana", 3));
fruits.add(new Fruit("Cherry", 7));
for (Fruit fruit : fruits) {
System.out.println(fruit);
}
}
}
这里,TreeSet
按水果的数量对Fruit
对象进行排序。
3.2 TreeMap
TreeMap
会根据键的自然顺序或者指定的Comparator
对键进行排序。
import java.util.TreeMap;
class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public double getPrice() {
return price;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Product{name='" + name + "', price=" + price + "}";
}
}
public class SortTreeMap {
public static void main(String[] args) {
TreeMap<Integer, Product> products = new TreeMap<>();
products.put(3, new Product("Laptop", 1000));
products.put(1, new Product("Mouse", 20));
products.put(2, new Product("Keyboard", 50));
for (Integer key : products.keySet()) {
System.out.println("Key: " + key + ", Value: " + products.get(key));
}
}
}
此代码中,TreeMap
按键的自然顺序对Product
对象进行排序。
综上所述,Java中可根据不同的集合类型和需求,使用Collections.sort()
、List.sort()
、Arrays.sort()
、TreeSet
和TreeMap
等工具对元素进行排序。