preface

  • inJavaIn, the use of collections is the most important
  • This article will present a comprehensive & detailedJavaCollective learning guide, I hope you will like it.

directory


1. Introduction


2. Differences with arrays

Java collections differ from common arrays as follows:


3. Set types

  • Java collections can be classified into Set, List, Map, and Queue

  • The details are as follows

This will be explained in more detail below


4. Set inheritance relationship

  • JavaThe root interface of the collection =CollectionInterface +Mapinterface

That is, Java collection classes all implement the above two interfaces

Among them:

  • List,Set,QueueTo achieve theCollectionInterface. The inheritance relationship is as follows:

  • MapTo achieve theMapInterface. The inheritance relationship is as follows:

Next, I’ll focus on the Collection interface, the Map interface, and their concrete Collection implementation classes


5. The Collection related

In this section, we introduce the Collection interface first and then the concrete Collection implementation classes (List, Set, Queue).

5.1 the Collection interface

  • Define an interface that is the top-level parent of the collection List, Set, Queue

  • Package structure

  • The methods defined in the interface are used to add elements, delete elements, and so on
Boolean add (Object o); // Add a reference to an Object to the collection. // Remove a reference to an object from the collection Void clear(); Boolean contains(Object o); // Delete all objects in the collection, i.e. no longer holds references to these objects. Object[] toArray(); Iterator Iterator (); Iterator (); Iterator (); Boolean isEmpty(); // Return 1 Iterator: Iterator for iterating over the elements in the collection Boolean isEmpty(); Int size(); // Return the number of elements in the collection Boolean equals(Object o); Int hashCode(); // Returns the hash codeCopy the code
  • withCollectionsThe difference between

Appendix: The use of Collections

// The main functions are: search elements, get Max and min values, sort sets, object thread safety, copy all elements from 1 List to another List, etc. Static T Max (Collection<? extends T> coll, Comparator<? super T> comp); static T min(Collection<? extends T> coll, Comparator<? super T> comp); Static void sort(List<T> List, Comparator<? super T> c); SynchronizedCollection (Collection<T> C); synchronizedCollection(Collection<T> C); static List<T> synchronizedList(List<T> list); static Map<K,V> synchronizedMap(Map<K,V> m); static Set<T> synchronizedSet(Set<T> s); Static void swap(List<? > list, int I, int j) // ArrayList = new ArrayList(); list.add(XXX); / /... the list. The add (XXX); Collectoions.sort(list);Copy the code
  • CollectionsArraysThe difference between

Next, I’ll introduce the concrete collection implementation classes: List, Set, Queue

5.2 the List collection

  • Introduction to the

  • Method of use
Void add(int index, Object element); // Insert element elment at index of Collection list Boolean addAll(Collection<? extends E> c); E set(int index, E element); // Replace the element at index with element Object get(int index); Int indexOf(Object o); Int lastIndexOf(Object O); SubList (int fromIndex,int toIndex); subList(int fromIndex,int toIndex); Object remove(int index); // Return fromIndex to toIndex; Void clear(); void clear(); // Empty the set int size(); Boolean isEmpty(); // Check whether the set is emptyCopy the code
  • Collection implementation class

    ListThe collection implementation classes mainly include:Vector,ArrayList,LinkedList,Stack. The details are as follows

  • Vector,ArrayList,LinkedListThe difference between

5.3 Set collection

  • Introduction to the

  • Method of use
boolean add(E e); Boolean remove(Object o); Boolean addAll(Collection<? extends E> c); Boolean removeAll(Collection<? > c); Boolean contains(Object o); Boolean containsAll(Collection<? > c); Object[] toArray(); Void clear(); // Return an array containing all elements of the collection. // Empty the set int size(); Boolean isEmpty(); // Check whether the set is emptyCopy the code
  • Collection implementation class

    SetThe collection implementation classes mainly include:TreeSet,HashSet(LinkedHashSet),EnumSet. The details are as follows

5.4 set the Queue

  • Introduction to the

  • Method of use
