0% found this document useful (0 votes)
69 views

Collections Framework in Java: RNS Institute of Technology

The document discusses the Java Collections Framework which standardizes how groups of objects are handled in programs. It introduces key interfaces like List, Set, and Map. Common implementations of these interfaces include ArrayList, LinkedList, HashSet and TreeMap. The Collections Framework aims to increase program speed and quality through reusable and interchangeable data structures and algorithms.

Uploaded by

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

Collections Framework in Java: RNS Institute of Technology

The document discusses the Java Collections Framework which standardizes how groups of objects are handled in programs. It introduces key interfaces like List, Set, and Map. Common implementations of these interfaces include ArrayList, LinkedList, HashSet and TreeMap. The Collections Framework aims to increase program speed and quality through reusable and interchangeable data structures and algorithms.

Uploaded by

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

Collections Framework in Java

Abhinav Dogra
Department of Computer science

RNS Institute of Technology


Benagluru, India
Email-: [email protected]

Abstract— This paper aims at providing introduction to


EASE OF USE
Java Collections Framework (JCF). A lot of classes and
methods have been developed in framework to enhance the REDUCES PROGRAMMING EFFORT
speed of programs in terms of computational complexity. By providing useful data structures and algorithms, the
There are several methods that must be taken in Collections Framework frees you to concentrate on the
consideration; time complexity, code reusability, important parts of your program rather than on the low-level
interoperability. "plumbing" required to make it work .

Keywords— Interface, API, Instantiation. INCREASES PROGRAM SPEED AND QUALITY


This Collections Framework provides high-performance,
INTRODUCTION high-quality implementations of useful data structures and
algorithms. The various implementations of each interface
The Java Collections Framework standardizes the are interchangeable, so programs can be easily tuned by
way in which groups of objects are handled by your switching collection implementation.
programs. Collections were not part of the original Java
release, but were added by J2SE 1.2. Prior to the ALLOWS INTEROPERABILITY AMONG UNRELATED
Collections Framework, Java provided ad hoc classes
APIS
such as Dictionary, Vector, Stack, and Properties to
store and manipulate groups of objects. Although these The collection interfaces are the vernacular by which
classes were quite useful, they lacked a central, APIs pass collections back and forth. If my network
unifying theme. The way that you used Vector was administration API furnishes a collection of node names and
if your GUI toolkit expects a collection of column headings,
different from the way that you used Properties, for our APIs will interoperate seamlessly, even though they were
example. Also, this early, ad hoc approach was not written independently.
designed to be easily extended or adapted. Collections
are an answer to these problems. The Collections
Framework was designed to meet several goals. First, FOSTERS SOFTWARE REUSE
the framework had to be high-performance. The New data structures that conform to the standard
implementations for the fundamental collections collection interfaces are by nature reusable. The same
goes for new algorithms that operate on objects that
(dynamic arrays, linked lists, trees, and hash tables) are implement these interfaces.
highly efficient. Second, the framework had to allow
different types of collections to work in a similar
manner and with a high degree of interoperability. Abbreviations and Acronyms
Third, extending and/or adapting a collection had to be
easy. Toward this end, the entire Collections J2SE- Java platform, Standard Edition
Framework is built upon a set of standard interfaces. GUI- Graphical user interface
Several standard implementations (such as LinkedList,
HashSet, and TreeSet) of these interfaces are provided LIFO- Last-in-first-out
that you may use as-is. You may also implement your FIFO- First-in -first-out
own collection, if you choose.[4]

XXX-X-XXXX-XXXX-X/XX/$XX.00 ©20XX IEEE


Hierarchy of the Collections Framework ordered collection of the objects. This also allows
duplicate data to be present in it. This list interface
is implemented by various classes
like ArrayList, Vector, Stack etc.

List <T> al = new ArrayList<> ();


List <T> ll = new LinkedList<> ();
List <T> v = new Vector<> ();
Where T is the type of the object
Instantiation a list object with different classes.[2]

THE CLASSES WHICH IMPLEMENT THE LIST


INTERFACE:-

Fig1: Hierarchy of the collections framework[3] • ArrayList : ArrayList provides us with


