
本文旨在帮助开发者掌握如何根据用户输入的多个排序条件对数据进行排序。我们将通过解析用户输入的字符串,提取排序优先级,并利用 Java 的 Comparator 接口实现多重排序。文章将提供清晰的代码示例和详细的步骤说明,帮助读者理解并应用该技术,提升数据处理的灵活性和用户体验。
在许多应用场景中,我们需要根据多个条件对数据进行排序,例如先按姓名排序,再按身高排序。Java 提供了强大的 Comparator 接口,结合集合类的 sort() 方法,可以轻松实现多重排序。
以下是实现多重排序的关键步骤:
假设我们有一个 Superhero 类,包含 name (String), height (int), power (String), weakness (String), 和 originFromEarth (boolean) 属性。 以下代码演示了如何根据用户输入的排序条件对 Superhero 列表进行排序:
立即学习“Java免费学习笔记(深入)”;
import java.util.*;
class Superhero {
String name;
int height;
String power;
String weakness;
boolean originFromEarth;
public Superhero(String name, int height, String power, String weakness, boolean originFromEarth) {
this.name = name;
this.height = height;
this.power = power;
this.weakness = weakness;
this.originFromEarth = originFromEarth;
}
public String getName() {
return name;
}
public int getHeight() {
return height;
}
public String getPower() {
return power;
}
public String getWeakness() {
return weakness;
}
public boolean isOriginFromEarth() {
return originFromEarth;
}
@Override
public String toString() {
return "Superhero{" +
"name='" + name + '\'' +
", height=" + height +
", power='" + power + '\'' +
", weakness='" + weakness + '\'' +
", originFromEarth=" + originFromEarth +
'}';
}
}
public class MultiSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Superhero> superheroes = new ArrayList<>();
superheroes.add(new Superhero("Superman", 190, "Flight", "Kryptonite", true));
superheroes.add(new Superhero("Batman", 180, "Intelligence", "No powers", true));
superheroes.add(new Superhero("Wonder Woman", 183, "Strength", "Piercing weapons", true));
superheroes.add(new Superhero("Martian Manhunter", 201, "Telepathy", "Fire", false));
System.out.println("""
1. Sort by name.
2. Sort by height.
3. Sort by power.
4. Sort by weakness.
5. Sort by origin FROM earth.
6. Sort by origin NOT from earth.
""");
System.out.println("Enter the sorting criteria (e.g., 1 2 for name then height):");
String userInput = sc.nextLine().toLowerCase();
String[] userInputs = userInput.split(" ");
Comparator<Superhero> comparator = null;
for (String input : userInputs) {
int choice = Integer.parseInt(input);
Comparator<Superhero> currentComparator = null;
switch (choice) {
case 1:
currentComparator = Comparator.comparing(Superhero::getName);
break;
case 2:
currentComparator = Comparator.comparingInt(Superhero::getHeight);
break;
case 3:
currentComparator = Comparator.comparing(Superhero::getPower);
break;
case 4:
currentComparator = Comparator.comparing(Superhero::getWeakness);
break;
case 5:
currentComparator = Comparator.comparing(Superhero::isOriginFromEarth, (a, b) -> Boolean.compare(b, a)); //true first
break;
case 6:
currentComparator = Comparator.comparing(Superhero::isOriginFromEarth); //false first
break;
default:
System.out.println("Invalid choice: " + choice);
break;
}
if (currentComparator != null) {
comparator = (comparator == null) ? currentComparator : comparator.thenComparing(currentComparator);
}
}
if (comparator != null) {
superheroes.sort(comparator);
System.out.println("Sorted Superheroes:");
for (Superhero superhero : superheroes) {
System.out.println(superhero);
}
} else {
System.out.println("No valid sorting criteria provided.");
}
}
}代码解释:
运行示例:
如果用户输入 "1 2",程序将首先按姓名排序,然后按身高排序。
通过本文的介绍,您应该已经掌握了如何使用 Java 的 Comparator 接口实现多重排序。这种技术可以灵活地根据用户需求对数据进行排序,提高应用程序的可用性和用户体验。在实际应用中,请根据具体场景进行适当的调整和优化,以满足性能和稳定性的要求。
以上就是使用多重排序条件优化数据排序:Java 实现指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号