Collection 基本容器

本文深入探讨Java集合框架中List和Set的区别与应用,包括ArrayList、LinkedList的特点及使用场景,以及HashSet和TreeSet如何保证元素唯一性和排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
Collection
    |--List:元素是有序的,元素可以重复,因为该集合体系有索引
        |--ArrayList:底层的数据结构使用的是数组结构,查询修改比较方便,但是增删较慢(默认长度为10)
        |--LinkedList:底层使用的是链表结构,增删的速度很快,但查询的速度慢
        |--Vector:底层使用的是数组数据结构,线程同步,支持枚举,所以速度慢,被ArrayList替代了
    |--Set:元素是无序的,元素不可以重复
        |--HashSet:底层数据结构是哈希表
        |--TreeSet:二叉树,提高比较效率

List:
特有方法:凡事有角标的方法都是该体系的特有方法

增:add(index,element);
   addAll(index,Collection);
删:remove(index);
改:set(index,element);
查:get(index);
   subList(from,to);
   listIterator();

Set:元素是无序的(存入和取出的顺序不一定一致),元素不可以重复
    |--HashSet:底层数据结构是哈希表
        <1>HashSet如何保证元素的唯一性?
            是通过元素的两个方法:hashCode和equals来完成
            如果元素的HashCode值相同,才会判断equals是否为true;
            如果元素的HashCode值不同,不会调用equals;
        <2>对于判断元素是否存在,删除操作时,依赖的方法是元素的hashcode,equals方法
    |--TreeSet:可以对Set的元素进行排序(强行排序)
               底层数据结构是二叉树;
               保证元素唯一性的依据:compareTo方法的返回值

               TreeSet排序的第一种方式:让元素自身具有比较性
                    元素需要实现Comparable接口,覆盖Comparable方法
               TreeSet的第二种排序方式:当元素不具备比较性时,或者具备的比较性不是所需要的
                    这时就需要让集合自身具备比较性:在集合初始化时,就有了比较方式


List集合特有的迭代器:ListIterator是Iterator的子接口
在迭代时,不可以通过集合对象的方法操作集合中的元素,因为:ConcurrentModificationException异常

所以,在使用迭代器时,只能用迭代器的方法操作元素,可是Iterator方法是有限的
只能对元素进行判断,取出,删除的操作,
如果想要其他的操作如:添加,修改等,就需要使用其子接口,ListIterator

该接口只能通过List集合的listIterator方法获取


*/

import java.util.*;
class  ListDemo
{
    //迭代器
    public static void print(ArrayList obj)
    {
        Iterator it=obj.iterator();
        while(it.hasNext())
        {
            sop("next:"+it.next());
        }
    }
    //输出
    public static void sop(Object obj)
    {
        System.out.println(obj);
    }
    //增加
    public static void add(ArrayList al)
    {
        //添加元素
        al.add("java01");
        al.add("java02");
        al.add("java03");
        //在指定位置添加元素
        al.add(1,"java04");
        print(al);
    }
    public static void delete(ArrayList al)
    {
        //删除指定位置的元素
        al.remove(1);
        print(al);
    }
    public static void change(ArrayList al)
    {
        al.set(2,"java001");
        print(al);
    }
    public static void get(ArrayList al)
    {
        sop("get(1):"+al.get(1));
    }
    public static void main(String[] args)
    {
        ArrayList al=new ArrayList();
        //add(al);
        //delete(al);
        //change(al);
        //get(al);
        it(al);
    }