// Insert a new element into the queue: true if the insert succeeds; If the queue is full, throw IllegalStateException Boolean add(E E); // Insert a new element into the queue, return true if the insert succeeds; If the queue is full, return false, but do not raise Boolean offer(E E); E remove(); // return the first element & removed from the queue; If the queue is empty, an exception E poll() is thrown; // return the first element & removed from the queue; If the queue is empty, null is returned, but E Element () is not raised; // The first element returned & is not removed from the queue; If the queue is empty, throw an exception E peek(); // return the first element & not removed from the queue; If the queue is empty, return NULL // time complexity analysis // 1. Offer (), poll(), remove(), add ()= O(log(n)) // 2. Remove method: contains() = O(n) // 3. Peek (), Element (), size() = O(1) Traversal, if using but not removing elements, use Element (), peek() (time efficient) // 2. Add elements with offer (), poll () // a. Avoid using Collection add(), remove() // b. Cause = Success can be determined by the return value, but add() and remove() will throw an exception if they failCopy the code
  • Collection implementation class

    QueueThe collection implementation classes mainly include:PriorityQueue,Dueue(ArrayDeque,LinkedList),PriorityQueue(ArrayBlockingQueue,LinkedBlockingQueue). The details are as follows

This concludes the presentation of the Collection interface and its concrete Collection implementation classes (List, Set, Queue). Summarized below


6. The Map

In this section, the Map interface is introduced first, followed by the most common HashMap, LinkedHashMap, and TreeMap in its concrete collection implementation classes

6.1 the Map interface

  • Introduction to the

Note: Map interface has nothing to do with Collection interface

  • Package structure

// In order to better understand the various kinds of relationships, the following is attached: Public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable Serializable{} // LinkedHashMap public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>{} // TreeMap public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, java.io.Serializable{} // Hashtable public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, java.io.Serializable {} // ConcurrentHashMap public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K, V>, Serializable {}Copy the code
  • Methods defined in the interface
Set<K> keySet(); Set Collection<V> values(); // Generate a Collection V remove(Object key) for all values; V get(Object key); // Delete the mapping. V put(K key, V value); Void putAll(Map<? extends K, ? extends V> m); Boolean containsKey(Object key); Return true Boolean containsValue(Object value); Void clear(); // Return true void clear(); // Clear all mappings int size(); // Return the number of key-value relationships Boolean isEmpty(); // Map also contains one inner class: Entry // This class encapsulates a key-value pair. // The Entry class contains the following methods: Boolean equals (Object O); // compare the equality of the specified object with this item K getKey (); // return the corresponding key V getValue (); // return the corresponding value int hashCode (); // return the hash value of this mapping item V setValue (V value); // Replace the value corresponding to this item with the specified valueCopy the code
  • withSetThe collection,ListRelation of sets

Next, I’ll introduce the most common HashMap, LinkedHashMap, and TreeMap in their concrete collection implementation classes

6.2 a HashMap

  • The class definition
public class HashMap<K,V>
         extends AbstractMap<K,V> 
         implements Map<K,V>, Cloneable, Serializable
Copy the code
  • This paper mainly introduces

For a more detailed introduction to source code analysis, see the article:

  • Java: Take you through HashMap 1.7 source code analysis hand in hand
  • Java source code analysis: A major update to HashMap 1.8

Note: The implementation of HashMap is quite different in JDK 1.7 and JDK 1.8

6.3 LinkedHashMap

  • The class definition
public class LinkedHashMap<K,V> 
    extends HashMap<K,V> 
    implements Map<K,V>{}
Copy the code
  • Introduction to the

  • For more details on LinkedHashMap & source code analysis, see the article:
  • Java: Take you through HashMap 1.7 source code analysis hand in hand
  • Java source code analysis: A major update to HashMap 1.8

6.4 TreeMap

  • The class definition
public class TreeMap<K,V> 
    extends AbstractMap<K,V> 
    implements NavigableMap<K,V>, Cloneable, java.io.Serializable{} 
Copy the code
  • Introduction to the

  • On sorting methods & criteria for judging equality

6.5 Comparison of related subclasses

  • HashMapLinkedHashMap,TreeMapThe difference between

  • Difference between HashMap and Hashtable

  • HashMapConcurrentHashMapThe difference between

This concludes the tutorial on Java collections (Colletcion, Map interfaces, and their related implementation subclasses).


7. To summarize

  • This article mainly explainsJavaThe related content of the collection, includingColletcionInterfaces,MapInterface & its related implementation subclasses), summarized as follows:

  • So I’m going to continue with thisAndroid & JavaIn the knowledge of in-depth explanation, interested can continue to pay attention toCarson_Ho android Development Notes

Please give me a thumbs up! Because your encouragement is the biggest power that I write!