dynamic arrays in Java. Though, it may be
• Class: A class is a user-defined blueprint or slower than standard arrays but can be helpful
prototype from which objects are created. It in programs where lots of manipulation in the
represents the set of properties or methods that are array is needed.[1] The size of an ArrayList is
common to all objects of one type. increased automatically if the collection
grows or shrinks if the objects are removed
• Interface: An interface can have methods and from the collection. Java ArrayList allows us
variables, but the methods declared in an interface to randomly access the list. ArrayList can not
are by default abstract (only method signature, no
be used for primitive types, like int, char, etc.
body). Interfaces specify what a class must do and
not how. It is the blueprint of the class.
We will need a wrapper class for such cases.

Interfaces which extend the Collections Interface


• Vector : A vector 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
• Iterable Interface : This is the root interface for
the entire collection framework. The collection
array is needed. This is identical to ArrayList
interface extends the iterable interface. Therefore, in terms of implementation. However, the
inherently, all the interfaces and classes implement primary difference between a vector and an
this interface. The main functionality of this ArrayList is that a Vector is synchronized and
interface is to provide an iterator for the collections. an ArrayList is non-synchronized.
Therefore, this interface contains only one abstract
method which is the iterator.[2]
• Collection Interface : This interface extends the • LinkedList: LinkedList class is an
iterable interface and is implemented by all the
classes in the collection framework. This interface
implementation of the LinkedList data
contains all the basic methods which every structure which is a linear data structure
collection has like adding the data into the where the elements are not stored in
collection, removing the data, clearing the data, etc. contiguous locations and every element is a
All these methods are implemented in this interface separate object with a data part and address
because these methods are implemented by all the part. The elements are linked using pointers
classes irrespective of their style of implementation. and addresses. Each element is known as a
• List Interface : This is a child interface of the node.
collection interface. This interface is dedicated to
the data of the list type in which we can store all the
• Stack : Stack class models and implements • Deque Interface : Usually pronounced
the Stack data structure. The class is based on as deck, a deque is a double-ended-queue.
the basic principle of last-in-first-out. In A double-ended-queue is a linear collection
addition to the basic push and pop operations, of elements that supports the insertion and
the class provides three more functions of removal of elements at both end points.
empty, search and peek. The class can also be The Deque interface is a richer abstract data
referred to as the subclass of Vector. type than both Stack and Queue because it
implements both stacks and queues at the
same time. The Deque interface, defines
methods to access the elements at both ends
of the Deque instance. Methods are
• Queue Interface : A queue interface
provided to insert, remove, and examine the
maintains the FIFO(First In First Out) order elements. Predefined classes
similar to a real-world queue line. This like ArrayDeque and LinkedList implement
interface is dedicated to storing all the the Deque interface.
elements where the order of the elements
matter. For example, whenever we try to
book a ticket, the tickets are sold at the first
come first serve basis. Therefore, the person
Deque<T> ad = new
whose request arrives first into the queue gets ArrayDeque<> ();
the ticket. There are various classes Where T is the type of the
like PriorityQueue, Deque, ArrayDeque, etc. object.
Instantiation a deque object with ArrayDeque
class.[2]

Queue <T> pq = new PriorityQueue<> ();


Queue <T> ad = new ArrayDeque<> ();
Where T is the type of the object.
Instantiation a queue object with different classes.[2]

Fig3: Deque Methods

Fig2: Queue Interface

• Priority Queue : A PriorityQueue is used • ArrayDeque : ArrayDeque class which is


when the objects are supposed to be implemented in the collection framework
processed based on the priority. It is provides us with a way to apply resizable-
known that a queue follows the First-In- array. This is a special kind of array that
First-Out algorithm, but sometimes the grows and allows users to add or remove an
elements of the queue are needed to be element from both sides of the queue. Array
processed according to the priority and deques have no capacity restrictions and they
this class is used in these cases. The grow as necessary to support usage.[2] Null
PriorityQueue is based on the priority elements are prohibited in the array deques.
heap. The elements of the priority queue They are faster than Stack and LinkedList.
are ordered according to the natural They are not thread-safe; in the absence of
ordering, or by a Comparator provided at external synchronization.[4]
queue construction time, depending on
which constructor is used.[4]
• Set Interface : A set is an unordered TreeSet. Since this class implements the
collection of objects in which duplicate SortedSet, we can instantiate a SortedSet
values cannot be stored. This collection is object with this class.[2]
used when we wish to avoid the duplication
of the objects and wish to store only the
unique objects.[1] This set interface is SortedSet<T> ts = new TreeSet<>
implemented by various classes
like HashSet, TreeSet, LinkedHashSet, etc.
();
Since all the subclasses implement the set, we Where T is the type of the object.
can instantiate a set object with any of these
classes.[4]

