使用 Java Comparator 实现复杂排序逻辑
本文介绍了如何使用它 Java Comparator 对 Actor 对列表进行排序包括 Actor 有类型(如 Artist、Producer、Mixer 等等)和名称。排序规则是:首先按类型优先排序(Artist 最优先然后是 Producer接着是 Mixer第二按照 Actor 名字的字母顺序排序。本文提供了两种实现方法使用枚举类型定义类型优先级和使用 Map 优先考虑存储类型并分析各自的优缺点。在 Java 在开发过程中通常需要对集合进行排序。Comparator 界面为定义自定义排序规则提供了一种灵活的方法。当排序逻辑复杂时例如需要考虑多个排序条件时需要巧妙地使用 Comparator。本文将介绍如何使用 Comparator 对 Actor 列表需要按照列表进行排序 Actor 类型如 Artist、Producer、Mixer 等)优先级和 Actor 对名称进行排序。方案1使用枚举类型定义类型优先级如果 Actor 类型是固定的可以提前定义然后用枚举类型来表示 Actor 类型和定义其优先级是一种非常清晰和安全的方式。首先定义一个 ActorType 每一枚枚枚举包括每一枚 Actor 类型优先级public enum ActorType { ARTIST(1), PRODUCER(2), MIXER(3); private final int priority; ActorType(int priority) { this.priority priority; } public int getPriority() { return priority; } public static int compare(ActorType t1, ActorType t2) { return Integer.compare(t1.priority, t2.priority); } }在这个枚举中每一个 Actor 所有类型都有相关优先级。compare 比较两种方法 ActorType 对象的优先级。然后创建一个 Actor 包括类别 ActorType 属性public class Actor { private String name; private ActorType actorType; public Actor(String name, ActorType actorType) { this.name name; this.actorType actorType; } public String getName() { return name; } public ActorType getActorType() { return actorType; } }接下来创建一个 Comparator 来比较两个 Actor 对象import java.util.Comparator; public class ActorByActorTypeComparator implements ComparatorActor { Override public int compare(Actor actor1 Actor actor2) { int typeComparison ActorType.compare(actor1.getActorType(), actor2.getActorType()); if (typeComparison ! 0) { return typeComparison; } return actor1.getName().compareTo(actor2.getName()); // 按名字排序 } }这个 Comparator 首先比较两个 Actor 类型优先级。如果类型优先级不同则返回比较结果。如果类型优先级相同则根据 Actor 比较名称。示例代码import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main { public static void main(String[] args) { ListActor actors new ArrayList(); actors.add(new Actor(Bob, ActorType.PRODUCER)); actors.add(new Actor(Alice, ActorType.ARTIST)); actors.add(new Actor(Charlie, ActorType.MIXER)); actors.add(new Actor(David, ActorType.ARTIST)); Collections.sort(actors, new ActorByActorTypeComparator()); for (Actor actor : actors) { System.out.println(actor.getName() - actor.getActorType()); } } }输出结果Alice - ARTIST David - ARTIST Bob - PRODUCER Charlie - MIXER方案二:使用 Map 存储类型优先级如果 Actor 如果字符串或枚举类型不能使用则可以使用 Map 存储每种类型的优先级。import java.util.Comparator; import java.util.HashMap; import java.util.Map; public class ActorByTypeComparator implements ComparatorActor { private final MapString, Integer typePriorityMap new HashMap(); public ActorByTypeComparator(MapString, Integer typePriorityMap) { this.typePriorityMap.putAll(typePriorityMap); } Override public int compare(Actor a1, Actor a2) { int a1Priority this.typePriorityMap.getOrDefault(a1.getType(), Integer.MAX_VALUE); int a2Priority this.typePriorityMap.getOrDefault(a2.getType(), Integer.MAX_VALUE); int priorityComparison Integer.compare(a1Priority, a2Priority); if (priorityComparison ! 0) { return priorityComparison; } return a1.getName().compareTo(a2.getName()); // 按名字排序 } }在这个 Comparator 中typePriorityMap 存储了每个 Actor 类型优先级。getOrDefault 该方法用于获取 Actor 如果不存在类型优先级 Map 中则返回 Integer.MAX_VALUE表示优先级最低。同样在优先级相同的情况下按照Actor名称进行排序。示例代码import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { public static void main(String[] args) { ListActor actors new ArrayList(); actors.add(new Actor(Bob, Producer)); actors.add(new Actor(Alice, Artist)); actors.add(new Actor(Charlie, Mixer)); actors.add(new Actor(David, Artist)); MapString, Integer priorityMap new HashMap(); priorityMap.put(Artist, 1); priorityMap.put(Producer, 2); priorityMap.put(Mixer, 3); Collections.sort(actors, new ActorByTypeComparator(priorityMap)); for (Actor actor : actors) { System.out.println(actor.getName() - actor.getType()); } } static class Actor { private String name; private String type; public Actor(String name, String type) { this.name name; this.type type; } public String getName() { return name; } public String getType() { return type; } } }输出结果Alice - ARTIST David - ARTIST Bob - PRODUCER Charlie - MIXER总结本文介绍了两种用途 Comparator 实现复杂排序逻辑的方法。使用枚举类型 适用于 Actor 类型固定代码清晰类型安全。使用 Map 适用于 Actor 如果类型不固定或不能使用枚举类型则更灵活。在实际开发中应根据具体情况选择合适的方案。使用 Comparator 排序规则可以灵活定义使代码更容易理解。需要注意的是在比较多种条件时需要按照优先顺序进行比较。如果优先级高的条件比较结果相同则继续比较优先级低的条件。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2450324.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!