美文网首页一天一点学Java
数组合并排序与List(Collection)合并排序

数组合并排序与List(Collection)合并排序

作者: Sandy_678f | 来源:发表于2018-05-19 10:19 被阅读0次

    数组合并:直接使用common-lang中的jar包
    pom.xml配置

    <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-lang3</artifactId>
          <version>3.7</version>
    </dependency>
    

    基本数据类型的排序不必重写compareTo方法

    package ArrayDemo;
    
    import org.apache.commons.lang3.ArrayUtils;
    
    import java.util.Arrays;
    
    public class ArrayMerge {
    
        public static void main(String[] args) {
    
            int i_first[] = {1,3,3,7};
            int i_second[] = {2,7,9};
    
            int contact[] = ArrayUtils.addAll(i_first, i_second);
            Arrays.sort(contact);
    
            for (int i:contact) {
                System.out.println(i + " ");
            }
    
        }
    }
    
    
    package ListDemo;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    public class ListMerge {
    
        public static void main(String[] args) {
            List<Person> list_first = new ArrayList<>();
            List<Person> list_second = new ArrayList<>();
            List<Person> list_final = new ArrayList<>();
    
            Person p_one = new Person("Sandy",17);
            Person p_two = new Person("Monica",20);
            Person p_three = new Person("Coco", 16);
            Person p_four = new Person("Ivy", 18);
    
            list_first.add(p_one);
            list_first.add(p_two);
    
            list_second.add(p_three);
            list_second.add(p_four);
    
            list_final.addAll(list_first);
            list_final.addAll(list_second);
    
            /*
             * Object 排序一
             */
            Object[] obj = list_final.toArray();
            Arrays.sort(obj);
            for (Object o:obj) {
                System.out.println(((Person)o).getAge() +" ");
            }
    
            /*
             * Object 排序二
             * 调用原理:Collections类 ——> List的sort方法 ——> List的sort方法中调用toArray()——>调用数组的sort方法Array.sort()
             * ——> 调用compareTo()方法
             */
    //        Collections.sort(list_final);
    //        for (Person person : list_final) {
    //            System.out.println( person.getAge() +" ");
    //        }
    
        }
    
        public static class Person implements Comparable<Person>
        {
            private static final long serialVersionUID = 8656128222714547171L;
            transient private String name;
            private int age;
    
            Person(String name,int age){
                this.name= name;
                this.age=age;
            }
    
            public int getAge() {
                return age;
            }
    
            public String getName() {
                return name;
            }
    
            public void setAge(int age) {
                this.age = age;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            @Override
            public int compareTo(Person o) {
                return (this.age < o.getAge()) ? -1 : ((this.age == o.getAge()) ? 0 : 1);
            }
        }
    
    }
    
    

    相关文章

      网友评论

        本文标题:数组合并排序与List(Collection)合并排序

        本文链接:https://www.haomeiwen.com/subject/uisodftx.html