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

Collection Notes - PDF

The document discusses the Collection framework in Java, which represents a group of objects as a single entity. It describes 9 key interfaces of the framework including Collection, List, Set, SortedSet, Queue, Map, SortedMap and NavigableMap. It then provides details about the Collection interface and List interface, their common methods, and the ArrayList class.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Collection Notes - PDF

The document discusses the Collection framework in Java, which represents a group of objects as a single entity. It describes 9 key interfaces of the framework including Collection, List, Set, SortedSet, Queue, Map, SortedMap and NavigableMap. It then provides details about the Collection interface and List interface, their common methods, and the ArrayList class.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

COLLECTIONfRAMEWORK

--------------------
It is the collection of classes and interfaces to represent a group of object as a
single entity.

9 KEY INTERFACES OF COLLECTIONfRAMEWORK


----------------------------------------
1-Collection
2-List
3-Set
4-SortedSet
5-Navigableset
6-Queue
7-Map
8-SortedMap
9-NavigableMap

Collection
============
1-If we want to represent a group of indivisual object as a single entity then we
should go for COLLECTION interface.

METHODS OF COLLECTION INTERFACE


================================
1-boolean add(Object o)
-------------------------
* It is used to add an element in Collection.
* It returns true if the element is added successfully and it returns false if the
element not added successfully.

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

System.out.println(l1);//[aaa, bbb, ccc]


}
}

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]

ArrayList l2=new ArrayList();


System.out.println(l2);//[]
l2.addAll(l1);
System.out.println(l2);//[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]

ArrayList l2=new ArrayList();


System.out.println(l2);//[]
l2.addAll(l1);
System.out.println(l2);//[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]
}
}

2-boolean add(int index,Object o)


----------------------------------
* It is used to add element at a specified index.

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]
}
}

3-boolean addAll(int index, Collection c)


------------------------------------------
* It is used to add a group of element at a specified index.

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]

ArrayList l2=new ArrayList();


l2.add("ddd");
l2.add("eee");
l2.add("fff");
System.out.println(l2);//[aaa, bbb, ccc]

l1.addAll(2,l2);
System.out.println(l1);//[aaa, bbb, ddd, eee, fff, ccc]
}
}

4-Object get(int index)


------------------------
* It is used to retrive the element present in a specified index.

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
}
}

5-Object remove(int index)


---------------------------
* It is used to remove an element to a specified index.

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]
}
}

6- Object set(int index,Object o)


----------------------------------
* It is used to replace the element present at a specified index.

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();

* The default capacity of ArrayList is 10.


* If we reach the the max capacity then the new capacity will be like

new capacity=(current capacity*3/2) + 1


........................................

2-ArrayList l1=new ArrayList(int initialcapacity);

* Create an ArrayList with a Specified Size.


* ex:-ArrayList l1=new ArrayList(5);

3-ArrayList l1=new ArrayList(Collection c);

* Creats an equivalent ArrayList Object for the given collection

* 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.size());//4

ArrayList l2=new ArrayList(l1);


System.out.println(l2.size());//4

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();

* It is used to create an empty LinkedList object

2-LinkedList l1=new LinkedList(Collection c);

* It is used to create an equivalent LinkedList object.

METHODS OF LINKEDLIST
======================
1-add(Object 0)
2-addFirst(Object o)
3-addLast(Object o)
4-removeFirst()
5-removeLast()

LinkedList l1=new LinkedList();


l1.add("a");
l1.add("10");
l1.add("b");
l1.add("10.5");
l1.add("a");
l1.add("null");
System.out.println(l1);//[a, 10, b, 10.5, a, null]

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();

* The default capacity of vector is 10

* If we reach the the max capacity then the new capacity will be like

New capacity=Initial capacity * 2


..................................

2-Vector v=new Vector(int initialcapacity);

3-Vector v=new Vector(Collection c);

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

* Stack s=new Stack();

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
==================

HashSet h1=new HashSet();


