Java集合框架详解1

1,013 阅读2分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第7天,点击查看活动详情

简介:本篇详细去讲一下Java集合框架,如ArrayList、HashSet、HashMap等。

一、什么是集合

1.1 概念

对象的容器,实现了对对象常用的操作。

1.2 与数组的区别

  1. 数组长度固定,集合长度不固定。
  2. 数组可以存储基本类型和引用类型,集合只能存储引用类型。

1.3 导包

import java.util.*;

二、Collection体系

请添加图片描述

2.1 Collection 父接口

特点:代表一组任意类型的对象,无序、无下标、不能重复。

创建集合: Collection collection = new ArrayList();

2.1.1 常用方法

  1. 添加元素 collection.add();

  2. 删除元素

    collection.remove();

    collection.clear();

  3. 遍历元素(重点)

    1. 使用增强for(因为无下标)

      for(Object object : collection){ }

    2. 使用迭代器

      //haNext(); 有没有下一个元素
      //next(); 获取下一个元素
      //remove(); 删除当前元素
      Iterator it = collection.iterator();
      while(it.hasNext()){
        String object = (String)it.next(); //强制
        // 可以使用it.remove(); 进行移除元素
        // collection.remove(); 不能用collection其他方法 会报并发修改异常
      }
      
  4. 判断 collection.contains():是判断一个集合是否包含某个指定对象; collection.isEmpty():是判断一个集合是否为空值;

2.2 List 子接口

特点:有序、有下标、元素可重复

创建集合对象 List list = new ArrayList<>( );

2.2.1 常用方法

  1. 添加元素 list.add( ); 会对基本类型进行自动装箱

  2. 删除元素 可以用索引 list.remove(0)

    当删除数字与索引矛盾时 对数字强转

    list.remove((Object) 10)list.remove(new Integer(10))

  3. 遍历

    1. 使用for遍历

      for(int i = 0; i < lise.size(); i++){
        System.out.print(list.get(i)); 
      }
      
    2. 使用增强for

      for(Object list: collection){ }

    3. 使用迭代器

      Iterator it = collection.iterator();
      while(it.hasNext()){
        String object = (String)it.next(); //强转
        // 可以使用it.remove(); 进行移除元素
        // collection.remove(); 不能用collection其他方法 会报并发修改异常
      }
      
    4. 使用列表迭代器 💡(注意和迭代器区别)

      ListIterator li = list.listIterator();
      while(li.hasNext()){
        System.out.println(li.nextIndex() + ":" + li.next()); //从前往后遍历
      }
      while(li.hasPrevious()){
        System.out.println(li.previousIndex() + ":" + li.previous()); //从后往前遍历
      }
      
  4. 获取 list.indexOf( );

  5. 返回子集合 sublist(x, y); 左闭右开

    List subList = list.subList(1, 3); 返回索引 1、2

2.2.2 List实现类

  • ArrayList 【重点】
    • 数组结构实现,必须要连续空间,查询快、增删慢
  • Vector
    • 数组结构实现,查询快、增删慢
  • LinkedList
    • 双向链表结构实现,无需连续空间,增删快,查询慢
2.2.2.1 ArrayList

创建集合 ArrayList arrayList = new ArrayList<>();

  1. 添加元素 arrayList.add();

  2. 删除元素 arrayList.remove(new Student("name", 10));

    这里重写了 equals(this == obj) 方法

    public boolean equals(Object obj){
      //1 判断是不是同一个对象
      if(this == obj){
        return true;
      }
      //2 判断是否为空
      if(obj == null){
        return false;
      }
      //3 判断是否是Student类型
      if(obj instanceof Student){
        Student == (Student)obj;
        //4 比较属性
        if(this.name.equals(s.getName()) && this.age == s.getAge()){
          return true;
        }
      }
      //5 不满足条件返回false
      return false;
    }
    
  3. 遍历元素【重点】

    1. 使用迭代器

      Iterator it = arrayList.iterator();
      while(it.hasNext()){
        Student s = (Student)it.next(); //强转
      }
      
    2. 列表迭代器

      ListIterator li = arrayList.listIterator();
      while(li.hasNext()){
        Student s = (Student)li.next(); //从前往后遍历
      }
      
      while(li.hasPrevious()){
        Student s = (Student)li.previous();//从后往前遍历
      }
      
  4. 判断

    arrayList.contains();arrayList.isEmpty();

  5. 查找

    arrayList.indexof();

2.2.2.2 Vector

创建集合 Vector vector = new Vector<>();

增加、删除、判断同上

遍历中枚举器遍历

Enumeration en = vector.elements();
while(en.hasMoreElements()){
  String o = (String)en.nextElement();
  System.out.println(o);
}
2.2.2.3 LinkedList

创建链表集合LinkedList li = new LinkedList<>();

常用方法与List一致