Analysis of Java collection class

java. util. Collection is a top-level interface of a collection class. It provides general interface methods for operating collection objects, including the basic operations and properties of a collection. The direct inheritance interfaces are list and set.

List

The list interface and its implementation class are variable capacity lists that can access the elements in the collection by index.

Features: the elements in the set are orderly and repeatable;

The list interface has three implementation classes: ArrayList, LinkedList and vector.

1. ArrayList ArrayList is the most commonly used list implementation class. It is internally implemented through an array. It allows fast random access to elements. In essence, it and vector can be regarded as an array that can automatically increase capacity, that is, when the array length is not enough, a larger array will be defined and the previous array will be copied to the new array, but the size of the extended array of ArrayList and vector is different.

When inserting or deleting elements from the middle of the ArrayList, you need to copy and move the array, which is expensive. Therefore, it is suitable for random search and traversal, not for insertion and deletion.

It provides functions similar to the vector class, but it does not support thread synchronization.

ArrayList operation

add to:

List list = new ArrayList();  
// 在列表的尾部追加元素  
list.add("咩");  
// 在列表的头部追加元素  
list.add(0,"咩");  
// 追加指定 collection 中的所有元素到列表结尾  
list.addAll(new ArrayList());  

Query:

// 返回列表中指定位置的元素  
list.get(0); 
// 返回列表中的元素数  
list.size();  
// 返回列表中索引为0到2的元素
list.subList(0,2);  
// 将list直接转为Object[] 数组后返回
list.toArray();  
// 将list转化为需要类型的数组
list.toArray(T[] a); 
// 返回列表的哈希值,hascode实际上是返回的对象存储的物理地址
list.hashCode();  
// 返回列表中首次出现指定元素的索引(不包含此元素,则返回-1)
list.indexOf("咩");  
// 返回列表中最后出现指定元素的索引(不包含此元素,则返回-1) 
list.lastIndexOf("咩");
// 如果列表包含指定的元素,则返回true  
list.contains("咩");
// 如果列表包含指定collection的所有元素,则返回true  
list.containsAll(new ArrayList());  
// 比较指定的对象与列表是否相等  
list.equals(new ArrayList());  
// 如果列表不包含元素,则返回 true  
list.isEmpty();  

to update:

// 移除列表中的所有元素  
list.clear();  
// 移除列表中指定索引的元素  
list.remove(0);  
// 移除列表中出现的首个指定元素  
list.remove("咩");  
// 从列表中移除指定 collection 中包含的所有元素  
list.removeAll(new ArrayList());  
// 用指定元素替换列表中指定位置的元素  
list.set(0,"咩");  

Traversal:

List list = new ArrayList();
list.add("咩");
list.add("喵");
//对列表进行遍历操作
Iterator<String> item = list.iterator();  
while (item.hasNext()) {  
    String str = item.next();  
    System.out.println(str);  
} 

2. LinkedList LinkedList stores data in a linked list structure, which is suitable for dynamic insertion and deletion of data elements. The speed of random access and traversal is relatively slow. It provides methods not defined in the list interface, which are specially used to operate header and footer elements. They can be used as stacks, queues and bidirectional queues.

LinkedList operation

List linkList = new LinkedList();
list.add("咩");
list.add("喵");
//对列表进行遍历操作
ListIterator linkItem = linkList.listIterator();  
// 判断迭代器中是否有下一个元素  
while (linkItem.hasNext()) {  
    String str = linkItem.next();  
    System.out.println(str);
}  

3. Like ArrayList, vector vector is also implemented through array. The difference is that it supports thread synchronization, that is, only one thread can write vector at a certain time, so as to avoid inconsistency caused by multiple threads writing at the same time.

When there is not enough memory, the vector is expanded by one time by default, and the ArrayList is expanded by 50% + 1 by default. Vector provides indexof (obj, start) interface, but ArrayList does not. Vector is thread safe, but it is not used in most cases because thread safety requires greater system overhead.

Set

Set is a collection that does not allow duplicate elements.

The implementation classes of set include hastset and TreeSet. HashSet depends on HashMap. In fact, it is implemented through HashMap and is disordered; TreeSet depends on treemap. In fact, it is implemented through treemap and is orderly.

Features: the elements in the set are not sorted in a specific way, but simply add objects to the set. The access and operation of members in the set are carried out through the reference of objects in the set, so there can be no duplicate objects in the set.

1. All elements in the hastset must be unique. As its subclass, HashSet also needs to ensure the uniqueness of elements.

Element uniqueness is determined by the hashcode and equals methods of the element: 1) if the hashcode value of the object is different, the object will be stored directly in the collection without calling the equals method; 2) If the hashcode value of the object is the same, you need to call the equals method to determine whether the return value is true. If true, it is regarded as the same element and will not be stored. If false, it is regarded as a different element and will be stored directly.

If you want to use a HashSet to store an element, the class of the element must override the hashcode method and the equals method.

HashSet main methods

add(Object) //添加元素
addAll(Collection) //添加collection中的所有元素
remove(object) //移除元素
removeAll(Collection) //移除collection中的所有元素
size() //返回所有元素的个数
iterator() //遍历
toArray() //转换为数组
clear() //清空
isEmpty() //判断是否为空
contain(object) //判断是否包含元素
containAll(Collection) //判断是否包含collection中的所有元素

2. TreeSet TreeSet is an ordered set. You can insert elements into the set in any order. When iterating over the set, each value will automatically appear in the sorted order. The elements in TreeSet are arranged in ascending order. By default, they are sorted in natural order, which means that the elements in TreeSet should implement the comparable interface or have a custom comparator.

The difference between TreeSet and HashSet is that TreeSet will automatically sort the elements according to the natural sorting method, that is, 1 is in front of 2 and a is in front of B. However, HashSet automatically sorts the elements according to the hashcode of the elements If we do not need to use the sorting function, we should use HashSet because its performance is better than TreeSet.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>