Overview of java commonly used collections

1. Collection
1. Two kinds of containers
A. Array (storage of arbitrary types, fixed length)
B. Collection (storage of object types, variable length)
2. Architecture
Single-column collection Collection
List (ArrayList, LinkedList, Vector)
Set (HashSet) , LinkedHashSet, TreeSet)
3. Common methods
A. Add
public boolean add(E)
B. Delete
public void clear()
public boolean remove(E)
C. Change
public Object[] toArray()
public T[] toArray()
D .Check
public int size()
public boolean isEmpty()

2. Iterator
1. Obtain the iterator object
Iterator iter = c.iterator(); // Obtain the iterator
2. Use the iterator method
iter.hasNext(); // Whether there is the next element
E e = iter.next (); //Get the next element
3. Template case
//Create object
Collection c = new ArrayList();
//Add data
c.add("Hello");
c.add("world");
c.add ("Good");
//Get the iterator
Iterator iter = c.iterator();
//Loop through the iterator itit
while (iter.hasNext()) { String next = iter.next(); System.out.println (next); } 4. Enhance the use of for loop (shortcut collection name.for) //Create object Collection c = new ArrayList(); //Add data c.add("Hello"); c.add(" world"); c.add("good"); //Enhance for











for (String s: c) { System.out.println(s); } 5. Notes for iterators: Modification operations cannot be performed in the process of traversing the collection, otherwise a concurrent modification exception will occur. Concurrent ModificationException : ConcurrentModificationException




3. List collection
1. List collection features
A, order (the order of storage and the order of retrieval are the same)
B, there is duplication (the stored data can be repeated)
C, there is index (the stored elements have indexes, you can Operate elements by index)
2. The unique function of List collection (index related)
A, add
void add(int index, E e) //add element to the specified position
B, delete
E remove(int index) //delete the specified position Element, and return the element to
C, change
E set(int index, E e) //Modify the element at the specified position, and return the original element to
D, check
E get(int index) //Get the element at the specified index position
3 , Common data structure
A, stack (first in last out)
B, queue (first in first out)
C, array (fast search, slow addition and deletion)
D, linked list (slow search, fast addition and deletion)
E, red-black tree (binary tree, promotion Search efficiency)
4. About LinkedList (mainly the method of operating the head and tail)
A. Add
public void addFirst(E e) //Add the specified element to the beginning of the list
public void addLast(E e) //Add the specified element To the end of the list
B, delete
public E removeFirst() //Remove and return the first element of this list
public E removeLast() //Remove and return the last element of this list
C, check
public E getFirst() //Return the first element of this list
public E getLast() //Return the last element of this list
D , Other
public E pop() //Pop an element from the stack represented by this list
public void push(E e) //Push the element into the stack represented by this list
public boolean isEmpty() //If the list does not contain any Element, return true

Four, Set collection
1, Set collection characteristics
A, disorder (the order of storage and retrieval may not be the same)
B, no duplication (stored data can be no duplication, can be used to de-duplicate)
C, no index (Without index, element value cannot be obtained by index)
2. Set collection system
A, subclass HashSet disorderly, no repetition
B, subclass LinkedHashSet ordered, no repetition
3, variable parameter
A, basic format of variable parameter:
a. Define format
modifier return value type method name (data type... parameter name) { method body; return return value; } b. use format data type return value = method name (parameter 1, parameter 2, parameter 3, parameter 4 ); B. Variable parameters Note: Variable parameters can only be used at the end of the parameter list





5. Collections
1. Commonly used methods
public static boolean addAll(Collection c,T... elements) //Add elements to the collection
public static void shuffle (List<?> list) //Random replacement disturbs the order of the collection
public static void sort(List list) //Sort the elements in the collection according to the default rules
public static void sort(List list,Comparator<? super T> comparator) //Sort the collection elements according to the specified rules
2. Two types of comparators
A, internal Comparable uses the implementation interface to implement [fixed] the
current class to implement the Comparable interface, adds unimplemented method
B, the outer comparator uses an anonymous inner class to implement [flexible]
Collections.sort(list, new Comparator(){… });
C, ascending and descending order operations
Ascending order: currently in front ascending (current-incoming) o1.compareTo(o2)
Descending order: currently in last descending (incoming-current) o2.compareTo(o1)

Six, Map collection
1, the role of Map collection to
deal with one-to-one correspondence relationship collection
Collection is a single-column collection
Map is a two-column collection
2, Map collection common methods
A. Add and modify
public V put (K key, V value) // specify Add the key and value to the Map collection
B. Delete
public V remove(K key) // Delete the value of the corresponding key of the specified key, and return the deleted value
C. Check
public int size() // Return the map collection the size of the data
public V get (K key) // key obtaining a value corresponding to the specified key
public boolean containsKey (K) // contains the specified key contains the return key to true
public Boolean containsValue (V) contains the specified // The value value contains return true
D. Traverse
public Set keySet() //Get all the keys in the Map collection and store them in the Set collection
public Set<Map.Entry<K,V>> entrySet() //Get into the Map collection The collection of all key-value pair objects
3. The traversal method of the Map collection
A. Find the value through the key
Set set = map.keySet();
for (String s: set) {
System.out.println(s);
}
B.Entry key-value pair object (marriage certificate object)
Set<Map.Entry<String, String>> entry = map.entrySet();

Guess you like

Origin blog.csdn.net/chenailun1990/article/details/115290404