0% found this document useful (0 votes)
21 views6 pages

Collection Framework

Uploaded by

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

Collection Framework

Uploaded by

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

Collections Framework : (Java 1.

2 v)
-----------------------
In java to represents group of objects into a single object(entity) we need
certain Classes and Interfaces.
Collection framework provides different classes and interfaces.

Framework : It is a set of tools and modules that can be reused for various
projects.
-----------

Advantages of collections framework :


-------------------------------------
->They are grow-able in nature. We modify our group of data.
->These are using under-laying data-structure.So,ready-mate methods are available
->It holds homogeneous & heterogeneous dataType.

There are two parts in Collections framework,


a. Collection (I) : Used to store the data in single object format ex :
[10,20,30,40]
It has child interfaces, they are

i. List implementation classes : ArrayList, LinkedList ,


Vector
ii. Set implementation classes : HashSet, LinkedHashSet,
TreeSet
iii Queue implementation classes: PriorityQueue

b. Map(I) : Used to store the data two objects format in the form of
key:value (pair) [(k,v),(k,v),(k,v)]
{"ratan":5,"anu":2,"sravya":10,"that":5}
Map implementation classes : HashMap , LinkedHashMap ,
TreeMap , Hashtable

Collections (C):
----------------
It is a class in java .It is present in java.util package.It contains certain
methods to perform some
operations for collection object.

i. Collection (I):
------------------
The Collection interface is a member of the Java Collections Framework. It is a
part of java.util package.
It is one of the root interfaces of the Collection Hierarchy.
The Collection interface is not directly implemented by any class. It is
implemented indirectly via its
child-interfaces like List, Queue, and Set.

Methods:
--------

add(Object)
addAll()
remove()
removeAll()
clear()
isEmpty()
size()
contain(Object)
containAll(collection)
Arrays.toArray(collection)

=>These are common for all child interfaces of collection interface.

1. List(I):
-----------
List in Java provides the facility to maintain the ordered collection.
It can have the duplicate elements also. We can also store the null elements
in the list.
It contains the index-based methods to insert, update, delete and search the
elements.

->The List interface is found in the java.util package and inherits the Collection
interface
->The implementation classes of List interface are ArrayList(50%), LinkedList and
Vector
->The Vector class is deprecated since Java 1.2.
->The List interface is a data structure that store the ordered collection.

Methods:(Extra)
---------------

add(index,object)
addAll(index,collection)
remove(index)
get(object)
set(index,object)

Implement Classes:
------------------

-> ArrayList
-> LinkedList
-> Vector

ArrayList:
----------

ArrayList class uses a dynamic array for storing the elements. It is like
an array, but there is no size
limit. We can add or remove elements any time. So, it is much more
flexible than the traditional array.
It is found in the java.util package.

Important points :
------------------

-> Java ArrayList class can contain duplicate elements.


-> Java ArrayList class maintains insertion order.
-> Java ArrayList class is non synchronized.
-> Java ArrayList allows random access because the array works on an index
basis.
-> In ArrayList, manipulation is a little bit slower than the LinkedList
in Java because a lot of shifting
needs to occur if any element is removed from the array list.
-> We can not create an array list of the primitive types (such as int,
float, char, etc).
It is required to use the required wrapper class in such cases.
-> The size is dynamic in the array list, which varies according to the
elements getting added or removed
from the list.

LinkedList:
-----------
Java LinkedList class uses a doubly linked list to store the elements. It
provides a linked-list data
structure. It inherits the AbstractList class and implements List and
Deque interfaces

Important points :
------------------

-> Java LinkedList class can contain duplicate elements.


-> Java LinkedList class maintains insertion order.
-> Java LinkedList class is non synchronized.
-> In Java LinkedList class, manipulation is fast because no shifting
needs to occur.
-> Java LinkedList class can be used as a list, stack or queue.

Difference between ArrayList and LinkedList:


--------------------------------------------
-> ArrayList internally uses a dynamic array to store the elements.
LinkedList internally uses a doubly linked list to store the elements.

-> Manipulation with ArrayList is slow because it internally uses an


array. If any element is removed
from the array, all the other elements are shifted in memory.
Manipulation with LinkedList is faster than ArrayList because it uses a
doubly linked list, so no bit
shifting is required in memory.

