【面试题】Java集合类有哪些? 排序方法(附案例源码)

Java中常用的排序工具类是Arrays.sort()和Collections.sort(),它们底层分别使用的是基于快速排序归并排序的排序算法。

Java集合类包括两个根接口:Collection和Map。

一、Collection

List 接口

继承了 Collection 接口,定义一个允许重复项的有序集合;按照对象进入顺序进行保存对象,List集合就像是一个数组,有序,长度可变。基于索引的队成员随机访问。

特点: 有序的,可重复的

常用的实体类: ArrayList,LinkedList。

ArrayList:常用来查找

LinkedList:常用来增加和删除操作

Set 接口

继承了 Collection 接口,集合元素无序且无重复。成员不能重复。

特点: 无序的,唯一的

常用的实体类:HashSet、TreeSet,LinkedHashSet。

HashSet 无序的,唯一的

TreeSet 有序的,唯一的,底层是自平衡二叉树结构(红黑树);附加实现了SortedSet。成员一般为同一类型。

LinkedHashSet 按照成员的插入顺序遍历成员

Collection主要实现类特点
实现类名称主要特点
ArrayList线性表,底层用Object数组实现,特点:访问快,增删慢
LinkedList链表,底层用双向链表结构实现的,特点:访问慢,增删快
HashSet无序,唯一,基于HashMap实现的,至多有一个null元素
TreeSet底层是红黑树(自平衡的排序二叉树),有序,唯一
LinkedHashSet底层哈希表+链表实现,元素不重复,与HashSet相比访问更快,增删稍慢

二、Map:

Map接口含有两个部分(两列):关键字和值 (key-value,简称键值对);key不可重复,value可重复;添加数据时,如果key重复,则用新值替换原有的值。

常用实现类:HashMap、TreeMap

键值对成员,基于键找值的操作。

集合排序方法

使用Collections类的sort(List list)方法

sort(List list)是根据元素的自然顺序对指定列表按升序进行排序。

1、对基本数据类型排序

List中只能存放对象,要想存放基本数据类型的话,泛型中只能写其对应的包装类。

2、对字符串排序

集合中字符串的排序后其实是按字符顺序,ASCII值顺序进行排序的。

三、自定义排序

使用Comparable或Comparator接口

下面是Comparator和Comparable接口的区别:

1、Comparator:

①位于java.util包

②在要比较的类的外部实现该接口

③调用sort方法时,要指定Comparator的实现类

使用顺序:

实现要排序的接口

实现comparator接口

测试

【下面是完整的案例代码】:

//定义实体Cat
public class Cat {

    private Integer id;

    private String name;

    private Integer month;


    public Cat(){}
    public Cat(Integer id, String name, Integer month) {
        this.id = id;
        this.name = name;
        this.month = month;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getMonth() {
        return month;
    }

    public void setMonth(Integer month) {
        this.month = month;
    }

    @Override
    public String toString() {
        return "编号:"+id + "名字:" + name +"月份:" + month;
    }
}

import java.util.Comparator;

/**
 * 按名字排序的比较器
 */
public class NameComparator implements Comparator<Cat> {

    @Override
    public int compare(Cat o1, Cat o2) {
        //按名字升序排序 (name1与name2位置调换下,则会实现倒序的排序)
        String name1 = o1.getName();
        String name2 = o2.getName();
        int n = name1.compareTo(name2);
        return n;
    }

}

import java.util.ArrayList;
import java.util.Collections;

public class ComparatorCat {

    private static final ArrayList catList = new ArrayList();
    public static void main(String[] args) {

        Cat cat9 = new Cat(1,"cat9",9);
        Cat cat5 = new Cat(2,"cat5",5);
        Cat cat6 = new Cat(3,"cat6",6);
        catList.add(cat9);
        catList.add(cat5);
        catList.add(cat6);

        Collections.sort(catList, new NameComparator());
//        Collections.sort(catList, new AgeComparator());

        System.out.println(catList);
    }
}

【运行结果如下】:

[编号:2名字:cat5月份:5, 编号:3名字:cat6月份:6, 编号:1名字:cat9月份:9]

2、Comparable

①位于java.lang包

②在要比较的类上实现该接口

③调用sort方法时,只需指定集合名即可

使用顺序:

定义要比较的类,并实现comparable接口

测试

【下面是完整的案例代码】:

/**
 * 实现Comparable接口,泛型限定比较的类型
 */
public class Goods implements Comparable<Goods> {

    //商品编号
    private String id;
    //商品名称
    private String name;
    //商品价格
    private double price;

    public Goods() {

    }

    public Goods(String id, String name, double price) {
        this.setId(id);
        this.setName(name);
        this.setPrice(price);
    }
    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 double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "商品编号:" + id + ",商品名称:" + name + ",商品价格:" + price;
    }

    /**
     * //重写compareTo方法
     * @param o
     * @return
     */
    @Override
    public int compareTo(Goods o) {
        //取出商品价格
        double price1 = this.getPrice();
        double price2 = o.getPrice();
        //double类型的差值转为int
        int n = new Double(price2 - price1).intValue();
        return n;
    }
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ComparableGoods {

    public static void main(String[] args) {

        Goods good1 = new Goods("1","苹果",9.8);
        Goods good2 = new Goods("2","香蕉",4.9);
        Goods good3 = new Goods("3","柑橘",5.9);
        List goodList = new ArrayList();
        goodList.add(good1);
        goodList.add(good2);
        goodList.add(good3);

        Collections.sort(goodList);
        System.out.println(goodList);
    }
}

【运行结果如下】:

[商品编号:1,商品名称:苹果,商品价格:9.8, 商品编号:3,商品名称:柑橘,商品价格:5.9, 商品编号:2,商品名称:香蕉,商品价格:4.9]

(3)Comparator与Comparable接口比较

  • Comparator是在集合外部实现的排序,Comparable是在集合内部实现的排序;一个类实现了Camparable接口则表明该类对象之间是可相互比较的,该类对象组成的集合就可以直接使用Collections.sort()排序。
  • Comparator一种比较器,能将算法和数据分离,通过Comparator来实现排序而不必改变对象本身。
  • 可定义多种Comparator为同一个集合对象使用。
  • 21
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值