• TreeSet : The TreeSet class uses a Tree for


Set<T> hs = new HashSet<> (); storage. The ordering of the elements is
Set<T> lhs = new LinkedHashSet<> (); maintained by a set using their natural
Set<T> ts = new TreeSet<> (); ordering whether or not an explicit
comparator is provided. This must be
Where T is the type of the object. consistent with equals if it is to correctly
Instantiation of set object with different classes[2] implement the Set interface. It can also be
ordered by a Comparator provided at set
creation time, depending on which
• HashSet : The HashSet class is an inherent constructor is used.[3]
implementation of the hash table data
structure. The objects that we insert into the
HashSet do not guarantee to be inserted in the
same order. The objects are inserted based on
their hashcode. This class also allows the • Map Interface : A map is a data structure
insertion of NULL elements . HashSet was which supports the key-value pair mapping
introduced in Java 2. for the data. This interface doesn’t support
duplicate keys because the same key cannot
have multiple mappings. A map is useful if
there is a data and we wish to perform
operations on the basis of the key. This map
• LinkedHashSet : A LinkedHashSet is very interface is implemented by various classes
like HashMap, TreeMap etc. Since all the
similar to a HashSet. The difference is that this subclasses implement the map, we can
uses a doubly linked list to store the data and instantiate a map object with any of these
classes.[2]
retains the ordering of the elements. HashSet is an
unordered & unsorted collection of the data set,
whereas the LinkedHashSet is an ordered and Map<T> hm = new HashMap<> ();
sorted collection of HashSet. LinkedHashSet was Map<T> tm = new TreeMap<> ();
Where T is the type of the object.
introduced in Java 4.[3]
Instantiation of set object with different classes.[2]

• HashMap : HashMap provides the basic


• Sorted Set Interface: This interface is implementation of the Map interface of Java.
very similar to the set interface. The only It stores the data in (Key, Value) pairs. To
difference is that this interface has extra access a value in a HashMap, we must know
methods that maintain the ordering of the its key. HashMap uses a technique called
Hashing. Hashing is a technique of converting
elements. The sorted set interface extends a large String to small String that represents
the set interface and is used to handle the the same String so that the indexing and
data which needs to be sorted. The class search operations are faster. HashSet also uses
which implements this interface is HashMap internally.
• Elements in a Deque can be used in both LIFO
and FIFO.
Summary of Interfaces
• The second tree starts with the Map interface,
which maps keys and values similar to
The Java Collections Framework hierarchy consists a Hashtable.
of two distinct interface trees :
• Map's subinterface, SortedMap, maintains its
key-value pairs in ascending order or in an
order specified by a Comparator.[4]
• The first tree starts with
the Collection interface, which provides for the
basic functionality used by all collections, such
as add and remove methods. Its subinterfaces
— Set, List, and Queue — provide for more
specialized collections.
ACKNOWLEDGMENT
• The Set interface does not allow duplicate I would like to thank our project mentor
elements. This can be useful for storing Dr.Bhavanishankar K Assistant Prof. Dept. of CSE, RNSIT
collections such as a deck of cards or student Bengaluru.
records. The Set interface has a
subinterface, SortedSet, that provides for
REFERENCES
ordering of elements in the set.

• The List interface provides for an ordered [1] Herbert Schildt, The Complete Reference , JAVA 9 th edition. Java.util
Part 2: The Collection Framework, 498-510.
collection, for situations in which you need
[2] GeeksForGeeks, Collection in Java
precise control over where each element is https://www.geeksforgeeks.org/collections-in-java-2/
inserted. You can retrieve elements from [3] Tutorials point, Java-The Collections Framework ,
a List by their exact position. https://www.tutorialspoint.com/java/java_collections.htm
[4] Oracle,The JAVA tutorials,Intro to Collections,
• The Queue interface enables additional https://docs.oracle.com/javase/tutorial/collections/intro/index.html.
insertion, extraction, and inspection operations.
Elements in a Queue are typically ordered in on .
a FIFO basis.

• The Deque interface enables insertion, deletion,


and inspection operations at both the ends.

You might also like