Collection Notes - PDF
Collection Notes - PDF
--------------------
It is the collection of classes and interfaces to represent a group of object as a
single entity.
Collection
============
1-If we want to represent a group of indivisual object as a single entity then we
should go for COLLECTION interface.
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
System.out.println(l1);//[aaa, bbb]
System.out.println(l1.add("ccc"));//true
2-boolean addAll(Collection c)
-------------------------------
* It is used to add a group of element at a time.
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("ccc");
System.out.println(l1);//[aaa, bbb, ccc]
3-boolean remove(Object o)
---------------------------
* It is uded to remove the Specified element.
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("ccc");
System.out.println(l1);//[aaa, bbb, ccc]
l1.remove("ccc");
System.out.println(l1);//[aaa, bbb]
}
}
4-boolean removeAll(Collection c)
----------------------------------
* It is used to remove a group of element at a time.
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("ccc");
System.out.println(l1);//[aaa, bbb, ccc]
l2.removeAll(l1);
System.out.println(l2);//[]
}
}
5-boolean contains(Object o)
-----------------------------
* It checks wheather a particular element is present or not.
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("ccc");
System.out.println(l1);//[aaa, bbb, ccc]
System.out.println(l1.contains("bbb"));//true
System.out.println(l1.contains("zzz"));//false
}
}
6-boolean isEmpty()
--------------------
* It is used to check wheather the Collection is empty or not.
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("ccc");
System.out.println(l1);//[aaa, bbb, ccc]
System.out.println(l1.isEmpty());//false
}
}
7-int size()
-------------
* It returns the number of elements present in the Collection.
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("ccc");
System.out.println(l1);//[aaa, bbb, ccc]
System.out.println(l1.size());//3
}
}
8-void clear()
---------------
* It removes all the elements from the Collection.
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("ccc");
System.out.println(l1);//[aaa, bbb, ccc]
l1.clear();
System.out.println(l1);//[]
}
}
LIST
=====
* It is the child interface of Collection Interface.
* If we want to represent a group of indivisual Objects in to a single entity where
insertion is preserved and duplicates Objects are allowed then we should go for list
interface.
* We can differenctiate duplicate elements with respect to index,hence index plays
an important role in list interface.
METHODS OF LIST INTERFACE
==========================
1-boolean add()
----------------
* It is used to add element in the list.
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("ccc");
System.out.println(l1);//[aaa, bbb, ccc]
}
}
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("ccc");
System.out.println(l1);//[aaa, bbb, ccc]
l1.add(2,"ddd");
System.out.println(l1);//[aaa, bbb, ddd, ccc]
}
}
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("ccc");
System.out.println(l1);//[aaa, bbb, ccc]
l1.addAll(2,l2);
System.out.println(l1);//[aaa, bbb, ddd, eee, fff, ccc]
}
}
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("ccc");
System.out.println(l1);//[aaa, bbb, ccc]
System.out.println(l1.get(1));//bbb
}
}
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("ccc");
System.out.println(l1);//[aaa, bbb, ccc]
l1.remove(1);
System.out.println(l1);//[aaa, ccc]
}
}
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("ccc");
System.out.println(l1);//[aaa, bbb, ccc]
l1.set(1,"zzz");
System.out.println(l1);//[aaa, zzz, ccc]
}
}
7-int indexOf(Object o)
------------------------
* It is used to get the index of a specified Object.
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("ccc");
System.out.println(l1);//[aaa, bbb, ccc]
System.out.println(l1.indexOf("bbb"));//1
}
}
8-int lastIndexOf(Object o)
----------------------------
It is used to get the last index of an object.
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList l1=new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("aaa");
l1.add("ccc");
System.out.println(l1);//[aaa, bbb, aaa, ccc]
System.out.println(l1.lastIndexOf("aaa"));//2
}
}
ARRAYLIST
==========
* The underlying data structure is resizeable array or growable array.
* Duplicates are allowed.
* Insertion order is preserved.
* Heterogenious objects are allowed.
* Null insertion is possible.
* If our frequent operation is insertion and deletion from middle then ArrayList is
the bad choice
* If our frequent operation is retrival operation then ArrayList is the best
choicwe.
CONSTRUCTOR OF ARRAYLIST
=========================
1-ArrayList l1=new ArrayList();
METHODS OF ARRAYLIST
=====================
1-add(Object o)
2-remove(Object o)
3-contains(Object o)
4-isEmpty()
5-size()
6-add(int index)
7-remove(int index)
9-indexOf(Object o)
LINKEDLIST
===========
* The underlying data structure is doubly linked list
* Duplicates are allowed.
* Insertion order is preserved.
* Heterogenious objects are allowed.
* Null insertion is possible.
* If our frequent operation is insertion and deletion from middle then LinkedList is
the best choice
* If our frequent operation is retrival operation then LinkedList is the bad
choicwe.
CONSTRUCTOR OF LINKEDLIST
==========================
1-LinkedList l1=new LinkedList();
METHODS OF LINKEDLIST
======================
1-add(Object 0)
2-addFirst(Object o)
3-addLast(Object o)
4-removeFirst()
5-removeLast()
l1.addFirst("zzz");
System.out.println(l1);//[zzz, a, 10, b, 10.5, a, null]
l1.addLast("xxx");
System.out.println(l1);//[zzz, a, 10, b, 10.5, a, null, xxx]
l1.removeFirst();
System.out.println(l1);//[a, 10, b, 10.5, a, null, xxx]
l1.removeLast();
System.out.println(l1);//[a, 10, b, 10.5, a, null]
VECTOR
=======
* It is a legacy class(the class which is introduced in 1.0 version of java).
* The underlying data structure is resizable array or growable array.
* Duplicates objects are allowed.
* Insertion order is preserved.
* Heterogenious elements are allowed.
* Null insertion is possible
CONSTRUCTOR OF VECTOR
======================
1-Vector v=new Vector();
* If we reach the the max capacity then the new capacity will be like
METHODS OF VECTOR
==================
1-addElement(Object o)
2-capacity()
3-elementAt(int index)
4-firstelement()
5-lastelement()
Vector v=new Vector();
v.addElement("1");
v.addElement("2");
v.addElement("3");
v.addElement("4");
v.addElement("5");
System.out.println(v);//[1, 2, 3, 4, 5]
System.out.println(v.capacity());//10
System.out.println(v.elementAt(3));//4
System.out.println(v.firstElement());//1
System.out.println(v.lastElement());//5
STACK
======
* It is the child interface of vector.
* Whenever LIFO(last in first out) order is required then we should go for stack.
CONSTRUCTOR OF STACK
=====================
* It contains only one constructor
METHODS OF STACK
=================
1-Object push(Object O)
------------------------
* It is used to insert an object in to the stack.
2-Object pop()
---------------
* It is used to remove and return top of the stack.
3-Object peek()
----------------
* It is used to return the top of the stack without removing it.
4-boolean empty()
------------------
* It is used to check the corresponding stack is empty or not.
5-int search(Object o)
-----------------------
* It is used to return offset if the element is available and otherwise it returns
-1.
Stack s=new Stack();
s.push("a");
s.push("b");
s.push("c");
s.push("d");
s.push("e");
System.out.println(s);//[a, b, c, d, e]
System.out.println(s.pop());//e
System.out.println(s);//[a, b, c, d]
System.out.println(s.peek());//d
System.out.println(s);//[a, b, c, d]
System.out.println(s.isEmpty());//false
System.out.println(s.search("c"));//2
System.out.println(s.search("z"));//-1
SET
===
* Set is the child interface of Collection interface.
* If we want to representb a group of indivisual object as a single entity where
duplicates are not allowed and insertion order is not preserved then we should go
for Set interface.
* It does not contains any new method,so that we have to use the Collection
interface methods only.
HASHSET
=======
* It is introduced in 1.2v of java.
* Here the insertion order is not preserved.
* Duplicates objects are not allowed,if we are trying to add any duplicate element
then it will not showing any compile time error or run time exception,it simply
returns false.
* Heterogenious objects are allowed.
* Null insertion is possible but only once.
CONSTRUCTOR OF HASHSET
======================
1-HashSet h1=new HashSet();
2-HashSet h1=new HashSet(int initialcapacity)
3-HashSet h1=new HashSet(Collection c)
METHODS OF HASHSET
==================
System.out.println(h1.add("a"));//false
System.out.println(h1);//[a, b, c, null, 10]
System.out.println(h1.size());//5
LINKEDHASHSET
=============
* It is the child interface of HashSet.
* LinkedHashSet is exactly same as HashSet.
* In LinkedHashSet insertion order is preserved.
System.out.println(h1.add("a"));//false
System.out.println(h1);//[null, a, b, c, 10]
System.out.println(h1.size());//5
SORTEDSET
=========
* It is the chid interface of set interface.
* If we want to represent a group of indivisual object as a single entity where
duplicates are not allowed and the elements are inserted according to some sorting
order then we should go for SortedSet.
* There are two types of sorting
METHODS OF SORTEDSET
====================
1- Object first()
2- Object last()
3- SortedSet headSet()
4- SortedSet tailSet()
TREESET
=======
* The underlying data structure is balanced tree.
* Duplicates objects are not allowed.
* Insertion order is not preserved hence it based on some sorting order.
* Heterogenious objects are not allowed, if we are trying to insert any duplicate
then it will throw an exception like ClassCastException.
* Null insertion is possyble but only once.
CONSTRUCTOR OF TREESET
======================
1-TreeSet t1=new TreeSet();
2-TreeSet t1=new TreeSet(Comparator c);
3-TreeSet t1=new TreeSet(Collection c);
h1.add(10);
System.out.println(h1);//ClassCastException
h1.add(null);
System.out.println(h1);//NullPoingterException
COMPARABLE INTERFACE
====================
* It is present in java.lang package.
* It contains only one method i.e "compareTo()" method.
ex
--
obj1.compareTo(obj2)
--------------------
COMPARATOR INTERFACE
=====================
* It is present in java.util package.
* It contains two mwthods i.e
Example:-
========
import java.util.*;
class Test
{
public static void main(String[] args)
{
TreeSet t1=new TreeSet(new MyComparator());
t1.add(10);
t1.add(15);
t1.add(35);
t1.add(7);
t1.add(20);
t1.add(8);
System.out.println(t1);//[35, 20, 15, 10, 8, 7]
}
}
class MyComparator implements Comparator
{
public int compare(Object obj1,Object obj2)
{
Integer i1=(Integer) obj1;
Integer i2=(Integer) obj2;
* If we are not passing any Comparator object then jvm will always calls compareTo()
which is meant for default natural sorting order(ascending order),hence in this case
the o/p is [7, 8, 10, 15, 20, 35].
*If we are passing Comparator Object Then jvm calls compare() of MyComparator class
which is meant for customized sorting order(descending order),hence in this case the
o/p is [35, 20, 15, 10, 8, 7].
MAP
===
* If we want to represent a group of objects as "key-value" pain then we should go
for map interface.
* In map both key and value are objects.
* Duplicates keys are not allowes but the value can be duplicates.
* Each key value pair is called entry.
* Map interface is not the child interface of collection interface so we can not
apply the collection interface methods here.
HASHMAP
=======
* It is the implemented class of Map interface.
* In hashset duplicate keys are not allowed but values can be duplicates.
* Insertion order is not preserved,it is based on the hashcode number of keys.
* Heterogenious objects are allowed for both key and value.
* Null is allowed for keys(only once) and for values(any number of times).
* It is suitable for searching operations.
* It Is introduced in 1.2v.
CONSTRUCTOR OF HASHMAP
======================
1-HashMap m=new HahMap();
2-HashMap m=new HahMap(int initialcapacity);
3-HashMap m=new HahMap(Map m);
System.out.println(m.get("103"));//ccc
System.out.println(m.remove("105"));
System.out.println(m);//{101=aaa, 102=bbb, 103=ccc, 104=ddd}
System.out.println(m.containsKey("101"));//true
System.out.println(m.containsValue("bbb"));//true
System.out.println(m.isEmpty());//false
System.out.println(m.size());//4
m.clear();
System.out.println(m);//{}
LINKEDHASHMAP
=============
* It is exactly same as HashMap except the following differences like
IDENTITYHASHMAP
===============
* It is exactly same as hashmap except the following differences like
m.put(i1,"aaa");
m.put(i2,"bbb");
System.out.println(m);//{10=bbb}
* In the case of identityhashmap jvm will always use " == " to identify
duplicate keys.whwich is meant for reference comparision.
IdentityHashMap m=new IdentityHashMap();
m.put(i1,"aaa");
m.put(i2,"bbb");
System.out.println(m);//{10=aaa, 10=bbb}
WEAKERHASHMAP
=============
* In the case of normal hashmap an object is not eligible for garbage collector even
it does not have any references.bcz the hashmap dominates the garvage collector.
* In the case of weakerhashmap if an object does not have any object reference then
it is eligible for garbage collector.bcz garbage collector dominates weakerhashmap.
===========================================