[关闭]
@File 2019-10-08T11:12:54.000000Z 字数 1486 阅读 124

集合

java



Collection 接口

List 接口(有序,可重复)

ArrayList 类

  • 非线性安全
  • 性能较好
  1. public static void main(String[] args){
  2. // 创建一个不能修改的list
  3. List list1 = Arrays.asList(new int[]{1,2,3,4});
  4. // 创建一个有值的List
  5. List list2 = new ArrayList(list1);
  6. // 创建一个空List
  7. List list3 = new ArrayList();
  8. }

LinkedList 类

  • 非线性安全
  1. public static void main(String[] args){
  2. // 创建一个不能修改的list
  3. List list1 = Arrays.asList(new int[]{1,2,3,4});
  4. // 创建一个有值的List
  5. List list2 = new LinkedList(list1);
  6. // 创建一个空List
  7. List list3 = new LinkedList();
  8. }

Vector 类

  • 线性安全

Set 接口(无序,不可重复)

HashSet 类

  • 非线程安全
  1. public static void main(String[] args){
  2. // 创建一个不能修改的list
  3. List list1 = Arrays.asList(new Integer[]{1,2,3,4});
  4. // 创建一个有值的set
  5. Set set1 = new HashSet(list1);
  6. // 创建一个空set
  7. Set set2 = new HashSet();
  8. }

TreeSet 类

  • 非线程安全
  1. public static void main(String[] args){
  2. // 创建一个不能修改的list
  3. List list1 = Arrays.asList(new Integer[]{1,2,3,4});
  4. // 创建一个有值的set
  5. Set set1 = new TreeSet(list1);
  6. // 创建一个空set
  7. Set set2 = new TreeSet();
  8. }

Map 接口(无序,以键值对存储,键不可重复)

集合类 Key Value Super 说明
Hashtable 不允许为null 不允许为null Dictionary 线程安全
ConcurrentHashMap 不允许为null 不允许为null AbstractMap 锁分段技术(JDK8:CAS)
TreeMap 不允许为null 允许为 null AbstractMap 线程不安全
HashMap 允许为 null 允许为 null AbstractMap 线程不安全

HashMap 类

  • 非线程安全
  1. public static void main(String[] args){
  2. // 创建一个空map
  3. Map map1 = new HashMap();
  4. }
  5. // list 转成 map(jdk1.8)
  6. // 网上找的,暂时没看懂
  7. public Map<Long, String> getIdNameMap(List<Account> accounts) {
  8. return accounts.stream().collect(Collectors.toMap(Account::getId, Account::getUsername));
  9. }

ThreeMap 类

  • 非线性安全

Hashtable 类

  • 线性安全
  • 有并发问题

ConcurrentHashMap类

  • 锁分段技术
  • 改善了并发问题
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注