Discuss the Java collection framework in detail

  • 2020-04-01 03:51:26
  • OfStack

1. Why use a collection framework

When we don't know how many objects the program will need to run, or when we need a more complex way to store objects, we can use the Java collections framework

2. What the Java collections framework contains

Interface :(parent) Collection interface contains List (subclass) interface and Set (subclass)
The interface List interface contains (ArrayList collection implementation class and LinkedList collection implementation class)
The Set interface also contains (HashSet Set Set implementation class and TreeSet Set implementation class)
Interface :(parent) Map interface contains (HashMap collection implementation class and TreeMap collection implementation class)
The *Collections interface provides a variety of algorithm implementations for sorting Collections, traversing Collections, and more

3. Features of Collection, List and Set:

The Collection interface stores a non-unique, unordered set of objects
The List interface stores a set of non-unique, ordered (insert order) objects
The Set interface stores a unique, unordered Set of objects called the Map interface stores a Set of key-value objects, providing a key-to-value mapping

4. Advantages of ArrayList collection and LinkedList collection

1. ArrayList implements arrays of variable length and allocates continuous space in memory. Traversing and accessing elements at random are more efficient
2. LinkedList is stored by LinkedList. It is efficient to insert and delete elements

The List interface provides the corresponding methods remove() and contains(), which can be used directly

List interface common methods:    

      Boolean add (Object o)                 Add elements to the end of the list in order, starting at 0
    Void add(int index,Object o) adds an element at the specified index position. The index position must be between 0 and the number of elements in the list
    Int size ()                                 Returns the number of elements in the list
    The Object get (int index)                 Returns the element at the specified index location. The element is of type Object and needs to be cast before use
    Boolean contains(Object o) determines whether the specified element exists in the list
    Boolean remove(Object o) removes an element from the list
      Object remove(int index) removes the specified position element from the list, starting at 0
      A special method of LinkedList
      Void addFirst(Object o) adds an element to the top of the list
      Void addLast(Object o) adds an element to the end of the list
      The Object getFirst ()                 Returns the first element in the list
      The Object getLast ()                 Returns the last element in the list
      The Object removeFirst ()                 Deletes and returns the first element in the list
      The Object removeLast ()                 Deletes and returns the last element in the list

Common methods of Map interface:

Object put(Object key, Object val) is stored as a key-value pair
Object get (Object key) returns the associated value based on the key, or null if the specified key does not exist
Object remove (Object key) removes the key-value pair mapped by the specified key
Int size() returns the number of elements
Set keySet () returns the collection of keys
Collection values () returns a Collection of values
Boolean containsKey (Object key) returns true if there is a key-value pair mapped by the specified key


Related articles: