赞
踩
一、背景
1.软件开发过程中集合排序是比较强大的功能,会使用集合Map、Set、List实现排序功能,知道匿名内部类Comparator很关键,搞清楚集合排序的性能开销,排序遇到的坑以及解决的方法,注意下面的例子都是JDK1.8的用法。
二、LIst集合排序
1.UML类图
2.重点分析下ArrayList的排序,毕竟实战开发用的最频繁的就是它了
三、第一种做法
介绍:这种叫定制排序,或自定义排序,需编写匿名内部类,先new一个Comparator接口的比较器对象c,同时实现compare()其方法;
然后将比较器对象c传给Collections.sort()方法的参数列表中,实现排序功能;一般用这种的比较多。
1.实体类(private int id)
- public class Person{
- private int id;
- private String name;
- private String address;
-
- public Person(int id, String name, String address) {
- this.id = id;
- this.name = name;
- this.address = address;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getAddress() {
- return address;
- }
-
- public void setAddress(String address) {
- this.address = address;
- }
-
- @Override
- public String toString() {
- return "Person{" +
- "id='" + id + '\'' +
- ", name='" + name + '\'' +
- ", address='" + address + '\'' +
- '}';
- }
- }
2.测试类
- public class ArrayListTest {
- public static void main(String[] args) {
- List<Person> list=Lists.newArrayList();
- //产生10以内的随机数
- int num = (int)(Math.random()*1000+1);
- for(int i=num;i>0;i--){
- list.add(new Person(i,"张三","河南"));
- }
- for(Object o : list){
- System.out.println(o);
- }
- System.out.println("++++++++++++++++++++++++++++++++++++");
- Collections.sort(list, new Comparator<Person>() {
- @Override
- public int compare(Person o1, Person o2) {
- if (o1.getId() > o2.getId()) {
- return 1;
- } else if (o1.getId() < o2.getId()) {
- return -1;
- }
- return 0;
- }
-
- });
- for(Object o : list){
- System.out.println(o);
- }
- }
- }
3.结果(升序)
- Person{id='10', name='张三', address='河南'}
- Person{id='9', name='张三', address='河南'}
- Person{id='8', name='张三', address='河南'}
- Person{id='7', name='张三', address='河南'}
- Person{id='6', name='张三', address='河南'}
- Person{id='5', name='张三', address='河南'}
- Person{id='4', name='张三', address='河南'}
- Person{id='3', name='张三', address='河南'}
- Person{id='2', name='张三', address='河南'}
- Person{id='1', name='张三', address='河南'}
- ++++++++++++++++++++++++++++++++++++
- Person{id='1', name='张三', address='河南'}
- Person{id='2', name='张三', address='河南'}
- Person{id='3', name='张三', address='河南'}
- Person{id='4', name='张三', address='河南'}
- Person{id='5', name='张三', address='河南'}
- Person{id='6', name='张三', address='河南'}
- Person{id='7', name='张三', address='河南'}
- Person{id='8', name='张三', address='河南'}
- Person{id='9', name='张三', address='河南'}
- Person{id='10', name='张三', address='河南'}
四、第二中做法
1.实体类(private String id)
- public class Person{
- private String id;
- private String name;
- private String address;
-
- public Person(String id, String name, String address) {
- this.id = id;
- this.name = name;
- this.address = address;
- }
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getAddress() {
- return address;
- }
-
- public void setAddress(String address) {
- this.address = address;
- }
-
- @Override
- public String toString() {
- return "Person{" +
- "id='" + id + '\'' +
- ", name='" + name + '\'' +
- ", address='" + address + '\'' +
- '}';
- }
- }
2.测试类
- public class ArrayListTest {
- public static void main(String[] args) {
- List<Person> list=Lists.newArrayList();
- //产生10以内的随机数
- int num = (int)(Math.random()*100+1);
- for(int i=num;i>0;i--){
- list.add(new Person(""+i,"张三","河南"));
- }
- for(Object o : list){
- System.out.println(o);
- }
- System.out.println("++++++++++++++++++++++++++++++++++++");
- Collections.sort(list, new Comparator<Person>() {
- @Override
- public int compare(Person o1, Person o2) {
- return new Double(o1.getId()).compareTo(new Double(o2.getId()));
- }
-
- });
- for(Object o : list){
- System.out.println(o);
- }
- }
- }
3.结果
- Person{id='10', name='张三', address='河南'}
- Person{id='9', name='张三', address='河南'}
- Person{id='8', name='张三', address='河南'}
- Person{id='7', name='张三', address='河南'}
- Person{id='6', name='张三', address='河南'}
- Person{id='5', name='张三', address='河南'}
- Person{id='4', name='张三', address='河南'}
- Person{id='3', name='张三', address='河南'}
- Person{id='2', name='张三', address='河南'}
- Person{id='1', name='张三', address='河南'}
- ++++++++++++++++++++++++++++++++++++
- Person{id='1', name='张三', address='河南'}
- Person{id='2', name='张三', address='河南'}
- Person{id='3', name='张三', address='河南'}
- Person{id='4', name='张三', address='河南'}
- Person{id='5', name='张三', address='河南'}
- Person{id='6', name='张三', address='河南'}
- Person{id='7', name='张三', address='河南'}
- Person{id='8', name='张三', address='河南'}
- Person{id='9', name='张三', address='河南'}
- Person{id='10', name='张三', address='河南'}
五、第三种做法
另外一种称为自然排序,参与排序的对象需实现comparable接口,重写其compareTo()方法,方法体中实现对象的比较大小规则。
1.实体类
- public class Person implements Comparable{
- private String id;
- private String name;
- private String address;
-
- public Person(String id, String name, String address) {
- this.id = id;
- this.name = name;
- this.address = address;
- }
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getAddress() {
- return address;
- }
-
- public void setAddress(String address) {
- this.address = address;
- }
-
- @Override
- public String toString() {
- return "Person{" +
- "id='" + id + '\'' +
- ", name='" + name + '\'' +
- ", address='" + address + '\'' +
- '}';
- }
-
- @Override
- public int compareTo(Object o) {
- if (o instanceof Person){
- Person o1 = (Person)o;
- return new Double(this.getId()).compareTo(new Double(o1.getId()));
- }
- throw new ClassCastException("不能转换为Person类型的对象...");
- }
- }
2.测试类
- public class ArrayListTest {
- public static void main(String[] args) {
- List<Person> list=Lists.newArrayList();
- //产生10以内的随机数
- int num = (int)(Math.random()*100+1);
- for(int i=num;i>0;i--){
- list.add(new Person(""+i,"张三","河南"));
- }
- for(Object o : list){
- System.out.println(o);
- }
- System.out.println("++++++++++++++++++++++++++++++++++++");
- Collections.sort(list);
- for(Object o : list){
- System.out.println(o);
- }
- }
- }
3.结果
- Person{id='10', name='张三', address='河南'}
- Person{id='9', name='张三', address='河南'}
- Person{id='8', name='张三', address='河南'}
- Person{id='7', name='张三', address='河南'}
- Person{id='6', name='张三', address='河南'}
- Person{id='5', name='张三', address='河南'}
- Person{id='4', name='张三', address='河南'}
- Person{id='3', name='张三', address='河南'}
- Person{id='2', name='张三', address='河南'}
- Person{id='1', name='张三', address='河南'}
- ++++++++++++++++++++++++++++++++++++
- Person{id='1', name='张三', address='河南'}
- Person{id='2', name='张三', address='河南'}
- Person{id='3', name='张三', address='河南'}
- Person{id='4', name='张三', address='河南'}
- Person{id='5', name='张三', address='河南'}
- Person{id='6', name='张三', address='河南'}
- Person{id='7', name='张三', address='河南'}
- Person{id='8', name='张三', address='河南'}
- Person{id='9', name='张三', address='河南'}
- Person{id='10', name='张三', address='河南'}
六、结束
今天就写到这吧,太困了,晚安!各位,希望能帮到你们。
Always keep the faith!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。