Interview Question 3-Collection

1.ArrayList

Ordered, subscripted, thread unsafe, Null allowed
1. Initially an empty Object array
2. When we add elements for the first time, the length of the array is assigned to 10,
3. When the collection length is not enough, expand 1.5 times the original
4. Query is fast, adding and deleting are slow

usage:

创建:ArrayList< >list = new ArrayList<>();
增加:list.add();
删除:list.remove();
修改:list.set();
查看:list.get();

Ways to traverse the ArrayList

1. Ordinary for loop traversal
2. Enhanced for loop traversal (foreach)
3. Comparison of advantages and disadvantages of iterator traversal 1. For
loop is based on subscript search, so we can do some subscript operations
2. If subscript is not used, It is recommended to use enhanced for loops or iterators.
3. Do not delete or add data in enhanced for loops or iterators. Concurrency exceptions will be reported
. 4. Enhanced for is still implemented by iteration. It is a new addition to JDK 1.5, iteration. It is a summary of JDK1.2: ArrayList is characterized by fast query, slow addition and deletion, because it involves moving elements, so we use it in some scenarios where the proportion of queries is higher than that of deletion and addition, such as: e-commerce system

2.Vector

The vector class has the same API as ArrayList, but it is a thread-safe interview question: What is the difference between ArrayList and Vector?
1. Vector is available in JDK1.0, and ArrayList is only
available in JDK1.2. 2. Vector's parameterless construction directly assigns the array length to 10, while ArrayList assigns the array length to 10 when the content is added for the first time
3. Vector expansion is twice the original, ArrayList is 1.5 times the original
4. Vector is thread-safe, ArrayList is thread-unsafe traversal method, they are the same

3.linkedlist

Features: Unordered, can be Null, thread is not safe, doubly linked list query is slow, add and delete fast
1. Usage: Create: LinkedList list = new LinkedList(); Add: list.add(); Delete: list.remove (); list.removeFirst(); list.removeLast();
Modification: list.set (subscript, modified content); Find: list.get (subscript);
2. Traverse the same three methods as Arraylist

list is not safe

// java.util.ConcurrentModificationException 并发修改异常
public class Thread12 {
    
    
public static void main(String[] args) {
    
    
//并发下 arraylist 不安全
/**
* 解决方案:
* 1.List<String> list = new Vector<>(); 安全的加锁了 不高分
* 2.List<String> list = Collections.synchronizedList(new ArrayList<>());用Collections工具类的安全方法
* 3. List<String> list = new CopyOnWriteArrayList<>();juc解决并发编程 推荐用
* CopyOnWriteArrayList:底层用的是transient 和 volatile 两个关键字修饰的
* copyOnWrite 写入时复制 COW 计算机领域的一种优化策略
* 多线程调用时,list 读取的时候 固定的 写入覆盖
* 在写入的时候避免覆盖 造成数据问题
* 读写分离
* CopyOnWriteArrayList 比 vector 高级在哪?
* vector 底层是 synconized 修饰的 效率低
* CopyOnWriteArrayList 用的是 lock 锁 add 是 copyof 然后 set回去
*/
// List<String> list = new ArrayList();
List<String> list = new CopyOnWriteArrayList<>();
for (int i = 1; i < 100; i++) {
    
    
new Thread(()->{
    
    
list.add(UUID.randomUUID().toString().substring(0,5));
System.out.println(list);
},String.valueOf(i)).start();
}
}
}

4.set collection

Set collection is unordered and non-repeatable, divided into hashset treeset

4.1Hashset:

1. The subclass of set collection, the bottom layer is the hash table. It stores elements by judging the hash value, not in order.
2. It judges the repetition according to the hashcode and equals methods.
3. First, when an element When storing, first use the hashcode to get the position of the element. If there is no element at that position, insert it directly. If there is already an element at the position, use the equals method to determine whether the two elements are the same. If it returns true, the element is considered the same and insert If it fails, if it returns false, it indicates that the two elements are different. Stored in a postponed manner. It can be considered that the elements with the same hash value are placed in a hash pass, which is somewhat similar to the linked list storage of hash.

4.2TreeSet

The bottom layer of TreeSet is actually implemented with TreeMap, and a simplified version of TreeMap is maintained internally, which stores Set elements through keys.
The stored elements need to be sorted inside the TreeSet. Therefore, our corresponding class needs to implement the Comparable interface. In this way, the size between the objects can be compared according to the compareTo() method, and the internal sorting can be performed.

set is not safe

//java.util.ConcurrentModificationException
public class Thread13 {
    
    
public static void main(String[] args) {
    
    
/**
* 解决方案:
* 1.Set<String> set = Collections.synchronizedSet(new HashSet<>());
* 2. Set<String> set = new CopyOnWriteArraySet();
*/
Set<String> set = new CopyOnWriteArraySet();
for (int i = 0; i < 30; i++) {
    
    
	new Thread(()->{
    
    
		set.add(UUID.randomUUID().toString().substring(0,5));
		System.out.println(set);
},String.valueOf(i)).start();
}
}
}

Guess you like

Origin blog.csdn.net/zyf_fly66/article/details/114016183