-> An ArrayList class can act as a list only because it implements List
only.
LinkedList class can act as a list and queue both because it implements
List and Deque interfaces.

-> ArrayList is better for storing and accessing data.


LinkedList is better for manipulating data.

-> The memory location for the elements of an ArrayList is contiguous.


The location for the elements of a linked list is not contagious.

-> Generally, when an ArrayList is initialized, a default capacity of 10


is assigned to the ArrayList.
There is no case of default capacity in a LinkedList. In LinkedList, an
empty list is created when a
LinkedList is initialized.

Vector:
-------
-> Vector is also like as Arraylist.
-> It is synchronized .
-> The vector object is Thread safe. i.e At a time only one thread can
operate on the Vector object.
-> Performance is low because Threads are needed to wait.
-> Default and initial capacity 10.
-> 100% incrementation is there in capacity.

2. Set(I):
----------
Basically, Set is a type of collection that does not allow duplicate elements.
That means an element can only
exist once in a Set.

Characteristics of a Set collection:


------------------------------------
-> The following characteristics differentiate a Set collection from others
in the Java Collections framework
-> Duplicate elements are not allowed.
-> Elements are not stored in order. That means you cannot expect elements
sorted in any order when
iterating over elements of a Set.

Why and When Use Sets?


Based on the characteristics, consider using a Set collection when:
You want to store elements distinctly without duplication, or unique elements.
You don’t care about the order of elements.
For example, you can use a Set to store unique integer numbers; you can use a Set
to store cards randomly in a card game; you can use a Set to store numbers in
random order, etc.

Implementations:
----------------
The Java Collections Framework provides three major implementations :
HashSet, LinkedHashSet and TreeSet.

HashSet:
--------
It is the best-performing implementation and is a widely-used Set
implementation.
It represents the core characteristics of sets: no duplication and
unordered.

LinkedHashSet:
--------------
This implementation orders its elements based on insertion order.
So consider using a LinkedHashSet when you want to store unique
elements in order.

TreeSet:
--------
This implementation orders its elements based on their values, either by
their natural ordering, or by a
Comparator provided at creation time.

-> Therefore, besides the uniqueness of elements that a Set guarantees, consider

using HashSet when ordering does not matter;


using LinkedHashSet when you want to order elements by their insertion
order;
using TreeSet when you want to order elements by their values.

Map (I):
--------
A Map is an object that maps keys to values, or is a collection of attribute-
value pairs.
The map interface is present in java.util package.
The Map interface is not a sub-type of the Collection interface. Therefore it
behaves a bit differently from
the rest of the collection types.
A map contains unique keys. The maps are used to perform lookups by keys or
when someone wants to retrieve and
update elements by keys.

Ex:
---
{(1,ad),(3,ab),(2,ab)}

Map interface is implemented by

Implementations of Map:
-----------------------
In the inheritance tree of the Map interface, there are several
implementations but only 3 major, common,
and general purpose implementations - they are HashMap and LinkedHashMap and
TreeMap.

HashMap:
--------
-> This implementation uses a hash table as the underlying data structure.
-> It implements all of the Map operations and allows null values and one
null key.
-> This class is roughly equivalent to Hashtable - a legacy data structure
before Java Collections Framework,
but it is not synchronized and permits nulls. HashMap does not
guarantee the order of its key-value elements. Therefore, consider to use a HashMap
when order does not matter and nulls are acceptable.

LinkedHashMap:
--------------
-> This implementation uses a hash table and a linked list as the underlying
data structures, thus the order
of a LinkedHashMap is predictable, with insertion-order as the default
order.
-> This implementation also allows nulls like HashMap.
-> So consider using a LinkedHashMap when you want a Map with its key-
value pairs are sorted by their
insertion order.

TreeMap:
--------
-> This implementation uses a red-black tree as the underlying data
structure.
-> A TreeMap is sorted according to the natural ordering of its keys, or
by a Comparator provided at creation
time.
-> This implementation does not allow nulls. So consider using a TreeMap
when you want a Map sorts its
key-value pairs by the natural order of the keys (e.g. alphabetic order
or numeric order), or by a custom order you specify.

You might also like