h1.add("null");
h1.add("a");
h1.add("b");
h1.add("c");
h1.add("10");
System.out.println(h1);//[a, b, c, null, 10]

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.

LinkedHashSet h1=new LinkedHashSet();


h1.add("null");
h1.add("a");
h1.add("b");
h1.add("c");
h1.add("10");
System.out.println(h1);//[null, a, b, c, 10]

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

1- Default Natural Sorting(ASCENDING)


2- Customized Sorting(DESCENDING)

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);

TreeSet h1=new TreeSet();


h1.add("a");
h1.add("b");
h1.add("c");
h1.add("d");
h1.add("e");
System.out.println(h1);//[a, b, c, d, e]

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.

syn-> public int compareTo(Object o)

ex
--
obj1.compareTo(obj2)
--------------------

-> it returns -ve if obj1 has to be come before obj2.


-> it returns +ve if obj1 has to be come after obj2.
-> it returns 0(zero) if obj1 and obj2 are equal.

TreeSet t1=new TreeSet();


t1.add(10);
t1.add(15);
t1.add(35);
t1.add(7);
t1.add(20);
t1.add(8);
System.out.println(t1);//[7, 8, 10, 15, 20, 35]

COMPARATOR INTERFACE
=====================
* It is present in java.util package.
* It contains two mwthods i.e

1:-public int compare()

-> it returns -ve if obj1 has to be come before obj2.


-> it returns +ve if obj1 has to be come after obj2.
-> it returns 0(zero) if obj1 and obj2 are equal.

2:-public boolean equals()

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(i1 < i2)


return +1;
else if(i1 > i2)
return -1;
else
return 0;
}
}

* 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.

METHODS OF MAP INTERFACE


========================
1-Object put(Object key,Object value);
->it is used to insert key value pair/one entry in to the map.
2-void putAll(Map m);
->it is used to insert a group of entry in to the map.
3-Object get(Object key);
->it is used to get the Object on a particular key.
4-Object remove(Object key);
->->it is used to remove the Object on a particular key.
5-boolean containsKey(Object key);
6-boolean containsValue(Object value);
7-boolean isEmpty();
8-int size();
9-void clear();

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);

HashMap m=new HashMap();


m.put("101","aaa");
m.put("102","bbb");
m.put("103","ccc");
m.put("104","ddd");
m.put("105","eee");
System.out.println(m);//{101=aaa, 102=bbb, 103=ccc, 104=ddd,
105=eee}

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

->insertion order is preserved.


->it is introduced in 1.4v.

IDENTITYHASHMAP
===============
* It is exactly same as hashmap except the following differences like

* In the case of hashmap jvm will always use "equals()" to identify


duplicate keys.which is meant for content cpmparision.

HashMap m=new HashMap();

Integer i1=new Integer(10);


Integer i2=new Integer(10);

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();

Integer i1=new Integer(10);


Integer i2=new Integer(10);

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.

IMPORTANT QUESTIONS OF COLLECTION


FRAMEWORK

===========================================

01- What is Collection Framework in java ?


02- What is Collection in java ?
03- What is Collections in java ?
04- What is the difference between Collection and Collections ?
05- what are the key interfaces of Collection ?
06- What is List ?
07- What is ArrayList ?
08- What is LinkedList ?
09- What is the difference between ArrayList and LinkedList ?
10- What is Stack ?
11- What is Vector ?
12- What is the difference Between ArrayList and Vector ?
13- What is LEGACY class ?
14- What is UTILITY class ?
15- What is Set ?
16- What is TreeSet ?
17- What is Map ?
18- What is the difference between HashMap and LinkedHashMap ?
19- What is the difference between HashMap and IdentityHashMap ?
20- What is the difference between HashMap and WeakerHashMap ?
21- What is Comparable inyerface explain briefly ?
22- What is Comparator inyerface explain briefly ?
23- What is the dihherence between HashSet and LinkedHashSet ?
24- Which method is used to insert an object in to the Stack ?
25- Why Collection Came to the picture explain briefly ?

You might also like