Yige

Yige

Build

Summary of Java Collections

Summary of Common Knowledge Points in Java Collections#

  1. Difference between ArrayList and Vector
  2. Differences among List, Set, and Map
  3. How to distinguish duplicates in a Set where elements cannot be repeated
  4. Differences between ArrayList and LinkedList
  5. Differences between Collection and Collections
  6. Differences between Enumeration and Iterator interfaces
  7. Characteristics of ListIterator
  8. Java Collections' fail-fast mechanism
  9. Differences between comparable and Comparator

1. Difference between ArrayList and Vector#

  • ArrayList is non-synchronized, while Vector is synchronized.
  • Different growth sizes: Vector grows by doubling its size, while ArrayList grows by 0.5 times its original size.

2. Differences among List, Set, and Map#

  • List: The List interface stores a group of non-unique (can have multiple elements referencing the same object), ordered objects.
  • Set: A collection that does not allow duplicates. There will not be multiple elements referencing the same object.
  • Map: Stores using key-value pairs. Map maintains values associated with keys. Two keys can reference the same object, but keys cannot be duplicated; a typical key is of String type but can be any object.

3. How to distinguish duplicates in a Set where elements cannot be repeated#

Check if the key exists, using both == and equals() methods. If the added elements are the same, it will not be inserted but will directly modify the value.

Difference between == and equals#

  • == checks if two variables or instances point to the same memory space, while equals checks if the values of the memory spaces pointed to by the two variables or instances are the same.
  • == compares memory addresses, while equals() compares the content of strings.
  • == checks if references are the same, while equals() checks if values are the same.

How does HashSet ensure data uniqueness?#

When you add an object to a HashSet, it first calculates the object's hashcode to determine if there are any objects with the same hashcode. If it finds an object with the same hashcode, it calls the equals() method to check if the objects are truly the same.

4. Differences between ArrayList and LinkedList#

  • ArrayList uses an Object array at its core; LinkedList uses a doubly linked list data structure (before JDK1.6 it was a circular linked list, which was removed in JDK1.7).
  • LinkedList does not support efficient random element access, while ArrayList does; fast random access is achieved by quickly retrieving element objects by their index (corresponding to the get(int index) method).
  • Memory space usage: The space wastage of ArrayList mainly occurs at the end of the list where a certain capacity is reserved, while LinkedList's space consumption is due to each of its elements needing more space than ArrayList (to store direct successors, predecessors, and data).

5. Differences between Collection and Collections#

  • Collection is the parent interface of collections, with Set and List interfaces inheriting from it.
  • Collections is a utility class for collections, providing a series of static methods for searching, finding, synchronizing, and other operations on collections.

6. Differences between Enumeration and Iterator interfaces#

Iterator replaced Enumeration, which is an older iterator.

  • Iterator has a fail-fast mechanism, making it safer than Enumeration.
  • Iterator can remove elements, while Enumeration cannot.

7. Characteristics of ListIterator#

  • ListIterator inherits from the Iterator interface and is used to traverse elements of a List collection.
  • ListIterator allows bidirectional traversal, adding elements, and setting elements.

8. Java Collections' fail-fast mechanism#

The fail-fast mechanism is an error mechanism in Java containers (both Collection and Map have fail-fast mechanisms). When traversing a container object, if the container structure is modified, it is very likely to throw a ConcurrentModificationException, resulting in a fail-fast.

When does fail-fast occur?#

  • Single-threaded environment: When the structure of a collection is modified during its traversal. Note that the listIterator.remove() method does not throw this exception when modifying the collection structure.
  • Multi-threaded environment: When one thread traverses the collection while another thread modifies the collection structure.

Implementation principle#

The collection maintains a modCount variable internally. When traversing collection nodes, it checks if modCount and expectedModCount are equal; if not, it indicates that another thread has modified the collection structure.

9. Differences between comparable and Comparator#

  • The comparable interface actually comes from the java.lang package and has a compareTo(Object obj) method for sorting.
  • The comparator interface actually comes from the java.util package and has a compare(Object obj1, Object obj2) method for sorting.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.