Java collections are Java built-in library provided by JDK. You don’t have to implement your own data structures classes and methods. You can directly call the library. Before we list the Java Collections, the original data structures are introduced. There are arrays, linked lists, stacks, binary trees, hash tables, graphs among others. Some may not be in the library (eg trees and graphs). If you know the differences, you can choose when to use library and when to implement your own.
What are most used data structures in Java?
ArrayList and HashMap. Next are HastSet and PriorityQueue.
With data structure, we find ways to make data access more efficient. When dealing with data structure, we not only focus on one piece of data, but rather different sets of data and how they can relate to one another in an organized manner.
Array: An array is an index-based data structure, which means every element is referred by an index. An array holds same data type elements.
Linked list: A linked list is a sequence of nodes in which each node is connected to the node following it. This forms a chain-link of data storage. It consists of data elements and a reference to the next record.
Binary search tree: A binary search tree (BST) is a binary tree. The left subtree contains nodes whose keys are less than the node’s key value, while the right subtree contains nodes whose keys are greater than or equal to the node’s key value. Moreover, both subtrees are also binary search trees. A binary search tree can retrieve data efficiently.
Hash table: Hash table is a data structure that can map keys to values. A hash table uses a hash function to compute a key into an integer (hash value), which indicates the index of the butckets (aka array). From the key, the correct value can be stored and found. Hash table is one of the most used data structures.
Max-Heap: A heap is a tree-based data structure in which all the nodes of the tree are in a specific order. Max-heap is a binary tree. It is complete. The data item stored in each node is greater than or equal to the data items stored in its children.
Trie: A Trie is a tree. In a trie, every node (except the root node) stores one character or a digit. By traversing the trie down from the root node to a particular node n, a common prefix of characters or digits can be formed which is shared by other branches of the trie as well.
Suffix trie: Suffix trie is a trie containing all the suffixes of the given text. Suffix trie allows particularly fast implementations of many important string operations.
2. Java Collections
Java collection framework is the set of collection types that are included as part of core java. It provides the APIs or methods that you can use directly to operate on the data structures such as Arrays, Linked Lists, stacks, queues, sets and maps. If you master java collection, it will save you tons of time and helps to solve complex problems.
ArrayList:ArrayList class is a resizable-array implementation of the List interface. It implements all optional list operations and permits all elements.
Vector:Vector is very similar to ArrayList but Vector is synchronised and slow. It is a legacy class. ArrayList is preferred.
String: String class is used to create and manipulate strings.
LinkedList: LinkedList class is a doubly-linked list implementation of the List and Deque interface. A LinkedList stores its data as a list of elements and every element is linked to its previous and next element.
HashMap: A HashMap is a collection class implements Map interface. It requires a hash function and uses hashCode() and equals() methods, in order to put and retrieve elements to and from the collection respectively.
Hashtable: Hashtable class is similar to HashMap. It implements Dictionary. Hashtable provides an Enumeration of its keys. It doesn’t allow null as key or value. Please note that since HashMap was created later, it is an advanced version and improvement on the Hashtable. Hashtable is synchronized and slower. HashMap is preferred over Hashtable.
TreeMap:TreeMap implements SortedMap interface. It is sorted in the ascending order of its keys. The complexity of the operations is O(logn).
LinkedHashMap: LinkedHashMap keeps the inserting order. The complexity is the same as HashMap O(1).
HashSet: HashSet class implements Set interface. Duplicate values are not allowed. Its elements are not ordered. NULL elements are allowed in HashSet.
TreeSet: TreeSet is implemented using a tree structure. The elements in a TreeSet are sorted. The complexity of operations is O(logn).
LinkedHashSet: LinkedHashSet maintains the insertion order. Elements get sorted in the same sequence in which they have been added to the Set. The complexity is the same as HashSet O(1).
Stack: Stack class extends Vector class with five operations to support LIFO (Last In First Out). Stack internally has a pointer: TOP, which refers to the top of the Stack element.
PriorityQueue: PriorityQueue class is the implementation of Queue. The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time.
3. Data Structures Illustrated Book
The Data structures in action illustrated Java Book uses visual language to explain the major concepts in data structures, how they work, and when to use what. It can be read for reference or entertainment. This page includes free download of the source code of data structures implementations.