各容器区别比较

1. List:元素是有序的,可重复,可以根据索引操作数据

  1.1 ArrayList

    数据结构:数组

    线程不安全

  1.2 LinkedList

    数据结构:双向链表

    线程不安全

  1.3 Vector

    数据结构:数组

    线程安全:Synchronized

2. Set:元素无序,不可重复,取出元素只能使用迭代器

  2.1 HashSet

    数据结构:哈希表 + 红黑树

    允许元素为null

    底层是一个HashMap实例

    非同步,线程不安全,存取速度快

  2.2 TreeSet

    数据结构:红黑树,可排序

    不允许元素为null

    底层是一个TreeMap

    非同步

3. Map:键值对

  3,1 HashMap

    数组+链表+红黑树

    允许null作为键,null作为值

    线程不安全

  3.2 HashTable

    数组+链表+红黑树

    不允许null作为键,null作为值

    线程安全

  3.3 LinkedHashMap

4.Collection & Collections

  Collection是一个集合接口,提供了集合对象进行操作的基本方法。直接继承接口的有List Set Queue。

  Collections是一个工具类,含有多种静态方法,用于对集合元进行排序、搜索以及线程安全等操作,服务

于Collection。

//1.排序
Collections.sort(Collection c)
//2.反转
Collections.reverse(Collection c)
//3.替换所有元素
Collections.fill(List list , Object o)
//4.拷贝
Collections.copy(List list1,List list2)
//5.循环移动,集合中的元素向后移m个位置,在后面被遮盖的元素循环到前面来。
Collections.rotate(List list,int m)
//6.交换
Collections.swap(List list, int m, int n)
//同步容器
public static <T> Collection<T> synchronizedCollection(Collection<T> c) {
    return new SynchronizedCollection<>(c);
}

猜你喜欢

转载自www.cnblogs.com/qmillet/p/12499838.html