OOPS With Java Unit 4
OOPS With Java Unit 4
AKTU
Unit :- 4 BCS-403
Java Collections Framework
3. Dynamic Size
The LinkedList stores its items in "containers." The list has a link to the first container
and each container has a link to the next container in the list. To add an element to the
list, the element is placed into a new container and that container is linked to one of the
other containers in the list.
Vector uses a dynamic array to store the data elements. It is similar to ArrayList. However, It is
synchronized and contains many methods that are not part of Collection framework.
It implements the last-in-first-out data structure, i.e., Stack. The stack contains all of the methods of
Vector class and also provides its methods like boolean push(), boolean peek(), boolean
push(object o), which defines its properties.
Being an interface the queue needs a concrete
class for the declaration and the most common
classes are the PriorityQueue and LinkedList in
Java. Note that neither of these implementations
is thread-safe. PriorityBlockingQueue is one
alternative implementation if the thread-safe
implementation is needed
Queue interface maintains the first-in-first-out order.
It can be defined as an ordered list that is used to hold the elements which
are about to be processed.
There are various classes like PriorityQueue, Deque, and ArrayDeque
which implements the Queue interface.
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.
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
follows:
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:
A LinkedHashMap is an extension of the HashMap class and it implements
the Map interface.Therefore, the class is declared as:
In this class, the data is stored in the form of nodes. The implementation
of the LinkedHashMap is very similar to a doubly-linked list. Therefore,
each node of the LinkedHashMap is represented as:
The Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a
key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must
implement the hashCode method and the equals method.
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.
In the event that any data is changed from the properties record, you don’t have to recompile the
java class. It is utilized to store data that is to be changed habitually.
Note: The Properties class does not inherit the concept of a load factor from its superclass,
Hashtable Declaration
public class Properties extends Hashtable<Object,Object>
Constructors of Properties