java – 使用自定义排序顺序对对象的ArrayList进行排序
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 使用自定义排序顺序对对象的ArrayList进行排序,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2885字,纯文字阅读大概需要5分钟。
内容图文
![java – 使用自定义排序顺序对对象的ArrayList进行排序](/upload/InfoBanner/zyjiaocheng/717/c11c4d47751d4843ae579557f77f1fdf.jpg)
我希望为我的地址簿应用程序实现一个排序功能.
我想对ArrayList< Contact>进行排序contactArray.联系人是一个包含四个字段的类:姓名,家庭电话号码,手机号码和地址.我想对名字排序.
如何编写自定义排序功能来执行此操作?
解决方法:
这是一个关于订购对象的教程:
> The Java Tutorials – Collections – Object Ordering
虽然我会给出一些例子,但我仍然建议你阅读它.
有多种方法可以对ArrayList进行排序.如果你想定义一个自然(默认)排序,那么你需要让联系人实现Comparable.假设你想在名称上默认排序,那么为了简单起见省略了nullchecks:
public class Contact implements Comparable<Contact> {
private String name;
private String phone;
private Address address;
public int compareTo(Contact other) {
return name.compareTo(other.name);
}
// Add/generate getters/setters and other boilerplate.
}
这样你就可以做到
List<Contact> contacts = new ArrayList<Contact>();
// Fill it.
Collections.sort(contacts);
如果要定义外部可控排序(它会覆盖自然排序),则需要创建Comparator:
List<Contact> contacts = new ArrayList<Contact>();
// Fill it.
// Now sort by address instead of name (default).
Collections.sort(contacts, new Comparator<Contact>() {
public int compare(Contact one, Contact other) {
return one.getAddress().compareTo(other.getAddress());
}
});
您甚至可以在Contact本身中定义Comparators,以便您可以重复使用它们,而不是每次都重新创建它们:
public class Contact {
private String name;
private String phone;
private Address address;
// ...
public static Comparator<Contact> COMPARE_BY_PHONE = new Comparator<Contact>() {
public int compare(Contact one, Contact other) {
return one.phone.compareTo(other.phone);
}
};
public static Comparator<Contact> COMPARE_BY_ADDRESS = new Comparator<Contact>() {
public int compare(Contact one, Contact other) {
return one.address.compareTo(other.address);
}
};
}
可以使用如下:
List<Contact> contacts = new ArrayList<Contact>();
// Fill it.
// Sort by address.
Collections.sort(contacts, Contact.COMPARE_BY_ADDRESS);
// Sort later by phone.
Collections.sort(contacts, Contact.COMPARE_BY_PHONE);
最重要的是,您可以考虑使用通用的javabean比较器:
public class BeanComparator implements Comparator<Object> {
private String getter;
public BeanComparator(String field) {
this.getter = "get" + field.substring(0, 1).toUpperCase() + field.substring(1);
}
public int compare(Object o1, Object o2) {
try {
if (o1 != null && o2 != null) {
o1 = o1.getClass().getMethod(getter, new Class[0]).invoke(o1, new Object[0]);
o2 = o2.getClass().getMethod(getter, new Class[0]).invoke(o2, new Object[0]);
}
} catch (Exception e) {
// If this exception occurs, then it is usually a fault of the developer.
throw new RuntimeException("Cannot compare " + o1 + " with " + o2 + " on " + getter, e);
}
return (o1 == null) ? -1 : ((o2 == null) ? 1 : ((Comparable<Object>) o1).compareTo(o2));
}
}
您可以使用如下:
// Sort on "phone" field of the Contact bean.
Collections.sort(contacts, new BeanComparator("phone"));
(正如您在代码中看到的那样,在排序过程中可能已经覆盖了空字段以避免NPE)
内容总结
以上是互联网集市为您收集整理的java – 使用自定义排序顺序对对象的ArrayList进行排序全部内容,希望文章能够帮你解决java – 使用自定义排序顺序对对象的ArrayList进行排序所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。