COLLECTIONS FRAMEWORK IN JAVA
Collections
➢ Collections are the containers that group multiple items into a single unit.
➢ Collections are used to store, retrieve and manipulate data and to transmit data from one method to another.
Java Collections Framework
➢ A collections framework is a unified architecture for representing and manipulating collections.
➢ Java Collections framework provides many interfaces and classes in order to store data.
Benefits of a Collections Framework
🔸 It reduces programming effort.
🔸 It increases program speed and quality.
🔸 It allows interoperability among unrelated APIs.
🔸 It fosters software reuse.
🔸 It reduces effort to design new APIs.
Java Collections Framework Hierarchy
➢ A set of APIs linked together as a parent-child relationship.
Java Interfaces
➢ In interfaces in Java, you write methods which must be implemented by Java objects as rules.
➢ Interfaces are the reference types which are similar to classes but contains only abstract methods as rules.
Iterable
➢ The Iterable interface is the root interface for all the Collection classes.
➢ The Collection interface along with all its subclasses also implement the Iterable interface.
Methods: Iterator <T> iterator()
➢ From the above method, we get reference of Iterator and we can iterate in the forward direction in any data structure.
Collection
➢ Collection interface is implemented by all the classes in the collection framework and declares the methods that every collection contain.
➢ This achieves Runtime Polymorphism.
Methods: Boolean add (Object obj) Boolean addAll (Object obj) void clear() etc.
Lists
➢ Java list is an interface that extends the Collection.
➢ It contains an ordered collection of elements including duplicate values.
➢ Supports redundancy.
1. ArrayList
✽ ArrayList is the implementation of List Interface where the elements can be dynamically added or removed from the list.
✽ The size of the list is increased dynamically if the elements are added more than the initial size.
ArrayList object = new ArrayList();
2. LinkedList
✽ LinkedList is a sequence of links which contains items.
✽ Data Storage / Structure → Reference to other link
✽ Each link contains a connection to another link.
Linkedlist object = new Linkedlist();
2 different types of LinkedLists.
- Singly Linked List
● Each node in this list stores the data of the node and a pointer or reference to the next node in the list.
2. Doubly Linked List
● There are 2 references.
- To next node
- To previous node
Node ➜ Contains data
Next ➜ Points to next node
Previous ➜ Points to previous node
3. Vector
✽ This is an API that implements List Interface.
✽ Vectors are similar to arrays, where the elements of the vector object can be accessed via an index into the vector.
✽ A Vector is synchronized (thread safe) and implements a dynamic array that is not limited to specific size.
Vector object = new Vector(size, increment);
Java Queue
➢ Queue in Java follows a FIFO (First In First Out) approach.
First In First Out — Orders the elements according to First in First out manner
➢ The first element is removed first and last element is removed in the end.
Queue <Integer> q = new LinkedList<>();
Java Set
➢A set refers to a collection that cannot contain duplicate elements.
➢ Focuses on Uniqueness.
➢ It is mainly used to model the mathematical set abstraction.
➢ Uses hashing technique by which we will be having unique element coming in.
➢ Set has its implementation in various classes.
- HashSet
- LinkedHashSet
- TreeSet
1. HashSet
✽ Java HashSet class creates a collection that use a hash table for storage.
✽ HashSet only contain unique elements and it inherits the AbstractSet Class and implements Set Interface.
✽ It uses a hashing mechanism to store the elements.
Hashing function → Generates a hashed code for the element
HashSet <String> al = new HashSet();
2. LinkedHashSet
✽ Java LinkedHashSet class is a Hash table and Linkedlist implementation of the set interface.
✽ It contains only unique elements.
✽ It provides all optional set operations and maintains insertion order. (LinkedHashSet maintains how the data is getting inserted for us. HashSet is not able to do that, it gives us random/unordered output)
LinkedHashSet <String> al = new LinkedHashSet();
✽ This class inherits methods from other classes.
3. TreeSet
✽ A sorted version of HashSet.
✽ TreeSet class implements the Set Interface that uses a tree for storage.
✽ The objects of this class are unique and are stored in the ascending order.
✽ It inherits AbstractSet class and implements NavigableSet interface.
TreeSet <String> al = new TreeSet <String>();
Java Map
➢ A map is an object that maps keys to values that cannot contain duplicate keys. Each key can map to at most one value.
➢ Cannot traverse a Map, therefore it should be converted into Set using keySet() or entrySet() method.
HashMap< , > object1 = new HashMap < , > ();