2. 容器
18. java 容器都有哪些?
19. Collection 和 Collections 有什么区别?
20. List、Set、Map 之间的区别是什么?
21. HashMap 和 Hashtable 有什么区别?
22. 如何决定使用 HashMap 还是 TreeMap?
23. 说一下 HashMap 的实现原理?
24. 说一下 HashSet 的实现原理?
25. ArrayList 和 LinkedList 的区别是什么?
26. 如何实现数组和 List 之间的转换?
27. ArrayList 和 Vector 的区别是什么?
28. Array 和 ArrayList 有何区别?
29. 在 Queue 中 poll()和 remove()有什么区别?
30. 哪些集合类是线程安全的?
31. 迭代器 Iterator 是什么?
32. Iterator 怎么使用?有什么特点?
33. Iterator 和 ListIterator 有什么区别?
34. 怎么确保一个集合不能被修改?
18. java 容器都有哪些?
collection-set-
hashset
LinkedHashSet
Treeset
list ArrayList
LinkedList
queue
Map--HashMap
TreeMap
LinkedHashMap
ConcurrentHashMap
19. Collection 和 Collections 有什么区别?
接口 定义规范 操作集合
包装类 提供静态方法操作集合
20. List、Set、Map 之间的区别是什么?
可重复 不重复
键值对
21. HashMap 和 Hashtable 有什么区别?
同步
null key一个null
22. 如何决定使用 HashMap 还是 TreeMap?
有序 按key值升序 红黑树结构
无序 数组 链表 红黑树结构
查询功能
增加、快速创建
23. 说一下 HashMap 的实现原理?
数组+链表
put hashCode算出数组坐标 存入
get 算出坐标 取
如果存的时候已经有了
equals() 比较 不同 链表方式头插
取的时候 遍历
24. 说一下 HashSet 的实现原理?
底层HashMap实现 值存在key中 value为统一固定值
add 调用map.put 存在不插入 不存在插入
25. ArrayList 和 LinkedList 的区别是什么?
数组结构 查询快
双向链表结构 增删快
26. 如何实现数组和 List 之间的转换?
Arrays.asList(array);
list.toArray();
27. ArrayList 和 Vector 的区别是什么?
同步
1.5 2 倍增长
28. Array 和 ArrayList 有何区别?
长度固定
存储同一对象
基元
29. 在 Queue 中 poll()和 remove()有什么区别?
队列 先进先出 尾近 堆
poll() 在队列为空是 返回null
remove() 抛出异常
peek() null 和element() 异常 不移除的情况下返回队头
offer() 满了false 和add()异常
30. 哪些集合类是线程安全的?
Vector hashtable
ConcurrentHashMap
Stringbuffer
Stack 栈
31. 迭代器 Iterator 是什么?
遍历集合用到
32. Iterator 怎么使用?有什么特点?
while(Iterator.hasnext()){
sout(iterator.next());
}
33. Iterator 和 ListIterator 有什么区别?
List 特有迭代器
反向遍历 hasPrevious()和previous()
编辑set
定位索引nextIndex()和previousIndex()
34. 怎么确保一个集合不能被修改?
map = Collections.unmodifiableMap(map)
private final static ImmutableList<Integer> list = ImmutableList.of(1, 2, 3);
private final static ImmutableMap<Integer, Integer> map = ImmutableMap.of(1,2,3,4,5, 6);
private final static ImmutableMap<Integer, Integer> map2 = ImmutableMap.<Integer, Integer>builder().put(1,2).put(3,4).put(5,6).build();
由ImmutableMap.builer() 对象初始化
builder.put();
ImmutableMap<> map= builder.build();