Collection_framework_Java_By_Kunal_sir
Collection_framework_Java_By_Kunal_sir
Collection framework
Iterator interface
Iterator interface provides the facility of iterating the elements.
1. Public Boolean hasNext():It returns true if the iterator hasmore
elements otherwise it returns false.
2. public Object next(): It returns the element and moves the cursor
pointer to thenext element.
This is the root interface for the entire collection framework. The
collection interface extends the iterable interface. Therefore, inherently,
all the interfaces and classes implement this interface. The main
functionality of this interface is to provide an iterator for the collections.
Therefore, this interface contains only one abstract method which is the
iterator. It returns the
Iterator iterator();
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
2. Collection Interface:
This interface extends the iterable interface and is implemented by all
the classes in the collection framework. This interface contains all the
basic methods which every collection has like adding the data into the
collection, removing the data, clearing the data, etc. All these methods are
implemented in this interface because these methods are implemented by
all the classes irrespective of their style of implementation. And also,
having these methods in this interface ensures that the names of the
methods are universal for all the collections. Therefore, in short, we can
say that this interface builds a foundation on which the collection classes
are implemented.
3. List Interface:
This is a child interface of the collection interface. This interface is
dedicated to the data of the list type in which we can store all the ordered
collection of the objects. This also allows duplicate data to be present in it.
This list interface is implemented by various classes
like ArrayList, LinkedList,Vector etc. Since all the subclasses implement
the list, we can instantiate a list object with any of these classes. For
example,
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
import java.util.*;
public class Test {
public static void main(String args[])
{
List list=new ArrayList();
//Adding object in arraylist
list.add(10);
list.add(20);
list.add(30);
list.add(10);
System.out.println(list);//[10,20,30,10]
Iterator itr=list.iterator();
while(itr.hasNext())
{
intx=(int)itr.next();
System.out.println(x);
}
//Using Stream Api
list.stream().forEach(i->{
System.out.print(i+" ");
});
}
}
B. LinkedList:
Linked List class is an implementation of the LinkedList data
structure which is a linear data structure where the elements are not
stored in contiguous locations and every element is a separate object with
a data part and address part. The elements are linked using pointers and
addresses. Each element is known as a node. Let’s understand the
LinkedList with the following example:
import java.util.*;
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
Iterator itr=list.iterator();
while(itr.hasNext())
{
int x=(int)itr.next();
System.out.println(x);
}
//Using Stream Api
list.stream().forEach(i->{
System.out.print(i+" ");
});
}
}
Output:
1
2
3
4
5
C. Vector:
A vector provides us with dynamic arrays in Java. Though, it may be
slower than standard arrays but can be helpful in programs where
lots of manipulation in the array is needed. This is identical to
ArrayList in terms of implementation. However, the primary
difference between a vector and an ArrayList is that a Vector is
synchronized and an ArrayList is non-synchronized. Let’s understand
the Vector with an example:
import java.io.*;
import java.util.*;
public class Test{
// Main Method
public static void main(String[] args)
{
// Declaring the Vector
Vector<Integer> list = newVector<Integer>();
// Appending new elements at
// the end of the list
for(inti = 1; i<= 5; i++)
{
list.add(i);
}
// Printing elements
System.out.println(list);
Iterator itr=list.iterator();
while(itr.hasNext())
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
{
int x=(int)itr.next();
System.out.println(x);
}
}
}
Task 1:
import java.util.*;
states.stream().forEach(city->{
System.out.println(city);
});
});
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
}
}
Task 2:
import java.util.*;
List<String>jh=newArrayList<>();
jh.add("rachi");
jh.add("lohardaga");
List<List<String>>india=newArrayList<List<String>>();
india.add(mh);
india.add(jh);
List<String> state1=newArrayList<>();
state1.add(" city 1");
state1.add(" city 2");
List<String> state2=newArrayList<>();
state2.add("rachi");
state2.add("lohardaga");
List<List<String>>usa=newArrayList<List<String>>();
usa.add(state1);
usa.add(state2);
List<List<List<String>>> word=newArrayList<List<List<String>>>();
word.add(india);
word.add(usa);
System.out.println("using iterator");
Iterator<List<List<String>>>itr= word.iterator();
while(itr.hasNext())
{
List<List<String>> list =itr.next();
Iterator<List<String>> itr1=list.iterator();
while(itr1.hasNext())
{
List<String>st= itr1.next();
Iterator<String> itr2=st.iterator();
while(itr2.hasNext())
{
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
// Main Method
Public static void main(String args[])
{
// Creating HashSet and
// adding elements
HashSet<String>hs = newHashSet<String>();
hs.add("abc");
hs.add("pqr");
hs.add("mno");
hs.add("xyz");
Iterator<String>itr = hs.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Output:
abc
mno
pqr
xyz
B. LinkedHashSet:
A LinkedHashSet is very similar to a HashSet. The difference is that this
uses a doubly linked list to store the data and retains the ordering of
the elements. Let’s understand the LinkedHashSet with an example:
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
import java.util.*;
// Main Method
public static void main(String args[])
{
// Creating LinkedHashSet and
LinkedHashSet<String>lhs =newLinkedHashSet<String>();
// adding elements
lhs.add("abc");
lhs.add("mno");
lhs.add("pqr");
lhs.add("xyz");
// Traversing elements
Iterator<String>itr = lhs.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Output:
abc
xyz
mno
pqr
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
TreeSet:
The TreeSet class uses a Tree for storage. The ordering of the elements is
maintained by a set using their natural ordering whether or not an explicit
comparator is provided. This must be consistent with equals if it is to
correctly implement the Set interface. It can also be ordered by a
Comparator provided at set creation time, depending on which
constructor is used. Let’s understand TreeSet with an example:
import java.util.*;
// Traversing elements
Iterator<String>itr = ts.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Output:
abc
mno
pqr
xyz
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
ArrayList LinkedList
3) An ArrayList class can act as a list only LinkedList class can act as a list and
because it implements List only. queue both because it implements List
and Deque interfaces.
5) The memory location for the elements The location for the elements of a
of an ArrayList is contiguous. linked list is not contagious.
ArrayList Vector
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
List Set
3)List allows us to add any number Set allows us to add at least one null value
of null values. in it.
5)We can get the element of a We cannot find the element from the Set
specified index from the list using based on the index because it doesn't
the get() method. provide any get method().
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
Comparable Comparator
2) Comparable affects the original class, i.e., Comparator doesn't affect the
the actual class is modified. original class, i.e., the actual class is
not modified.
5) We can sort the list elements of We can sort the list elements of
Comparable type Comparator type
by Collections.sort(List) method. by Collections.sort(List,
Comparator) method.
Comparable Program :
package comparable;
public class Student implements Comparable<Student> {
private int rollno;
private String name;
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
public String getName() {
return name;
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
}
publicvoidsetName(String name) {
this.name = name;
}
@Override
public int compare To(Student stu) {
// sort by rollno
// return this.rollno=stu.rollno;
// sort by name
Return this.name.compareTo(stu.name);
}
}
Package comparable;
import java.util.*;
Student s2=newStudent();
s2.setRollno(3);
s2.setName("Pqr");
Student s=newStudent();
s.setRollno(2);
s.setName("Abc");
Set<Student> set=newTreeSet<>();
set.add(s);
set.add(s1);
set.add(s2);
for(Studentstu:set)
{
System.out.println(stu.getName());
System.out.println(stu.getRollno());
}
}
}
Comparator Program
public class Student {
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
import java.util.Comparator;
@Override
public int compare(Student stu1, Student stu2) {
// TODO Auto-generated method stub
returnstu1.getRollno()-stu2.getRollno();
}
}
import java.util.Comparator;
@Override
public int compare(Student stu1, Student stu2) {
// TODO Auto-generated method stub
return stu1.getName().compareTo(stu2.getName());
}
}
import java.util.*;
Student s1 = newStudent();
s1.setRollno(1);
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
s1.setName("Xyz");
Set<Student>set = null;
Scanner sc = new Scanner(System.in);
Int ch = sc.nextInt();
if (ch == 1) {
set = new TreeSet<>(newSortByName());
}
elseif (ch == 2) {
set = new TreeSet<Student>(newSortByRollno());
}
set.add(s);
set.add(s1);
set.add(s2);
5. Map Interface
The map interface is present in java.util package represents a
mapping between a key and a value. The Map interface is not a subtype of
the Collection interface. Therefore it behaves a bit differently from the rest
of the collection types. A map contains unique keys.
Maps are perfect to use for key-value association mapping such as
dictionaries. The maps are used to perform lookups by keys or when
someone wants to retrieve and update elements by keys. Some common
scenarios are as follows:
A map of error codes and their descriptions.
A map of zip codes and cities.
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
Output:
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732
By Kunal Sir
by using Iterator
Key : 1 value : java
Key : 2 value : C
Key : 3 value : C++
1st Floor, Abov e Rupam Sweets, Priyanka Collections Building, Near Karv enagar Stop,
Karv enagar, Pune-52.Call:+91 8888022204.+91 7066970732