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

数组合并:直接使用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);
        }
    }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,120评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,151评论 6 342
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,377评论 18 399
  • 清明,今天正清明。翻了翻公众号,大多是些诗一般的语句: “时间真的会带走很多, 遇到一些人, 告别一些人, 清明,...
    让猫吃辣椒阅读 1,630评论 0 0
  • 午后,心血来潮,骑着小电驴去寻找一份静谧。 看到雨后有些狼狈的座椅,日子好像又回到了从前。 长长的跑道曾经跌落了谁...
    大天使早就死掉了阅读 4,304评论 33 23

友情链接更多精彩内容