    public static void it(ArrayList al)
    {
        al.add("java01");
        al.add("java02");
        al.add("java03");
        //Iterator it=al.iterator();
        ListIterator li=al.listIterator();
        /*出现异常:ConcurrentModificationException
        while(it.hasNext())
        {
            并发访问,多种重复操作,既用迭代器取元素,又用集合添加元素
              引发安全问题
            Object obj=it.next();
            //在迭代过程中,添加或删除元素
            if(obj.equals("java02"))
                al.add("java002");
                it.remove();//将java02引用从集合中删除
            sop("obj="+obj);
        }
        */
        while(li.hasNext())
        {
            Object obj=li.next();
            //在迭代过程中,添加或删除元素
            if(obj.equals("java02"))
                //li.add("java002");
                li.set("java005");
            sop("obj="+obj);
        }
        sop(al);

        //sop("hasNext:"+li.hasNext());//false 元素遍历完毕,无后继元素
        //sop("hasPrevious:"+li.hasPrevious());//true,元素便利结束,有前驱元素
        while(li.hasPrevious())
        {
            sop("pre:"+li.previous());
        }

    }

}


import java.util.*;
class TreeSetDemo02
{
    public static void main(String[] args)
    {
        TreeSet ts=new TreeSet();//按照第一种方法:年龄排序
        TreeSet ts=new TreeSet(new MyCompare());//按照第二种方法:姓名排序
        try
        {
            ts.add(new Student("stu01",20));
            ts.add(new Student("stu02",20));
            ts.add(new Student("stu03",18));
            ts.add(new Student("stu01",28));
            ts.add(new Student("stu02",10));
        }
        catch(RuntimeException e)
        {
            System.out.println(e.getMessage());
        }
        Iterator it=ts.iterator();
        while(it.hasNext())
        {
            Student stu=(Student)it.next();
            System.out.println(stu.getName()+"...."+stu.getAge());
        }
    }
}
class Student implements Comparable
{
    private String name;
    private int age;
    Student(String name,int age)
    {
        this.name=name;
        this.age=age;
    }
    public String getName()
    {
        return this.name;
    }
    public int getAge()
    {
        return this.age;
    }
    public int compareTo(Object obj)
    {
        if(!(obj instanceof Student))
            throw new RuntimeException("对象不是学生类型");
        Student stu=(Student) obj;
        System.out.println(stu.name+"...com..."+this.name);
        if(this.age>stu.age)
        {
            return 1;
        }
        if(this.age==stu.age)
        {
            return (this.name).compareTo(stu.name);
        }
        return -1;
    }
}
//实现按姓名排序
class MyCompare implements Comparator
{
    public int compare(Object o1,Object o2)
    {
        Student stu1=(Student) o1;
        Student stu2=(Student) o2;
        int num=stu1.getName().compareTo(stu2.getName());
        if(num==0)
        {
            return new Integer(stu1.getAge().compareTo(stu2.getAge()));
            /*
            if(stu1.getAge()>stu2.getAge())
                return 1;
            else if(stu1.getAge()==stu2.getAge())
                return 0;
            return -1;
            */
        }
        return num;
    }
}


import java.util.*;
class  TreeSetDemo01
{
    public static void main(String[] args)
    {
        TreeSet ts=new TreeSet();
        try{
            ts.add(new Student("stu1",20));
            ts.add(new Student("stu2",21));
            ts.add(new Student("stu3",22));
            ts.add(new Student("stu3",24));
            ts.add(new Teacher("tea2",24));
        }
        catch(RuntimeException e)
        {
            System.out.println(e.getMessage());
        }

        Iterator it=ts.iterator();
        while(it.hasNext())
        {
            Student stu=(Student)it.next();
            System.out.println(stu.getName()+"..."+stu.getAge());
        }
    }
    public static void sop(Object obj)
    {
        System.out.println(obj);
    }
}
class Student implements Comparable //该接口强制让学生具备比较性
{
    private String name;
    private int age;
    Student(String name,int age)
    {
        this.name=name;
        this.age=age;
    }
    public int compareTo(Object obj){
            if(!(obj instanceof Student))
                throw new RuntimeException("不是学生对象!");
        Student stu=(Student)obj;
        System.out.println(this.name+"...equals..."+stu.name);
        if(this.age>stu.age)
        {
            return 1;
        }
        else if(this.age==stu.age)
        {
            return this.name.compareTo(stu.name);
        }
        return -1;
    }
    public String getName()
    {
        return this.name;
    }
    public int getAge()
    {
        return this.age;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值