0% found this document useful (0 votes)
18 views8 pages

Os Assignment

Uploaded by

Heroo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

Os Assignment

Uploaded by

Heroo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

SYCS37 OS ASSIGNMENT YADNESH SHINDE

Explain in detail about Queue, Linkedlist, Hashset, ArrayList in java.


1.QUEUE IN JAVA:
A queue is a data structure which follows the principle of FIFO (First-In-First-Out) i.e.
the elements are inserted at the end of the list, and are deleted from the beginning
of the list. This interface is available in the java.util.package and extends the
Collection Interface.

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.

Let us move to the next topic of this article on Java Queue,

Implementation Of Java Queue


In order to use the queue interface, we need to instantiate a concrete class.
Following are the few implementations that can be used:

 util.LinkedList
 util.PriorityQueue

Methods In Java Queue


 add(): The add() method is used to insert elements at the end, or at the tail of
the queue. The method is inherited from the Collection interface.
 offer(): The offer() method is preferable to the add() method, as it inserts the
specified element into the queue without violating any capacity restrictions.
 peek(): The peek() method is used to look at the front of the queue without
removing it. If the queue is empty, it returns a null value.
 element(): If the queue is empty, the method throws
NoSuchElementException.

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.

How Does LinkedList work Internally?

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.

Constructors in the LinkedList

In order to create a LinkedList, we need to create an object of the LinkedList class.


The LinkedList class consists of various constructors that allow the possible creation
of the list. The following are the constructors available in this class:

2
SYCS37 OS ASSIGNMENT YADNESH SHINDE

1. LinkedList(): This constructor is used to create an empty linked list. If we wish to


create an empty LinkedList with the name ll, then, it can be created as:

LinkedList ll = new LinkedList();

2. LinkedList(Collection C): This constructor is used to create an ordered list that


contains all the elements of a specified collection, as returned by the collection’s
iterator. If we wish to create a LinkedList with the name ll, then, it can be created as:

LinkedList ll = new LinkedList(C);

LinkedList Methods

For many cases, the ArrayList is more efficient as it is common to need


access to random items in the list, but the LinkedList provides several
methods to do certain operations more efficiently:

Method Description

addFirst() Adds an item to the beginning of the list.

addLast() Add an item to the end of the list

removeFirst() Remove an item from the beginning of the list.

removeLast() Remove an item from the end of the list

getFirst() Get the item at the beginning of the list

getLast() Get the item at the end of the list

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.

A few important features of HashSet are:

 Implements Set Interface.


 The underlying data structure for HashSet is Hashtable.
 As it implements the Set Interface, duplicate values are not allowed.
 Objects that you insert in HashSet are not guaranteed to be inserted in the
same order. Objects are inserted based on their hash code.
 NULL elements are allowed in HashSet.
 HashSet also implements Serializable and Cloneable interfaces.

The Hierarchy of HashSet is as follows:

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.

Syntax: Declaration of HashSet

public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable,


Serializable

5
SYCS37 OS ASSIGNMENT YADNESH SHINDE

where E is the type of elements stored in a HashSet.

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.

Constructors of HashSet class

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:

HashSet<E> hs = new HashSet<E>();

2. HashSet(int initialCapacity): This constructor is used to build an empty HashSet


object in which the initialCapacity is specified at the time of object creation. Here,
the default loadFactor remains 0.75.

HashSet<E> hs = new HashSet<E>(int initialCapacity);

3. HashSet(int initialCapacity, float loadFactor): This constructor is used to build an


empty HashSet object in which the initialCapacity and loadFactor are specified at the
time of object creation.

6
SYCS37 OS ASSIGNMENT YADNESH SHINDE

HashSet<E> hs = new HashSet<E>(int initialCapacity, float loadFactor);

4. HashSet(Collection): This constructor is used to build a HashSet object containing


all the elements from the given collection. In short, this constructor is used when any
conversion is needed from any Collection object to the HashSet object. If we wish to
create a HashSet with the name hs, it can be created as:

HashSet<E> hs = new HashSet<E>(Collection C);

4.ArrayList in Java

ArrayList is a part of collection framework and is present in java.util package. It


provides us with dynamic arrays in Java. Though, it may be slower than standard
arrays but can be helpful in programs where lots of manipulation in the array is
needed. This class is found in java.util package.

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:

 Creates a bigger-sized memory on heap memory (for example memory of


double size).
 Copies the current memory elements to the new memory.
 New item is added now as there is bigger memory available now.
 Delete the old memory.

Important Features:

 ArrayList inherits AbstractList class and implements the List interface.


 ArrayList is initialized by the size. However, the size is increased automatically
if the collection grows or shrinks if the objects are removed from the
collection.
 Java ArrayList allows us to randomly access the list.
 ArrayList can not be used for primitive types, like int, char, etc. We need a
wrapper class for such cases.
 ArrayList in Java can be seen as a vector in C++.
 ArrayList is not Synchronized. Its equivalent synchronized class in Java is
Vector.

You might also like