Collections in Java – Day 16

Published by Maneet Srivastav on

Collections in Java

The collection represents a group of objects. Depending on the method of storing and retrieving. Collections are basically divided into three parts –

  • Set
  • Map
  • List.

Where Set does not contain duplicate values, Map contains the key-value type of data whereas List can have a duplicate value stored in it sequentially. This framework is provided in “java.util” package. The collection is the parent interface of all collections in java.
Following list describes the core collection interfaces.

 

Collection

The root of the collection hierarchy. A collection represents a group of objects known as its elements. Some types of collections allow duplicate elements, and others do not. Some are ordered and others are unordered. The Java platform doesn’t provide any direct implementations of this interface but provides implementations of more specific subinterfaces, such as Set and List.

  • Set: A collection that cannot contain duplicate elements.
    • Hash Set: This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.
    • Linked Hash Set: Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation).
    • Interface Sorted Set: A set that further guarantees that its iterator will traverse the set in ascending element order sorted according to the natural ordering of its elements (see Comparable), or by a Comparator provided at sorted set creation time. Several additional operations are provided to take advantage of the ordering. (This interface is the set analogue of SortedMap).
    • Tree Set: This class implements the Set interface, backed by a TreeMap instance. This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used.
  • List: An ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position).
    • Array List: Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.
    • Vector: This class is roughly equivalent to ArrayList except it is Synchronized.
    • Linked List: Linked list implementation of the List and Queue interfaces. Implements all optional operations, and permits all elements (including null).
  • Queue: A collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations.
    Queues typically, but do not necessarily, order elements in a FIFO (first-in, first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator or the elements’ natural ordering.

Map

An object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value. If you’ve used Hashtable, you’re already familiar with the basics of Map.

  • Hash Map: The HashMap class is roughly equivalent toHashtable, except that it is unsynchronized and permits nulls.

    • Hash Table: Hash table based implementation of the Map interface. This implementation provides all of the optional map operations and permits null values and the null key. This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
  • Linked Hash Map: Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from Hash Map in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map.
  • TreeMap: This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key’s class (see comparable), or by the comparator provided at creation time, depending on which constructor is used.

CONNECT WITH THE AUTHOR ON LINKEDIN.

YOU CAN VISIT OUR WEBSITE BRIGHTERBEES FOR INFORMATION ABOUT JAVA.
TO GET ON THE BLOG OF DAY 7 OF THIS JAVA SERIES click here.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY CONNECT WITH US