Yige

Yige

Build

Java Collection Summary

Summary of Common Knowledge Points about Java Collections#

  1. Differences between ArrayList and Vector
  2. Differences among List, Set, and Map
  3. Elements in a Set cannot be duplicated; what method is used to distinguish duplicates
  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. Differences 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. A 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. Elements in a Set cannot be duplicated; what method is used to distinguish duplicates#

It checks whether the key exists, using both == and equals() methods. If the added elements are the same, it does not insert but directly modifies the value.

Differences between == and equals#

  • == checks whether two variables or instances point to the same memory space, while equals checks whether 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 the element object through its index (corresponding to the get(int index) method).
  • Memory space usage: The space wastage of ArrayList mainly manifests in reserving a certain capacity at the end of the list, while LinkedList's space consumption is due to each of its elements requiring 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 old 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 in a List collection.
  • ListIterator allows for bidirectional traversal, adding elements, and setting elements.

8. Java Collections' fail-fast mechanism#

The fail-fast mechanism is an error mechanism present in Java containers (both Collection and Map have fail-fast mechanisms). When traversing a container object, if the container structure is modified, it is 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 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 internally maintains a modCount variable. When traversing collection nodes, it checks whether modCount and expectedModCount are equal. If they are not equal, 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.