"Collection" in Java

"Collection" is a data structure, a collection of some objects. Data (objects in a set) can be numbers, strings, objects of custom classes, etc.

Main JCF interfaces and their implementations

At the top of the hierarchy in the Java Collection Framework are 2 interfaces: Collection and Map. These interfaces divide all collections included in the framework into two parts according to the type of data storage: simple sequential sets of elements and sets of key-value pairs, respectively.

Collection interface is extended by interfaces:

  • List is a collection in which duplicate values are allowed. The elements of such a collection are numbered starting from zero and can be accessed by index. Implementations:
    • ArrayList - encapsulates a regular array, the length of which is automatically increased when new elements are added.
    • LinkedList (bi-directional linked list) - consists of nodes, each of which contains both the actual data and two links to the next and previous node.
    • Vector is an implementation of a dynamic array of objects whose methods are synchronized.
    • Stack is an implementation of a LIFO (last-in-first-out) stack.
  • Set describes an unordered collection that does not contain duplicate elements. Implementations:
    • HashSet - uses a HashMap to store data. The added element is used as a key and value. Due to implementation specifics, the order of elements is not guaranteed when added.
    • LinkedHashSet - ensures that the order of elements when traversing the collection is identical to the order in which elements are added.
    • TreeSet - provides the ability to control the order of items in a collection using a Comparator object, or stores items using "natural ordering".
  • Queue is intended to store elements with a predefined way of inserting and retrieving FIFO (first-in-first-out):
    • PriorityQueue - provides the ability to control the order of items in a collection using a Comparator object, or stores items using "natural ordering".
    • ArrayDeque - implementation of the Deque interface that extends the Queue interface with methods that allow you to implement a LIFO (last-in-first-out) construction.

Map interface is implemented by classes:

  • Hashtable is a hash table whose methods are synchronized. Does not allow null as a value or key and is not ordered.
  • HashMap is a hash table. Allows null as a value or key, and is not ordered.
  • LinkedHashMap is an ordered implementation of a hash table.
  • TreeMap is an implementation based on red-black trees. Is ordered and provides the ability to control the order of elements in a collection using a Comparator object, or stores elements using "natural ordering".
  • WeakHashMap is a hash table implementation that is organized using weak references for keys (the garbage collector will automatically remove an item from the collection at the next garbage collection if there are no hard references to the key of this item).

Hierarchy of interfaces: List, Set, Map, SortedSet, SortedMap, Collection, Iterable, Iterator, NavigableSet, NavigableMap.

  • Iterable
    • Collection
      • List
      • Set
        • SortedSet
          • NavigableSet
  • Map
    • SortedMap
      • NavigableMap
  • Iterator

Why is Map not Collection while List and Set are Collection

Collection is a collection of some elements. Map is a collection of key-value pairs.

Difference between the java.util.Collection and java.util.Collections classes

java.util.Collections is a set of static methods for working with collections.

java.util.Collection is one of the main interfaces of the Java Collections Framework.


Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed