JavaSE----Java 集合

11.Java 集合

1、集合

  • 集合是对象的容器,实现了对对象的常用操作,类似数组的功能。如果对象的存储个数不确定,那么就用集合容器来存储。集合都位于java.util.*包里。

  • 集合特点:

    • 用于存储对象的容器;
    • 集合的长度可以改变。
    • 集合不可以存储基本类型。
  • 集合和数组的区别:

    • 数组定义长度不可以改变,集合可以改变。
    • 数组可以存储基本类型和引用类型,集合只能存储引用类型。
  • 常用集合关系图:

2、Collection 集合

  • 主要方法:

    • add():添加对象
    • addAll():将一个集合中的所有对象添加到此集合中
    • clear():清空集合对象
    • contains():检查对象是否在集合中
    • equals():比较此集合是否与指定对象相等
    • isEmpty():判断集合是否为空
    • remove():移除集合中的一个对象元素
    • size():查看集合元素个数
    • iterator():遍历集合
  • 案例:

    public static void main(String[] args) {
    
            System.out.println("---创建集合对象---");
            Collection collection = new ArrayList();
            System.out.println("---增加元素---");
            //集合只能存储引用类型,不能存储基本数据类型,这里自动装箱。
            collection.add(19);
            collection.add(12);
            collection.add(13);
            collection.add(15);
            System.out.println(collection);
            System.out.println("集合中元素的个数为:" + collection.size());
    
            //asList():将数组转换成集合
            List list = Arrays.asList(new Integer[]{34, 57, 18});
            System.out.println("---将list集合添加到collection中");
            collection.addAll(list);
            System.out.println(collection);
            System.out.println("集合中元素的个数为:" + collection.size());
            
            System.out.println("---遍历元素--");
            System.out.println("---增强for循环遍历---");
            for (Object o: collection) {
                System.out.println(o);
            }
    
            System.out.println("---迭代器遍历---");
            /**
             * 迭代器:专门用来遍历集合的一种方式
             * hasNext():判断有没有下一个元素
             * next():获取元素,并下移
             */
            Iterator iterator = collection.iterator();
            while (iterator.hasNext()){
                Object next = iterator.next();
                System.out.println(next);
            }
    
            System.out.println("---比较集合---");
            System.out.println(collection.equals(list));
    
            System.out.println("---判断---");
            System.out.println(collection.contains(15));
            System.out.println(collection.contains(40));
            System.out.println(collection.isEmpty());
    
            System.out.println("---删除元素---");
            collection.remove(12);
            System.out.println(collection);
            System.out.println("集合中元素的个数为:" + collection.size());
    
            System.out.println("---清空元素---");
            collection.clear();
            System.out.println(collection);
            System.out.println("集合中元素的个数为:" + collection.size());
            System.out.println(collection.isEmpty());
    
        }
    

1、List 接口

  • List特点:有序、有下标、元素可以重复。
  • 方法:
    • add(index,Object o):在index位置插入对象o
    • boolean addAll(int index,Collection c):将一个集合中的元素添加到此集合中的index位置
    • Object get(int index):返回集合中指定位置的元素
    • List subList(int fromIndex, int toIndex):返回formIndex和toIndex之间的集合元素
1、ArrayList
  • 特点:
2、LinkedList
  • 特点:

  • ArrayList和LinkedList的区别?

    • ArrayList和LinkedList都是List的实现类,有序、有下表,元素可以重复。
    • ArrayList使用动态数组来存储元素,LinkedList使用双向链接来存储元素
    • ArrayList必须开辟连续空间,查询快,增删慢。
    • LinkedList无需开辟连续空间,查询慢,增删快。

2、Set 接口

1、HashSet
2、TreeSet

3、Map集合

4、Collections 工具类

posted @ 2021-09-25 20:50  丫头的星星  阅读(34)  评论(0编辑  收藏  举报