[go: up one dir, main page]

0% found this document useful (0 votes)
26 views9 pages

Unit-4 Oopj PDF

Uploaded by

Pari Aggarwal
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)
26 views9 pages

Unit-4 Oopj PDF

Uploaded by

Pari Aggarwal
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/ 9

UNIT- 4th

Java Collections F ramework

Collections in Java

The Collection in Java is a framework that provides an architecture to


store and manipulate the group of objects.

Java Collections can achieve all the operations that you perform on a
data such as searching, sorting, insertion, manipulation, and deletion.

What is a framework in Java


o It provides readymade architecture.
o It represents a set of classes and interfaces.
o It is optional.

The Collection framework represents a unified architecture for storing


and manipulating a group of objects. It has:

1. Interfaces and its implementations, i.e., classes


2. Algorithm
Hierarchy of Collection Framework

Iterable Interface

The Iterable interface is the root interface for all the collection classes.
The Collection interface extends the Iterable interface and therefore all
the subclasses of Collection interface also implement the Iterable
interface.

It contains only one abstract method. i.e.,


Collection Interface

The Collection interface is the interface which is implemented by all the


classes in the collection framework. It declares the methods that every
collection will have. In other words, we can say that the Collection
interface builds the foundation on which the collection framework
depends.

Some of the methods of Collection interface are Boolean add ( Object


obj), Boolean addAll ( Collection c), void clear(), etc. which are
implemented by all the subclasses of Collection interface.

List Interface

List interface is the child interface of Collection interface. It inhibits a


list type data structure in which we can store the ordered collection of
objects. It can have duplicate values.

List interface is implemented by the classes Ar rayList, LinkedList,


Vector, and Stack.

To instantiate the List interface, we must use

1. List <data-type> list1= new ArrayList();


2. List <data-type> list2 = new LinkedList();
3. List <data-type> list3 = new Vector();
4. List <data-type> list4 = new Stack();

ArrayList

The ArrayList class implements the List interface. It uses a dynamic


array to store the duplicate element of different data types. The
ArrayList class maintains the insertion order and is non-synchronized.
The elements stored in the ArrayList class can be randomly accessed.
Consider the following example.

LinkedList

LinkedList implements the Collection interface. It uses a doubly linked


list internally to store the elements. It can store the duplicate elements. It
maintains the insertion order and is not synchronized. In LinkedList, the
manipulation is fast because no shifting is required.

Set Interface

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.

LinkedHashSet

LinkedHashSet class represents the LinkedList implementation of Set


Interface. It extends the HashSet class and implements Set interface.
Like HashSet, It also contains unique elements. It maintains the insertion
order and permits null elements.

SortedSet Interface

SortedSet is the alternate of Set interface that provides a total ordering


on its elements. The elements of the SortedSet are arranged in the
increasing (ascending) order. The SortedSet provides the additional
methods that inhibit the natural ordering of the elements.
TreeSet

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.

Java Map Interface

A map contains values on the basis of key, i.e. key and value pair. Each key and
value pair is known as an entry. A Map contains unique keys.

A Map is useful if you have to search, update or delete elements on the basis
of a key.
Java TreeMap class

Java TreeMap class is a red-black tree based implementation. It provides an


efficient means of storing key-value pairs in sorted order.

o Java TreeMap contains values based on the key. It implements the


NavigableMap interface and extends AbstractMap class.
o Java TreeMap contains only unique elements.
o Java TreeMap cannot have a null key but can have multiple null
values.
o Java TreeMap is non synchronized.
o Java TreeMap maintains ascending order .
Difference between Comparable and Comparator

Using Comparable Interface

A comparable object is capable of comparing itself with another


object. The class itself must implements
the java.lang.Comparable interface to compare its instances.
Consider a Movie class that has members like, rating, name, year.
Suppose we wish to sort a list of Movies based on year of release. We
can implement the Comparable interface with the Movie class, and we
override the method compareTo() of Comparable interface.

Using Comparator

Unlike Comparable, Comparator is external to the element type we are


comparing. It’s a separate class. We create multiple separate classes
(that implement Comparator) to compare by different members.
Collections class has a second sort() method and it takes Comparator.
The sort() method invokes the compare() to sort objects.
To compare movies by Rating, we need to do 3 things:
1. Create a class that implements Comparator (and thus the compare()
method that does the work previously done by compareTo()).
2. Make an instance of the Comparator class.
3. Call the overloaded sort() method, giving it both the list and the
instance of the class that implements Comparator.

 Comparable is meant for objects with natural ordering which means


the object itself must know how it is to be ordered. For example
Roll Numbers of students. Whereas, Comparator interface sorting is
done through a separate class.
 Logically, Comparable interface compares “this” reference with the
object specified and Comparator in Java compares two different
class objects provided.
 If any class implements Comparable interface in Java then
collection of that object either List or Array can be sorted
automatically by using Collections.sort() or Arrays.sort() method
and objects will be sorted based on there natural order defined by
CompareTo method.
 A basic differentiating feature is that using comparable we can use
only one comparison. Whereas, we can write more than one custom
comparators as you want for a given type, all using different
interpretations of what sorting means. Like in the comparable
example we could just sort by only one attribute, i.e., year but in the
comparator, we were able to use different attributes like rating,
name, and year as well.

Properties class in Java

The properties object contains key and value pair both as a string. The
java.util.Properties class is the subclass of Hashtable.

It can be used to get property value based on the property key. The
Properties class provides methods to get data from the properties file and
store data into the properties file. Moreover, it can be used to get the
properties of a system.

Features of P roperties class:

 Properties is a subclass of Hittable.


 It is used to maintain a list of values in which the key is a string and
the value is also a string i.e; it can be used to store and retrieve
string type data from the properties file.
 Properties class can specify other properties list as it’s the default.
If a particular key property is not present in the original Properties
list, the default properties will be searched.
 Properties object does not require external synchronization and
Multiple threads can share a single Properties object.
 Also, it can be used to retrieve the properties of the system.

You might also like