Collections
Collections
Collection Programs
1. What is Collection?
Ans
2. What is Framework ?
Ans
3. Why we use collection and framework ? What is the purpose of collection and
frameworks ?
Ans
With respect to variables in order to store one data one container is needed,
Hence to store n number of data n number of container is needed this becomes
costly operation. Hence we go for Arrays where it is possible to store n number
data in one container. but still the array size is fixed and array is homogenous
and does not have inbuilt method to perform data operation so hence we go for
collection frameworks.
4. What are the Classes and Interface present in Collection ? What is collection
Hiererchy ?
Collections 1
And
Ans
There are 11 abstract methods present in collection Interface:
add() :
addAll() :
method used to fetches all the object from one Collection and insert it
into the another Collection.
contains() :
containsAll() :
method is used to check if all the object are present from a particular
nested Collection or not.
remove() :
removeAll() :
clear() :
isEmpty() :
Collections 2
method is used to check if the Collection is empty or not.
size() :
iterator() :
toArray() :
6. What is List ?
Ans
7. What is ArrayList ?
Ans
3 Overloaded Constructors:
[1.ArrayList(), 2.ArrayList(int initialcapacity), 3.ArrayList(collection c).
Collections 3
ArrayList impleaments List, RandomAccess, Cloneable and Serializable
Interfaces.
Array-List is good for data retrieval & search operation . Because time taken
to search any data in entire List is same.
we use for better performance but it is not thread safe because of mult-
threaded
If we try to add the data in between the list then all the existing data gets
shifted to next position , so because of this shift operation the performance
becomes slow
Example Program
import java.util.*;
public class Arraylist1
{
public static void main(String[] args)
{
ArrayList a1=new ArrayList();
a1.add("Rahul");
a1.add("Virat");
a1.add("Rohit");
a1.add("MSD");
a1.add("Rishab");
System.out.println(a1);
System.out.println(a1.contains("Virat"));
a1.remove("Rahul");
System.out.println(a1);
System.out.println(a1.size());
System.out.println(a1.isEmpty());
a1.clear();
System.out.println(a1);
}
}
OUTPUT:
[Rahul, Virat, Rohit, MSD, Rishab]
true
[Virat, Rohit, MSD, Rishab]
4
Collections 4
false
[]
8. What is Vector ?
Ans
Vector is a part of Collections and it is a child class of List Interface and present
in java.util.Vector package since JDK1.0
4 Overloaded Constructors:
[1.Vector(),2.Vector(int initialcapacity),3.Vector(int initialcapacity,int
increamentalcapacity)] 4.Vector(collection c).
Vector is good for data retrieval & search operation . Because time taken to
search any data in entire List is same.
If we try to add the data in between the list then all the existing data gets
shifted to next position , so because of this shift operation the performance
becomes slow
Example Program
Collections 5
import java.util.*;
public class vector1
{
public static void main(String[] args)
{
Vector a1=new Vector();
a1.add("Rahul");
a1.add("Virat");
a1.add("Rohit");
a1.add("MSD");
a1.add("Rishab");
System.out.println(a1);
System.out.println(a1.contains("Virat"));
a1.remove("Rahul");
System.out.println(a1);
System.out.println(a1.size());
System.out.println(a1.isEmpty());
a1.clear();
System.out.println(a1);
}
}
OUTPUT:
[Rahul, Virat, Rohit, MSD, Rishab]
true
[Virat, Rohit, MSD, Rishab]
4
false
[]
Ans
10. What is the major problem we are going to face in Array-List and Vector ?
Ans
Collections 6
Whenever we add or delete in between two elements with respect to ArrayList
and Vector Shifting operation takes place where it will leads to decrease in the
performance hence the solution is LinkedList.
Ans
LinkedList doesn’t have any shift operation , hence it is suitable for insertion
or removal of data in between.
LinkedList is not suitable for any searching operation. because the control
has to start through first node
Example Program
import java.util.*;
public class Linkedlist1 {
Collections 7
public static void main(String[] args)
{
LinkedList a1=new LinkedList();
a1.add("Rahul");
a1.add("Virat");
a1.add("Rohit");
a1.add("MSD");
a1.add("Rishab");
System.out.println(a1);
System.out.println(a1.contains("Virat"));
a1.remove("Rahul");
System.out.println(a1);
System.out.println(a1.size());
System.out.println(a1.isEmpty());
a1.clear();
System.out.println(a1);
}
}
OUTPUT:
[Rahul, Virat, Rohit, MSD, Rishab]
true
[Virat, Rohit, MSD, Rishab]
4
false
[]
Ans
Ans
Singly LinkedList
Doubly LinkedList
the first node does not have previous node address and last node does not
have next node address.
Collections 8
Ans
For a collection if the generic are not specified then by default the generics will
be object.
with respect to list get() method is used in order to fetch the data.
Example Program
import java.util.*;
public class Generic_example
{
public static void main(String[] args)
{
ArrayList <String> al=new ArrayList();
al.add("Java");
al.add("SQL");
al.add("Programimg");
al.add("Web-Script");
for(int i=0; i<al.size(); i++)
{
System.out.println(al.get(i));
}
}
}
OUTPUT:
Java
SQL
Programimg
Web-Script
Ans
Ans
Collections 9
17. Difference Between List and Set?
Ans
Ans
Ans
Collections 10
Duplicate Insertion is not Allowed.
Example program
import java.util.*;
public class Hashset1
{
public static void main(String[] args)
{
HashSet hs=new HashSet();
hs.add("Rahul");
hs.add("Virat");
hs.add("Rohit");
hs.add("MSD");
hs.add("Rishab");
System.out.println(hs);
System.out.println(hs.contains("Virat"));
hs.remove("Rahul");
System.out.println(hs);
System.out.println(hs.size());
System.out.println(hs.isEmpty());
hs.clear();
System.out.println(hs);
}
}
OUTPUT:
[Rahul, Rohit, Rishab, Virat, MSD]
true
[Rohit, Rishab, Virat, MSD]
4
false
[]
Ans
Collections 11
LinkedHashSet internally stores the object in the form of Hashtable+7Doubly
Linkedlist
Example program
import java.util.*;
public class Linkedhashset2 {
public static void main(String[] args)
{
LinkedHashSet hs=new LinkedHashSet();
hs.add("Rahul");
hs.add("Virat");
hs.add("Rohit");
hs.add("MSD");
hs.add("Rishab");
System.out.println(hs);
System.out.println(hs.contains("Virat"));
hs.remove("Rahul");
System.out.println(hs);
System.out.println(hs.size());
System.out.println(hs.isEmpty());
hs.clear();
System.out.println(hs);
}
}
OUTPUT:
[Rahul, Virat, Rohit, MSD, Rishab]
true
[Virat, Rohit, MSD, Rishab]
Collections 12
4
false
[]
Ans
TreeSet is mainly used for sorting purpose. TreeSet follows default natural
sorting order(Ascending order).
TreeSet does not accepts Single Null value. If Null is added we will get
nullpointerexception.
It has a one Constructor that will takes Comparator Object. It is used for sorting
purpose.
Example Program
Collections 13
import java.util.*;
public class Treeset1 {
public static void main(String[] args)
{
TreeSet f1=new TreeSet();
f1.add(10);
f1.add(7);
f1.add(53);
f1.add(100);
f1.add(32);
f1.add(2);
System.out.println(f1);
}
}
OUTPUT:
[2, 7, 10, 32, 53, 100]
Ans
1. HashSet
2. LinkedHashSet
3. HashMap
4. LinkedHashMap
5. HashTable
Ans
Collections 14
Map Interface has nested Interface i,e Entry Interface. Inside Entry Interface 2
method are there 1)getKey(); 2)getValue();
Ans
Entry is basically a data which is available in the form of “key” and “value” pair.
where key must be unique value can be duplicated.
Ans
abstract method present in Map Interface
put() :
get() :
containsKey() :
containsValue() :
size() :
Collections 15
method is used to define the current number of entries present in the Map.
isEmpty() :
remove() :
method is used to delete a particular entry from Map from based on key.
clear() :
entrySet() :
method is used to fetch all the key and value pair from the Map.
keySet() :
values() :
putAll() :
method is used to fetches all the entries from one Map and put it into
another Map.
Example Program
import java.util.*;
public class Map1
{
public static void main(String[] args)
{
HashMap <Integer,String> hm=new HashMap <Integer,String>();
hm.put(7,"MSD");
hm.put(18,"Virat");
hm.put(45,"Rohith");
hm.put(null,null);
hm.put(45,"Raina");
hm.put(1,"Virat");
System.out.println(hm.get(1));
Collections 16
System.out.println(hm);
System.out.println(hm.containsKey(1));
System.out.println(hm.containsValue("Virat"));
System.out.println(hm.size());
}
}
OUTPUT:
Virat
{null=null, 1=Virat, 18=Virat, 7=MSD, 45=Raina}
true
true
5
Ans
Iterator is Cursors.
hasNext() :
next() :
Ans
hasPrevious() :
previous() :
Collections 17
28. What is HashMap ?
Ans
Example Program
Ans
Collections 18
LinkedHashMap ()
LinkedHashMap (Collection c)
LinkedHashMap (int initialCapacity, float fillratio)
Example Program
Ans
It is a single threaded
Default capacity is 1.
Example Program
Collections 19
Ans
It has a one Constructor that will takes Comparator Object. It is used for sorting
purpose.
Ans
Ans
Ans
Collections 20
35. Difference between HashMap and HashSet?
Ans
36. Difference between HashMap with HashTable and HashMap with LinkedHashMap ?
Ans
Ans
Collections 21
Ans
Ans
Ans
If we want do priority task then we will queue and it is used in O.S (real time
example)
Ans
Ans
Inorder
Ans
Comparable Interface
Collections 22
3. If we have access to the class, we go with comparable.
Program
//comparable
public class Employee implements Comparable
{
int id;
String name;
double sal;
public Employee(int id,String name,double sal)
{
this.id=id;
this.name=name;
this.sal=sal;
}
@Override
public String toString()
{
return this.id+" "+this.name+" "+this.sal;
}
Collections 23
System.out.println(tr.next());
}
sc.close();
}
@Override
public int compareTo(Object o)
{
Employee emp=(Employee)o;
return -((Double)this.sal).compareTo(emp.sal);
}
}
Comparator interface
2. In this way Comparator will sort the elements by overriding compare() and
equals() methods.
Program
//comparator
public class Employee2
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Comparator com=new Comparator()
{
@Override
public int compare(Object o1,Object o2)
{
Employee e1=(Employee) o1;
Employee e2=(Employee) o2;
return ((Double)e1.sal).compareTo(e2.sal);
}
};
ArrayList al=new ArrayList();
while(true)
{
int id=sc.nextInt();
Collections 24
String name=sc.next();
double sal=sc.nextDouble();
Employee emp=new Employee(id,name,sal);
al.add(emp);
System.out.println("More object are There?");
String s=sc.next();
if(s.equalsIgnoreCase("no"))
break;
}
Collections.sort(al,com);
ListIterator itr=al.listIterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
5. We can sort elements using comparable and comparator in only Array-List and
Vector. We connect use Comparable and Comparator in hash-based because
data structure is in node form. this is the main advantages of comparable and
comparator.
7. Collection.sort(list,Comparator object)—>List.sort(list,Com)—>Comparator—
>compare()—>compareTo().
Ans
Collections 25
45. What is get() method ?
Ans
get() method is in List Interface. Used to get the elements present in this list at a
given specific index.
We cannot use get() method in Set because get() is not present in Set Interface.
Ans
Ans
Advantages
For any reason a user wishes to store multiple values of similar type then
the Array can be used and utilized efficiently
Dis-Advantages
Collections 26
Array size is fixed:
The array is static, which means its size is always fixed. The memory
which is allocated to it cannot be increased or decreased.
Ans
Complie Time Error.
Ans
Ans
Ans
Ans
Addition or deletion of data from the middle is time consuming as data needs to
be shifted to update the list.
Resizing of an arraylist when it reaches it's capacity with it initial capacity which
is 10 is a costlier process as the elements will be copied from old to new space
with 50% more capacity.
53. Can we possible to write null insertion inside TreeSet and HashSet ?
Ans
TreeSet does not accepts Single Null value. If Null is added we will get
nullpointerexception.
Collections 27
54. What is the Data Structure of ArrayList, LinkedList, Vector and TreeSet ?
Ans
Ans
Ans
Ans
top(): is a method in Java is used Returns the top element of the stack.
isEmpty(): is a method in Java is used returns true is stack is empty else false
Ans
Collections 28
The stack is a linear data structure that is used to store the collection of objects.
It is based on Last-In-First-Out (LIFO).
Ans
Ans
Ans
Ans
Ans
Ans
Ans
Ans
Collections 29