您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在Java中,Comparator是一个接口,用于定义自定义的排序规则。它常常与Collections.sort()方法一起使用,以改变集合元素的顺序。以下是一些常见的Comparator应用案例:
import java.util.Arrays;
import java.util.Comparator;
public class ComparatorExample {
public static void main(String[] args) {
String[] names = {"Alice", "Bob", "Charlie", "David"};
// 按字母顺序排序
Arrays.sort(names, Comparator.naturalOrder());
System.out.println("按字母顺序排序: " + Arrays.toString(names));
// 按长度排序
Arrays.sort(names, Comparator.comparingInt(String::length));
System.out.println("按长度排序: " + Arrays.toString(names));
}
}
import java.util.Arrays;
import java.util.Comparator;
public class ComparatorExample {
public static void main(String[] args) {
Integer[] numbers = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
// 按升序排序
Arrays.sort(numbers, Comparator.naturalOrder());
System.out.println("按升序排序: " + Arrays.toString(numbers));
// 按降序排序
Arrays.sort(numbers, Comparator.reverseOrder());
System.out.println("按降序排序: " + Arrays.toString(numbers));
}
}
import java.util.ArrayList;
import java.util.Collections;
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 String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class ComparatorExample {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 30));
people.add(new Person("Bob", 25));
people.add(new Person("Charlie", 35));
// 按年龄升序排序
Collections.sort(people, Comparator.comparingInt(Person::getAge));
System.out.println("按年龄升序排序: " + people);
// 按姓名升序排序
Collections.sort(people, Comparator.comparing(Person::getName));
System.out.println("按姓名升序排序: " + people);
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Person {
private String name;
private int age;
private String city;
public Person(String name, int age, String city) {
this.name = name;
this.age = age;
this.city = city;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getCity() {
return city;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", city='" + city + '\'' +
'}';
}
}
public class ComparatorExample {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 30, "New York"));
people.add(new Person("Bob", 25, "Los Angeles"));
people.add(new Person("Charlie", 35, "New York"));
// 先按城市排序,再按年龄排序
Collections.sort(people, Comparator.comparing(Person::getCity).thenComparing(Person::getAge));
System.out.println("先按城市排序,再按年龄排序: " + people);
}
}
这些示例展示了Comparator在Java中的多种应用方式,包括字符串排序、整数排序、自定义对象排序和多级排序。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。