Os Assignment
Os Assignment
Queue supports multiple methods, including insertion and deletion. The queues
available in java.util.package are known as Unbounded Queues , while the queues
present in the java.util.concurrent package are known are Bounded Queues.
All queues, except the Deques, support insertion at the end and deletion from the
front. Deques support insertion and deletion of elements at both the ends.
util.LinkedList
util.PriorityQueue
1
SYCS37 OS ASSIGNMENT YADNESH SHINDE
remove(): The remove() method removes the front of the queue and returns
it. Throws NoSuchElementException if the queue is empty.
poll(): The poll() method removes the beginning of the queue and returns it. If
the queue is empty, it returns a null value.
2.LinkedList in Java:
Linked List is a part of the Collection framework present in java.util package. This
class is an implementation of the LinkedList data structure which is a linear data
structure where the elements are not stored in contiguous locations and every
element is a separate object with a data part and address part. The elements are
linked using pointers and addresses. Each element is known as a node. Due to the
dynamicity and ease of insertions and deletions, they are preferred over the arrays. It
also has a few disadvantages like the nodes cannot be accessed directly instead we
need to start from the head and follow through the link to reach a node we wish to
access.
Since a LinkedList acts as a dynamic array and we do not have to specify the size
while creating it, the size of the list automatically increases when we dynamically add
and remove items. And also, the elements are not stored in a continuous fashion.
Therefore, there is no need to increase the size. Internally, the LinkedList is
implemented using the doubly linked list data structure. The main difference
between a normal linked list and a doubly LinkedList is that a doubly linked list
contains an extra pointer, typically called the previous pointer, together with the
next pointer and data which are there in the singly linked list.
2
SYCS37 OS ASSIGNMENT YADNESH SHINDE
LinkedList Methods
Method Description
3
SYCS37 OS ASSIGNMENT YADNESH SHINDE
3.HashSet in Java
The HashSet class implements the Set interface, backed by a hash table which is
actually a HashMap instance. No guarantee is made as to the iteration order of the
set which means that the class does not guarantee the constant order of elements
over time. This class permits the null element. The class also offers constant time
performance for the basic operations like add, remove, contains, and size assuming
the hash function disperses the elements properly among the buckets, which we
shall see further in the article.
4
SYCS37 OS ASSIGNMENT YADNESH SHINDE
HashSet extends Abstract Set<E> class and implements Set<E>, Cloneable, and
Serializable interfaces where E is the type of elements maintained by this set. The
directly known subclass of HashSet is LinkedHashSet.
Now for the maintenance of constant time performance, iterating over HashSet
requires time proportional to the sum of the HashSet instance’s size (the number of
elements) plus the “capacity” of the backing HashMap instance (the number of
buckets). Thus, it’s very important not to set the initial capacity too high (or the load
factor too low) if iteration performance is important.
Initial Capacity: The initial capacity means the number of buckets when hashtable
(HashSet internally uses hashtable data structure) is created. The number of buckets
will be automatically increased if the current size gets full.
Load Factor: The load factor is a measure of how full the HashSet is allowed to get
before its capacity is automatically increased. When the number of entries in the
hash table exceeds the product of the load factor and the current capacity, the hash
table is rehashed (that is, internal data structures are rebuilt) so that the hash table
has approximately twice the number of buckets.
Effect on performance: Load factor and initial capacity are two main factors that
affect the performance of HashSet operations. A load factor of 0.75 provides very
effective performance with respect to time and space complexity. If we increase the
load factor value more than that then memory overhead will be reduced (because it
will decrease internal rebuilding operation) but, it will affect the add and search
operation in the hashtable. To reduce the rehashing operation we should choose
initial capacity wisely. If the initial capacity is greater than the maximum number of
entries divided by the load factor, no rehash operation will ever occur.
5
SYCS37 OS ASSIGNMENT YADNESH SHINDE
Internal working of a HashSet: All the classes of Set interface are internally backed
up by Map. HashSet uses HashMap for storing its object internally. You must be
wondering that to enter a value in HashMap we need a key-value pair, but in
HashSet, we are passing only one value.
Storage in HashMap: Actually the value we insert in HashSet acts as a key to the map
Object and for its value, java uses a constant variable. So in the key-value pair, all the
values will be the same.
In order to create a HashSet, we need to create an object of the HashSet class. The
HashSet class consists of various constructors that allow the possible creation of the
HashSet. The following are the constructors available in this class.
1. HashSet(): This constructor is used to build an empty HashSet object in which the
default initial capacity is 16 and the default load factor is 0.75. If we wish to create an
empty HashSet with the name hs, then, it can be created as:
6
SYCS37 OS ASSIGNMENT YADNESH SHINDE
4.ArrayList in Java
Illustration:
7
SYCS37 OS ASSIGNMENT YADNESH SHINDE
Since ArrayList is a dynamic array and we do not have to specify the size while
creating it, the size of the array automatically increases when we dynamically add
and remove items. Though the actual library implementation may be more complex,
the following is a very basic idea explaining the working of the array when the array
becomes full and if we try to add an item:
Important Features: