我整合了多个技术平台上的相关内容,从常见问题入手,结合应用实例,为你梳理出这篇Java集合面试题总结,希望能助你学习一臂之力。
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(1);
arrayList.add(2);
// 当继续添加元素时,如果当前容量不足,会自动扩容
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("a");
linkedList.add("b");
// 链表节点之间通过引用相互连接
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
int element = list.get(1); // 可以快速获取索引为1的元素,即20
LinkedList<Integer> linkedList = new LinkedList<>();
linkedList.add(10);
linkedList.add(20);
linkedList.add(30);
// 获取索引为1的元素,需要从链表头开始遍历
int element = linkedList.get(1);
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
// 在索引为1的位置插入元素4,后续元素都要向后移动
list.add(1, 4);
LinkedList<Integer> linkedList = new LinkedList<>();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
// 在索引为1的位置插入元素4,只需修改相邻节点引用
linkedList.add(1, 4);
HashMap<Integer, String> hashMap = new HashMap<>();
// 多线程同时进行put操作,可能会出现问题
new Thread(() -> {
hashMap.put(1, "a");
}).start();
new Thread(() -> {
hashMap.put(2, "b");
}).start();
Hashtable<Integer, String> hashtable = new Hashtable<>();
// 多线程环境下使用相对安全
new Thread(() -> {
hashtable.put(1, "a");
}).start();
new Thread(() -> {
hashtable.put(2, "b");
}).start();
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(null, "nullValue");
hashMap.put(1, null);
Hashtable<Integer, String> hashtable = new Hashtable<>();
// 以下代码会抛出NullPointerException
// hashtable.put(null, "value");
// hashtable.put(1, null);
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "a");
hashMap.put(2, "b");
Iterator<Map.Entry<Integer, String>> iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> entry = iterator.next();
// 这里如果直接调用hashMap.put(3, "c");会抛出异常
iterator.remove();
}
Hashtable<Integer, String> hashtable = new Hashtable<>();
hashtable.put(1, "a");
hashtable.put(2, "b");
Enumeration<Map.Entry<Integer, String>> enumeration = Collections.enumeration(hashtable.entrySet());
while (enumeration.hasMoreElements()) {
Map.Entry<Integer, String> entry = enumeration.nextElement();
// 这里如果调用hashtable.put(3, "c");不会立即抛出异常
}
HashMap<Integer, String> hashMap = new HashMap<>();
for (int i = 0; i < 13; i++) {
hashMap.put(i, "value" + i);
// 当添加到第13个元素时,会触发扩容
}
Hashtable<Integer, String> hashtable = new Hashtable<>();
for (int i = 0; i < 9; i++) {
hashtable.put(i, "value" + i);
// 当添加到第9个元素时,会触发扩容
}
HashSet<Integer> hashSet = new HashSet<>();
hashSet.add(1);
hashSet.add(2);
// 内部通过HashMap存储,键为1和2,值为固定对象
TreeSet<Integer> treeSet = new TreeSet<>();
treeSet.add(3);
treeSet.add(1);
treeSet.add(2);
// 内部通过红黑树存储,元素会按照从小到大的顺序排列
HashSet<Integer> hashSet = new HashSet<>();
hashSet.add(3);
hashSet.add(1);
hashSet.add(2);
// 遍历hashSet时,元素顺序可能不是3, 1, 2
for (int i : hashSet) {
System.out.print(i + " ");
}
TreeSet<Integer> treeSet = new TreeSet<>();
treeSet.add(3);
treeSet.add(1);
treeSet.add(2);
// 遍历treeSet时,元素顺序是1, 2, 3
for (int i : treeSet) {
System.out.print(i + " ");
}
HashSet<Integer> hashSet = new HashSet<>();
hashSet.add(1);
boolean contains = hashSet.contains(1); // 快速判断是否包含元素1,时间复杂度O(1)
TreeSet<Integer> treeSet = new TreeSet<>();
treeSet.add(1);
boolean contains = treeSet.contains(1); // 判断是否包含元素1,时间复杂度O(log n)
class CustomObject {
private int id;
public CustomObject(int id) {
this.id = id;
}
@Override
public int hashCode() {
return id;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
CustomObject other = (CustomObject) obj;
return id == other.id;
}
}
HashSet<CustomObject> hashSet = new HashSet<>();
hashSet.add(new CustomObject(1));
hashSet.add(new CustomObject(1)); // 由于hashCode和equals方法判断为相同元素,不会重复添加
class CustomComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
}
TreeSet<Integer> treeSet = new TreeSet<>(new CustomComparator());
treeSet.add(1);
treeSet.add(1); // 由于比较器判断为相同元素,不会重复添加
ConcurrentHashMap<Integer, String> concurrentHashMap = new ConcurrentHashMap<>();
// 多个线程可以同时对不同的Segment进行操作
new Thread(() -> {
concurrentHashMap.put(1, "a");
}).start();
new Thread(() -> {
concurrentHashMap.put(17, "b"); // 17和1可能位于不同的Segment,可并发操作
}).start();
// 假设某个Segment的数组长度为4
Segment<K, V> segment = new Segment<>(16, 0.75f);
// 当有元素put进来时,如果哈希冲突,会在链表上进行存储
ConcurrentHashMap<Integer, String> concurrentHashMap = new ConcurrentHashMap<>();
// 多个线程读操作可并发
new Thread(() -> {
String value = concurrentHashMap.get(1);
}).start();
new Thread(() -> {
String value = concurrentHashMap.get(2);
}).start();
// 写操作先尝试CAS,失败后加锁
new Thread(() -> {
concurrentHashMap.put(3, "c");
}).start();
ConcurrentHashMap<Integer, String> concurrentHashMap = new ConcurrentHashMap<>();
// 当某个桶中的链表长度超过8时,会转换为红黑树
for (int i = 0; i < 10; i++) {
concurrentHashMap.put(i, "value" + i);
// 假设这些元素哈希冲突,都在同一个桶中,当添加到第9个元素时,链表可能转换为红黑树
}
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "a");
hashMap.put(2, "b");
for (Map.Entry<Integer, String> entry : hashMap.entrySet()) {
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println("Key: " + key + ", Value: " + value);
}
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "a");
hashMap.put(2, "b");
for (Integer key : hashMap.keySet()) {
String value = hashMap.get(key);
System.out.println("Key: " + key + ", Value: " + value);
}
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "a");
hashMap.put(2, "b");
for (String value : hashMap.values()) {
System.out.println("Value: " + value);
}
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "a");
hashMap.put(2, "b");
Iterator<Map.Entry<Integer, String>> iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> entry = iterator.next();
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println("Key: " + key + ", Value: " + value);
}
Hashtable<Integer, String> hashtable = new Hashtable<>();
hashtable.put(1, "a");
hashtable.put(2, "b");
for (Map.Entry<Integer, String> entry : hashtable.entrySet()) {
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println("Key: " + key + ", Value: " + value);
}
Hashtable<Integer, String> hashtable = new Hashtable<>();
hashtable.put(1, "a");
hashtable.put(2, "b");
for (Integer key : hashtable.keySet()) {
String value = hashtable.get(key);
System.out.println("Key: " + key + ", Value: " + value);
}
Java 集合面试题,Java 集合常见问题,ArrayList 面试题,LinkedList 面试题,HashMap 面试题,Hash
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。