Java Collection Framework
Let’s understand what collections are…
☞ Collections can be defined as containers that group multiple items in a single unit. (And they are dynamic containers. That means we can add, remove data dynamically at the runtime)
☞ It provides an architecture to manipulate and also to store data.
☞ Various operations like searching, sorting, insertion, deletion ..etc can be done using the Java Collection Framework.
☞ There are many classes and interfaces in the Java Collection Framework.
What is an interface?
Completely abstract class.
To access an interface, it must be implemented by another class.
It Cannot be used to create objects.
It cannot contain constructors.
Iterable
This is the root interface of all collection classes.
Collection
All the classes in the Collection framework implement this Collection interface.
Lists
✦ This is an interface and extends the collection interface.
✦ Lists support redundancy.
✦ Can add duplicate elements.
✦ Contains ordered collections of elements including duplicate values.
ArrayList class
★ Here the elements can be added or removed dynamically.
★ If we add elements more than the initial value, the size of the list will be increased.
ArrayList Object = new ArrayList(); ArrayList <>list = new ArrayList<>();
★ This belongs to java. util.ArrayList package.
LinkedList class
★ Maintains insertion order.
★ Is a sequence of links that contain items.
★ Each element in the linked list is linked using pointers.
Linkedlist objet = new linkedlist();
Singly Linked List
✦ Here each node contains data of the particular node and the reference to the next node in the list. (Figure 2)
Doubly Linked List
✦ Here it has 2 references as one to the next node and another one is to the previous node. (Figure 3)
Circular Linked List
✦ Here the last node contains a pointer to the first node of the list.
✦ Also, both Singly-linked lists and doubly linked lists can be turned into a circular linked list.
Vector
★ It’s like a dynamic array.
★ Vector implements a dynamic array and it’s not limited to a specific size and is synchronized.
★ It is recommended to use the vector class only in the thread-safe implementation.
Queue
✦ This follows the FIFO approach. (Orders the elements in First In First Out)
✦ Here insertion happens in the rear end and deletion happens in the front end.
Priority Queue
★ It doesn't follow FIFO.
★ Elements with higher priority will be deleted before the lessor priority elements.
★ If there were 2 elements with the same priority, then it will follow the FIFO.
Deque
★ This is a linear data structure and insertion and deletion can be done in both ends.
Set
✦ This refers to a collection of data that can not contain any duplicated elements.
When to use sets?
☆ When you want to store elements without any duplications.
☆ When you do not care about the order.
HashSet class
★ Conatains only unique elemetns.
★ HashSet implements Set interface.
★ Best for searching.
★ Allows null values.
★ Uses Hashing mechanism to store the elements.
★ It is an unordered & unsorted collection of data set.
★ Not Thread-safe
HashSet<String> al = new HashSet();
LinkedHashSet class
★ Conatains only unique elemetns.
★ Maintains an ordered and sorted collection of HashSet.
★ Allows null values.
★ Not Thread-safe
LinkedHashSet<String> al = new LinkedHashSet();
TreeSet class
★ Doesn't contain null values.
★ No duplications.
★ Maintain sorting in ascending order.
★ Access and Retrieval are fast.
★ Not Thread-safe
★ Contains unique elements.
TreeSet<String> al = new TreeSet<String>();
Map
✦ This is used when finding something by key.
✦ Maps know the value associated with the given key.
✦ Can have duplicate values but Can’t have duplicate keys.
✦ Maps don't extend from java. util. collections, but still it is considered as a part of collections.
✦ Each element in a Map has 2 objects as a key and a value.
HashMap< , > object1 = new HashMap < , > ();
References