@File
2019-10-08T11:12:54.000000Z
字数 1486
阅读 124
java
- 非线性安全
- 性能较好
public static void main(String[] args){
// 创建一个不能修改的list
List list1 = Arrays.asList(new int[]{1,2,3,4});
// 创建一个有值的List
List list2 = new ArrayList(list1);
// 创建一个空List
List list3 = new ArrayList();
}
- 非线性安全
public static void main(String[] args){
// 创建一个不能修改的list
List list1 = Arrays.asList(new int[]{1,2,3,4});
// 创建一个有值的List
List list2 = new LinkedList(list1);
// 创建一个空List
List list3 = new LinkedList();
}
- 线性安全
- 非线程安全
public static void main(String[] args){
// 创建一个不能修改的list
List list1 = Arrays.asList(new Integer[]{1,2,3,4});
// 创建一个有值的set
Set set1 = new HashSet(list1);
// 创建一个空set
Set set2 = new HashSet();
}
- 非线程安全
public static void main(String[] args){
// 创建一个不能修改的list
List list1 = Arrays.asList(new Integer[]{1,2,3,4});
// 创建一个有值的set
Set set1 = new TreeSet(list1);
// 创建一个空set
Set set2 = new TreeSet();
}
集合类 | Key | Value | Super | 说明 |
---|---|---|---|---|
Hashtable | 不允许为null | 不允许为null | Dictionary | 线程安全 |
ConcurrentHashMap | 不允许为null | 不允许为null | AbstractMap | 锁分段技术(JDK8:CAS) |
TreeMap | 不允许为null | 允许为 null | AbstractMap | 线程不安全 |
HashMap | 允许为 null | 允许为 null | AbstractMap | 线程不安全 |
- 非线程安全
public static void main(String[] args){
// 创建一个空map
Map map1 = new HashMap();
}
// list 转成 map(jdk1.8)
// 网上找的,暂时没看懂
public Map<Long, String> getIdNameMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getId, Account::getUsername));
}
- 非线性安全
- 线性安全
- 有并发问题
- 锁分段技术
- 改善了并发问题