Collection Framework
Collection Framework
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.
-----------
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)
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 :
------------------
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 :
------------------
-> 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.
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.
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
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)}
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.