Java Collections

 Java Array List

A java array list is a kind of data structure where each value is stored using an index number instead of a sequential numerical order. In terms of performance, a java array list is faster than a linked list or a vector since they require linear search time and copying elements to make changes.


Linked List

The concept behind a linked list is similar to a database table. An ordered collection of items, each item contains an identifier (index) and the actual data (value). Items are accessed by their identifiers and not their position in the list. To add/remove an item, the first step is to locate the previous node, then follow its pointer to the next node.



A vector is a dynamic array-like container class that provides fast access to any element in the array. Unlike arrays, vectors are resizable and reorderable. A vector is declared using the keyword "Vector". Elements of the array are dynamically allocated memory and deallocated when no longer referenced.



HashMap is a binary search tree data structure. Each node contains two fields - key (of type Object) and value (of type T). A HashMap cannot have duplicate keys; each entry's key and its corresponding value together make up an unique combination.

The most commonly used method of adding elements to a HashMap is put(Object key,T value), which adds a mapping between the given key and value. The following example displays how to add a String value to an existing HashMap.

//Create a new HashMap object to store strings

myHash.put("a", "string 1");

myHash.put ("b", "string 2");

System.out.println("My Hash Map: " + myHash); //prints My Hash Map: {astring 1, bstring 2}

//Add string "c" to the HashMap

myHash.put( "c", "new string");

System.outprintln("My Hash Map after addition of c: "+ myHash); //prints out My Hash Map after addition of 5: {astring 11, bstring 22, cnew string}


Hash Table

Hash tables provide a way to store data efficiently while avoiding duplicate storage space. Hash tables use a hashing method to index elements in memory more efficiently than traditional arrays.

A Java hash table is a type of hash map data structure. A hash table is similar to a dictionary and uses hashing algorithms to store its elements. The name comes from the fact that you use the bucket number modulo the size of the table to locate the element.


Java Tree Map

JavaTreeMap is a collection of sorted data structures that implement NavigableMap and NavigableSet interfaces. These interfaces allow users to perform operations including size, first entry, last entry, predecessor search, and submap traversal.


Tree Set

A TreeSet object is ordered according to the natural ordering of its elements; each element's natural ordering determines where it should be inserted into the set, if duplicates are allowed. A TreeSet provides constant time performance for the basic operations (add, remove, contains), logarithmic time performance for searching, and linear time behavior except for detecting duplicates, which takes time proportional to the size of the tree.


Tree Map

A TreeMap is ordered associative array in which keys have no ordering. All values in the map are associated with their respective keys via their respective hash codes. Values in the map may not necessarily be comparable to those in a standard map since they do not inherit the Comparable interface. The iterators returned by the iterator methods and spliterator return nodes rather than entries.

Java Iterator is a class in Java programming language that implements the Iterable interface and provides the functionality of iteration over its elements (objects). Java iterator does not have a direct relationship with the underlying data structure; rather, it abstracts the collection's iteration logic. An object of java iterator class represents each item of a collection that supports iteration. Java Iterator class provides methods like hasNext(), remove() and current().


In general java iterators are used in collections classes of java. In Collections framework, java iterators is used to provide a simple syntax for accessing elements in various types of collection classes.


Iterable (or Iterator) is a Java programming language concept where collections of elements can be accessed and manipulated using foreach loops and similar constructs. Iterators were originally introduced in Java 1.0 in order to provide a generic solution for dealing with heterogeneous lists. Since then, they have gained popularity due to their simplicity and ease-of-use.


In Java 8, the Stream API was added and provided a way to do this without having to create your own class.

Hash maps (or Hash tables) are a data structure similar to an associative array. Like arrays, they have a fixed size known at compile-time, and each entry may store different types of data. Unlike arrays, however, their entries do not need to be indexed sequentially; their keys are used instead. The Java Collections Framework provides three implementations of hash table: LinkedHashMap, TreeMap and Hashtable. These classes are thread-safe, meaning that concurrent users can share them safely without worrying about inconsistent state.


The java.util.LinkedHashMap class uses doubly-linked list nodes that implement the Map interface, and thus the operations provided by Map are automatically implemented. In addition, these lists are maintained using volatile variables, which makes sure that threads always see consistent results.


A map's internal representation is an array of buckets where each bucket stores a group of key/value pairs called a node. A LinkedHashMap maintains a double hashing algorithm to handle collisions. To insert a value into a LinkedHashMap, iterate through its buckets until a free slot is found. Then, add the key to the bucket and return the previous location of the key. If no suitable location was found, create a new bucket before returning NULL. To retrieve a specific key, search through the buckets and find a free slot containing the key. Return NULL if there is none.


The Map interface extends AbstractCollection, Iterable and List interfaces. Thus, it supports the Collection methods, including remove(), retainAll() and contains(Object). The iterator returns keys rather than values.

It does NOT, however, support the add method.