Java-集合

2024-02-18  本文已影响0人  没有了遇见

Java 集合

1:集合的产生???
程序运行中我们会创建非常多的对象,为了对这些对象存放和管理,就提出了容器的概念.
数组就是一个容器,但是数组长度是固定的,这时候就出现了一个集合的概念.

1:什么是集合
Java集合:是一个用来存放对象的容器

注意:
1:集合只能存储对象,基本数据类型,会自动转换成对象类然后存入
2:集合存储的是对象的引用而非本身,对象还是存在堆内存中
3:集合可以存放不同类型 不限制数量(泛型统一类型,自动扩容)

问题:

集合和数组的区别
1:长度不同 结合不固定长度 数组固定长度
2:数据不同 数组存储基本数据类型和引用数据类型 集合只能存储引用数据类型(基本数据类型会转换为对象)
3:存储元素不同 数组只能存储同一种类型元素 集合存储多种数据类型(一般也是存储一种数据类型,泛型规范数据类型)

类型图:
https://img2018.cnblogs.com/blog/1175569/201908/1175569-20190813185822827-390071136.png

集合:
Collection 单列集合 实现 Iterable 接口 封装了 Iterator 迭代器

Map 双列集合 key values 形式存储数据

结构:

Collection ---> AbstractCollection extends Collection

            -->AbstractQueue implements AbstractCollection
            -->AbstractList  implements AbstractCollection
            -->AbstractSet   implements AbstractCollection

Collection ---> List
---> AbstractList extends AbstractCollection implements List

                                ---> ArrayList extends AbstractList implements  List<E>
                                
                                ---> Vector extends AbstractList<E> implements  List<E>
                                            ---> Stack                  
       ---> Set
                --->AbstractSet extends AbstractCollection  implements Set
                                --->HashSet extends AbstractSet<E> implements Set<E>

                                无序唯一 允许Null
                                            ---> LinkedHashSet 链表+hash 有序唯一 允许Null
                                            --->TreeSet
                ---> SortedSet<E> extends Set<E>
                
                            --->TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>
                            
                               自然排序 定义排序 不能未空
                            
       ---> Queue extends AbstractQueue  implements Set
       
                --- AbstractQueue extends   AbstractCollection implements Queue<E>
                
               LinkedList extends AbstractQueue implements List<E>, Deque<E

Collection 实现 ---> AbstractCollection 实现Collection接口

                                --->AbstractList 继承 AbstractCollection 实现List接口
                                                ---> AbstractSequentialList
                                                                            --->LinkedList
                                                --->Vetor
                                                        --->Stack
                                                --->ArrayList AbstractList (实现List 接口)      

Map --->AbstractMap implements Map
--->HashMap 允许 NUll key 0
--->LinkedHashMap
--->ConcurrentHashMap
--->WeakHashMap

--->SortedMap extends Map
            --->TreeMap
            
 --->HashTable  extends Dictionary  implements Map  HashTable  key values 都不能为空 
            ---> Properties

Vector、HashTable extends Dictionary 、 ConcurrentHashMap extends AbstratMap Properties extends Hashtable 线程同步,线程安全 其他不安全

问题:
Hash冲突??
hash 是将数据生成一个int类型的数字,数据是无线的hashCode 是有线的 所以当不同数据得到相同的hashCode的情况就叫Hash冲突
解决方案: 再hash

/**
* 集合去重
*/
public static void processList() {

    ArrayList<String> arrayList = new ArrayList<>();
    long sTime;
    for (int i = 0; i < 10000; i++) {
        arrayList.add("位置:" + i);
        if (i % 2 == 0) {
            arrayList.add("位置:" + i);
        }
    }
    
    sTime = System.currentTimeMillis();
    //因为 LinkedHashSet 链表+hash 实现  数据唯一 有序  
    LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(arrayList);
    List newList = Arrays.asList(linkedHashSet.toArray());
    ArrayList<String> arrayList3 = new ArrayList<String>(newList);
    System.out.println("时间:" + (System.currentTimeMillis() - sTime) + "");
    System.out.println("数据:" + arrayList3);

}

Collection 单列集合

Collection接口 提供了迭代器的方法 提供了add contains remove clear() size() 等方法

1:List 继承于Collection接口 有序可重复集合(允许Null) 提供了get 方法
    List接口继承于Collection接口拥有迭代器访问的属性,同时提供了有序存储和获取的方法 可以根据索引获取对象
    
    1:ArrayList实现有序访问可重复
            问题:ArrayList实现有序访问原因?
    add()的时候 数组+System.arraycopy()  通过索引存储
    2: Vector 和ArrayList 相似 同步线程安全(add方法 synchronized 修饰,所以是同步的)
    LinkedList LinkedList是采用双向循环链表实现,LinkedList是List接口的另一个实现 有序  不唯一
    既可以当一个接口也可以链表操作
    集合链表实现 
    
 数组:查询快,增删慢
 链表:查询慢增删快
 
 功能:
 ArrayList 底层数组 有序不唯一   线程不安全
 Vector    和ArrayList一样  线程安全
 LinkedList: 实现了Queue接口和List 接口 底层双链表 有序 不唯一  查询慢 增删快
    
    
2:Set 继承于Collection接口无序集合 Set代表无序不可重复集合,只能根据元素本身来访问

    HashSet 怎么实现唯一 ? hash保证唯一
    内部维护了一个HashMap 利用Key 唯一的特点存储数据保证唯一(会出现hash冲突)线程不同步
HashSet  LinkedHashSet  TreeSet 

HashSet 底层HashMap 数据唯一 
LinkedHashSet 继承 HashSet实现了数据唯一 调用addAll()数据通过角标添加到容器保证了顺序   
TreeSet 红黑树 支持排序

3:Queue继承于Collection接口 
    3.1是Java中定义的一种队列数据结构,元素是有序的(按插入顺序排序),先进先出(FIFO)原则
    3.2通常,队列不允许随机访问队列中的元素
LinkedList
上一篇 下一篇

猜你喜欢

热点阅读