Collection
Collection is the largest interface for collections, but the purpose of using Collection directly is not clear. Therefore, Collection is not used directly in development. Generally use its subclass list set
List
Common subclasses of List are ArrayList and LinkedList
ArrayList
①Iterative method, only Iterator and foreach can be used
②ArrayList is implemented in the form of an array. The characteristic of an array is that an index can be used to quickly locate objects. Therefore, for the requirement to obtain objects quickly, the implementation of ArrayList is more efficient. But delete and insert operations are inefficient
③Non-thread safe
LinkedLIst
①Iterative method, only Iterator and foreach can be used
②LinkedList uses a double-linked list to implement the List interface, so it is more efficient in insertion and deletion operations. Suitable for implementing stack (stack, first-in, last-out) and queue (queue, first-in, first-out)
set
A set cannot add duplicate elements and has no order.
HashSet: The output is also unordered. If you add a custom class, you need to write hashcode() and equals() methods to determine whether the object is duplicated. If it is not rewritten, the address of each instance of the custom class is different, so it will be judged as a different element.
TreeSet: The default is natural sorting or the Comparable interface overrides the compareTo method for sorting
map
map is stored in the form of key-value pairs
HashMap is stored out of order, and the key is not allowed to be repeated. Overriding the hashcode() method and the equals() method can not be repeated.
The Map collection that TreeMap can sort, according to the key in the collection, the key is not allowed to be repeated, the hashcode() method and the equals() method need to be rewritten to use the custom class as the key, and the compareTo() method is rewritten for sorting