Unit 4 JAVA 2
Unit 4 JAVA 2
Unit :- 4 BCS-403
Java Collections Framework
3. Dynamic Size
Deque interface extends the Queue interface. In Deque, we can remove and add
the elements from both the side. Deque stands for a double-ended queue which
enables us to perform the operations at both the ends.
ArrayDeque class implements the Deque interface. It facilitates us to use the Deque. Unlike queue, we can
add or delete the elements from both the ends.
ArrayDeque is faster than ArrayList and Stack and has no capacity restrictions.
In the above image, the navigable set extends the sorted set interface. Since a
set doesn’t retain the insertion order, the navigable set interface provides the
implementation to navigate through the Set. The class which implements the
navigable set is a TreeSet which is an implementation of a self-balancing tree. .
Therefore, this interface provides us with a way to navigate through this tree.
Set Interface in Java is present in java.util package. It extends the Collection interface
It represents the unordered set of elements which doesn't allow us to store the
duplicate items. We can store at most one null value in Set. Set is implemented by
HashSet, LinkedHashSet, and TreeSet. Set can be instantiated as:
Java TreeSet class implements the Set interface that uses a tree for storage. Like HashSet, TreeSet also
contains unique elements. However, the access and retrieval time of TreeSet is quite fast. The elements in
TreeSet stored in ascending order.
3. Efficient operations
Map stores Elements as
key-value pairs, where
each key is associated with a correspending value.
2. Uniqueness of Keys :- Each Key in a map is unique.
The LinkedHashMap Class is just like HashMap with an additional feature of maintaining an order of
elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion but it
never maintained the track and order of insertion, which the LinkedHashMap provides where the elements
can be accessed in their insertion order.
Important Features of a LinkedHashMap are listed as
Here, K is the key Object type and V is the value Object type
K – The type of the keys in the map.
V – The type of values mapped in the map.
It implements Map<K, V> interface, and extends HashMap<K, V> class.
Though the Hierarchy of LinkedHashMap is as depicted as follows:
How LinkedHashMap Work internally ?
The java.util.Hashtable class is a class in Java that provides a key-value data structure, similar to the Map
interface. It was part of the original Java Collections framework and was introduced in Java 1.0.
However, the Hashtable class has since been considered obsolete and its use is generally discouraged. This is
because it was designed prior to the introduction of the Collections framework and does not implement the
Map interface, which makes it difficult to use in conjunction with other parts of the framework. In addition, the
Hashtable class is synchronized, which can result in slower performance compared to other implementations
of the Map interface.
In general, it’s recommended to use the Map interface or one of its implementations (such as HashMap or
ConcurrentHashMap) instead of the Hashtable class.
It is similar to HashMap, but is synchronized.
Hashtable stores key/value pair in hash table.
In Hashtable we specify an object that is used as a key, and the value
we want to associate to that key.The key is then hashed, and the
resulting hash code is used as the index at which the value is
stored within the table.
HashMap doesn’t provide any Enumeration, while Hashtable provides not
fail-fast Enumeration.
Whenever we do hear sorting algorithms come into play such as selection sort,
bubble sort, insertion sort, radix sort, bucket sort, etc but if we look closer here we
are not asked to use any kind of algorithms.
1. Using loops
2. Using sort() method of Arrays class
3. Using sort method of Collections class
4. Sorting on a subarray
Java Comparable interface is used to order the objects of the user-defined class.
This interface is found in java.lang package and contains only one method named
compareTo(Object). It provides a single sorting sequence only, i.e., you can sort
the elements on the basis of single data member only. For example, it may be
rollno, name, age or anything else.
Collections class provides static methods for sorting the elements of collections.
If collection elements are of Set or Map, we can use TreeSet or TreeMap.
However, we cannot sort the elements of List. Collections class provides methods
for sorting the elements of List type elements.
Method 1: One obvious approach is to write our own sort() function using one of the standard
algorithms. This solution requires rewriting the whole sorting code for different criteria like Roll No.
and Name.
Method 2: Using comparator interface- Comparator interface is used to order the objects of a
user-defined class. This interface is present in java.util package and contains 2 methods
compare(Object obj1, Object obj2) and equals(Object element).
Using a comparator, we can sort the elements based on data members. For instance, it may be on
roll no, name, age, or anything else.
Method of Collections class for sorting List elements is used to sort the
elements of List by the given comparator.
Note: The Properties class does not inherit the concept of a load factor from its superclass,
Hashtable Declaration
public c l a s s Properties extends Hashtable<Object,Object>
Constructors of Properties