I need to know about complete topic in java's collections i
with an examples
Answer / vikneswarank
COLLECTION
1. Collecion 2. Set - HashSet
3. Set - TreeSet 4. List - ArrayList
5. List - Vector 6. List - LinkedList
7. Map - HashMap 8. Map - Hashtable
9. Map - Properties
Collection sample
--------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.TreeSet;
import java.util.Vector;
/**
* @author Vikneshwaran
* @version 1.0
* #################### Template #####################
* class class_name
* {
* Collection coll_obj1 = new HashSet();
* Collection coll_obj2 = new TreeSet();
* Collection coll_obj3 = new ArrayList();
* Collection coll_obj4 = new Vector();
* Collection coll_obj5 = new LinkedList();
*
* coll_obj1.add(...);
* coll_obj1.remove(...);
* coll_obj1.contains(...);
* int size = coll_obj1.size();
* Iterator itr_obj = coll_obj1.iterator();
* Object[] obj = coll_obj1.toArray();
* }
*
* ####################################################
*/
//1. Collection allows duplicate value
//2. Collection allows null value
//3. Collection allows different type of data types in
single object
//4. Not synchronized one
public class CollectionSample1
{
public static void main (String args[])
{
//since collection is interface, object cannt be
created as follows
//Collection listObj = new Collection();
//using implemented class like ArrayList, Vector &
LinkedList, can do
Collection coll_obj1 = new HashSet();
Collection coll_obj2 = new TreeSet();
Collection coll_obj3 = new ArrayList();
Collection coll_obj4 = new Vector();
Collection coll_obj5 = new LinkedList();
//cannt do
//Collection arrlist_obj2 = new ArrayList(10);
//Collection arrlist_obj2 = new ArrayList(10, 2);
//collection interface(Set, List) and Map interface
allows object only,
//primitive value cannt be added as follows
/*
int a = 50;
double b = 10.00;
long c = 100;
coll_obj1.add(10);
coll_obj1.add(10.00);
coll_obj1.add(test);
coll_obj1.add(a);
coll_obj1.add(b);
coll_obj1.add(c);
*/
//but can add as follows
int a = 40;
double b = 50.00;
long c = 60;
String d = "test3";
coll_obj1.add(new Integer(10));
coll_obj1.add(new Double(20.00));
coll_obj1.add(new Long(30));
coll_obj1.add("test1");
coll_obj1.add(new String("test2"));
coll_obj1.add(new Integer(a));
coll_obj1.add(new Double(b));
coll_obj1.add(new Long(c));
coll_obj1.add(d);
//collection allows duplicate value
coll_obj1.add("test4");
coll_obj1.add("test4");
//collection allows null value
coll_obj1.add(null);
//remove the entry
coll_obj1.remove("test2");
coll_obj1.remove(new Integer(10));
coll_obj1.remove(new Double(b));
//check the entry is already there or not
boolean cont = coll_obj1.contains("test4");//return
boolean true or false
//remove all the entry
//coll_obj1.clear();
//size
int size = coll_obj1.size();
System.out.println("Size of collection is " + size);
//to get or display all the entry
//1. using iterator
Iterator itr = coll_obj1.iterator();
while (itr.hasNext())
{
//if all the entry are same data type, then can
typecast and store in varibale as follows
//String entry = (String) itr.next();
//Double entry = (Double) itr.next();
//System.out.println("Iterator : Value = " + entry);
//if all the entry are different, then can store it
as follows
//Object obj = itr.next();
//System.out.println("Iterator : Value = " + obj);
System.out.println("Iterator : Value = " + itr.next());
}
//2. using toArray
Object[] obj = coll_obj1.toArray();
for (int i = 0; i < coll_obj1.size(); i++)
{
System.out.println("toArray : Value = " + obj[i]);
}
Collection collObj2 = new Vector();
collObj2.add("newtest");
//add all the entry from coll_obj1 to collObj2.
collObj2.addAll(coll_obj1);
coll_obj1.remove("test1");
coll_obj1.remove("test3");
//remove all entry of coll_obj1 in collObj2.
collObj2.removeAll(coll_obj1);
}
}
Set - HashSet sample
--------------------------------------------------------------------------------
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* @author Vikneshwaran
* @version 1.0
* #################### Template #####################
* class class_name
* {
* Set set_obj1 = new HashSet();
* HashSet hashset_obj1 = new HashSet();
*
* hashset_obj1.add(...);
* hashset_obj1.remove(...);
* hashset_obj1.contains(...);
* int size = hashset_obj1.size();
* Iterator itr_obj = hashset_obj1.iterator();
* Object[] obj = hashset_obj1.toArray();
* }
*
* ####################################################
*/
//1. HashSet doesnt allows duplicate value
//2. HashSet allows null value
//3. HashSet allows different type of data types in single
object
//4. Not synchronized one
public class SetSample1
{
public static void main (String args[])
{
//since set is interface object cannt be created as
follows
//Set setObj = new Set();
//using implemented class like HashSet and TreeSet,
can do
Set setObj1 = new HashSet(); //dynamic binding or
runtime polymorphim
HashSet hashsetObj1 = new HashSet();
//also can do
HashSet hashset_obj1 = new HashSet(10);//after size is
full, it will be increased to double size
HashSet hashset_obj2 = new HashSet(10, 2);//after size
is full, it will be increased by 2
//collection interface(Set, List) and Map interface
allows object only,
//primitive value cannt be added as follows
/*
int a = 50;
double b = 10.00;
long c = 100;
hashsetObj2.add(10);
hashsetObj2.add(10.00);
hashsetObj2.add(test);
hashsetObj2.add(a);
hashsetObj2.add(b);
hashsetObj2.add(c);
*/
//but can add as follows
int a = 40;
double b = 50.00;
long c = 60;
String d = "test3";
hashsetObj1.add(new Integer(10));
hashsetObj1.add(new Double(20.00));
hashsetObj1.add(new Long(30));
hashsetObj1.add("test1");
hashsetObj1.add(new String("test2"));
hashsetObj1.add(new Integer(a));
hashsetObj1.add(new Double(b));
hashsetObj1.add(new Long(c));
hashsetObj1.add(d);
//hashset doesnt allows duplicate value, can add as
follows but it will store single entry only
hashsetObj1.add("test4");
hashsetObj1.add("test4");
//hashset allows null value
hashsetObj1.add(null);
//remove the entry
hashsetObj1.remove("test2");
hashsetObj1.remove(new Integer(10));
hashsetObj1.remove(new Double(b));
//check the entry is already there or not
boolean cont = hashsetObj1.contains("test4");//return
boolean true or false
//remove all the entry
//hashsetObj2.clear();
//size
int size = hashsetObj1.size();
System.out.println("Size of hashset is " + size);
//to get or display all the entry in hashset
//1. using iterator
Iterator itr = hashsetObj1.iterator();
while (itr.hasNext())
{
//if all the entry are same data type, then can
typecast and store in varibale as follows
//String entry = (String) itr.next();
//Double entry = (Double) itr.next();
//System.out.println("Iterator : Value = " + entry);
//if all the entry are different, then can store it
as follows
//Object obj = itr.next();
//System.out.println("Iterator : Value = " + obj);
System.out.println("Iterator : Value = " + itr.next());
}
//2. using toArray
Object[] obj = hashsetObj1.toArray();
for (int i = 0; i < hashsetObj1.size(); i++)
{
System.out.println("toArray : Value = " + obj[i]);
}
HashSet hashsetObj2 = new HashSet();
hashsetObj2.add("newtest");
//add all the entry from hashsetObj1 to hashsetObj2.
hashsetObj2.addAll(hashsetObj1);
hashsetObj1.remove("test1");
hashsetObj1.remove("test3");
//remove all entry of hashsetObj1 in hashsetObj2.
hashsetObj2.removeAll(hashsetObj1);
}
}
Set - TreeSet sample
--------------------------------------------------------------------------------
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
/**
* @author Vikneshwaran
* @version 1.0
* #################### Template #####################
* class class_name
* {
* Set set_obj1 = new TreeSet();
* TreeSet treeset_obj1 = new TreeSet();
*
* treeset_obj1.add(...);
* treeset_obj1.remove(...);
* treeset_obj1.contains(...);
* int size = treeset_obj1.size();
* Iterator itr_obj = treeset_obj1.iterator();
* Object[] obj = treeset_obj1.toArray();
* }
*
* ####################################################
*/
//1. TreeSet allows duplicate value
//2. TreeSet doesnt allows null value
//3. TreeSet doesnt allows different type of data types in
single object
//4. Not synchronized one
//5. stores object or entry in sorted order
public class SetSample2
{
public static void main (String args[])
{
//since set is interface, object cannt be created as
follows
//Set setObj = new Set();
//using implemented class like HashSet and TreeSet,
can do.
Set setObj1 = new TreeSet(); //dynamic binding or
runtime polymorphim
TreeSet treesetObj1 = new TreeSet();
//cannt do
//TreeSet treeset_obj1 = new TreeSet(10);
//TreeSet treeset_obj2 = new TreeSet(10, 2);
//collection interface(Set, List) and Map interface
allows object only,
//primitive value cannt be added as follows
/*
int a = 50;
double b = 10.00;
long c = 100;
treesetObj1.add(10);
treesetObj1.add(10.00);
treesetObj1.add(test);
treesetObj2.add(a);
treesetObj2.add(b);
treesetObj2.add(c);
*/
//but can add as follows
/*
int a = 40;
double b = 50.00;
long c = 60;
String d = "test3";
treesetObj1.add(new Integer(10));
treesetObj1.add(new Double(20.00));
treesetObj1.add(new Long(30));
treesetObj1.add("test1");
treesetObj1.add(new String("test2"));
treesetObj1.add(new Integer(a));
treesetObj1.add(new Double(b));
treesetObj1.add(new Long(c));
treesetObj1.add(d);
*/
//since it doesnt allows different data type in single
objet
//if string object added first then other string
object should be added
treesetObj1.add("test2");
//suppose next entry is added as follows,
//it will through exception during runtime, since
string object is added already
//treesetObj1.add(new Integer(10));
treesetObj1.add("newtest1");
//treeset doesnt allows duplicate value, can add as
follows but it will store single entry only
treesetObj1.add("test1");
treesetObj1.add("test1");
treesetObj1.add("test4");
treesetObj1.add("test3");
//treeset doesnt allows null value
//treesetObj1.add(null);
//remove the entry
treesetObj1.remove("test1");
//check the entry is already there or not
boolean cont = treesetObj1.contains("test4");//return
boolean true or false
//remove all the entry
//treesetObj1.clear();
//size
int size = treesetObj1.size();
System.out.println("Size of treeset is " + size);
//to get or display all the entry in hashset
//1. using iterator
Iterator itr = treesetObj1.iterator();
while (itr.hasNext())
{
//if all the entry are same data type, then can
typecast and store in varibale as follows
//String entry = (String) itr.next();
//Double entry = (Double) itr.next();
//System.out.println("Iterator : Value = " + entry);
//if all the entry are different, then can store it
as follows
//Object obj = itr.next();
//System.out.println("Iterator : Value = " + obj);
System.out.println("Iterator : Value = " + itr.next());
}
//2. using toArray
Object[] obj = treesetObj1.toArray();
for (int i = 0; i < treesetObj1.size(); i++)
{
System.out.println("toArray : Value = " + obj[i]);
}
TreeSet treesetObj2 = new TreeSet();
treesetObj2.add("newtest");
//add all the entry from treesetObj1 to treesetObj2.
treesetObj2.addAll(treesetObj1);
treesetObj1.remove("test1");
treesetObj1.remove("test2");
//remove all entry of treesetObj1 in treesetObj2.
treesetObj2.removeAll(treesetObj1);
}
}
List - ArrayList sample
--------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author Vikneshwaran
* @version 1.0
* #################### Template #####################
* class class_name
* {
* List list_obj1 = new ArrayList();
* ArrayList arrlist_obj1 = new ArrayList();
*
* arrlist_obj1.add(...);
* arrlist_obj1.remove(...);
* arrlist_obj1.contains(...);
* int size = arrlist_obj1.size();
* Iterator itr_obj = arrlist_obj1.iterator();
* Object[] obj = arrlist_obj1.toArray();
*
* arrlist_obj1.add(.., ..);
* arrlist_obj1.set(.., ..);
* arrlist_obj1.get(...);
* arrlist_obj1.indexOf(...);
* }
*
* ####################################################
*/
//1. ArrayList allows duplicate value
//2. ArrayList allows null value
//3. ArrayList allows different type of data types in single
object
//4. Not synchronized one
//5. stores the values in indexed way.
public class ListSample1
{
public static void main (String args[])
{
//since list is interface, object cannt be created as
follows
//List listObj = new List();
//using implemented class like ArrayList, Vector &
LinkedList, can do
List listObj1 = new ArrayList(); //dynamic binding or
runtime polymorphim
ArrayList arrlistObj1 = new ArrayList();
//also can do
ArrayList arrlist_obj1 = new ArrayList(10);//after
size is full, it will be increased to double size
//cannt do
//ArrayList arrlist_obj2 = new ArrayList(10, 2);
//collection interface(Set, List) and Map interface
allows object only,
//primitive value cannt be added as follows
/*
int a = 50;
double b = 10.00;
long c = 100;
arrlistObj1.add(10);
arrlistObj1.add(10.00);
arrlistObj1.add(test);
arrlistObj1.add(a);
arrlistObj1.add(b);
arrlistObj1.add(c);
*/
//but we can add as follows
int a = 40;
double b = 50.00;
long c = 60;
String d = "test3";
arrlistObj1.add(new Integer(10));
arrlistObj1.add(new Double(20.00));
arrlistObj1.add(new Long(30));
arrlistObj1.add("test1");
arrlistObj1.add(new String("test2"));
arrlistObj1.add(new Integer(a));
arrlistObj1.add(new Double(b));
arrlistObj1.add(new Long(c));
arrlistObj1.add(d);
//arraylist allows duplicate value
arrlistObj1.add("test4");
arrlistObj1.add("test4");
//arraylist allows null value
arrlistObj1.add(null);
//remove the entry from arraylist
arrlistObj1.remove("test2");
arrlistObj1.remove(new Integer(10));
arrlistObj1.remove(new Double(b));
//check the entry is already there or not
boolean cont = arrlistObj1.contains("test4");//return
boolean true or false
//remove all the entry
//arrlistObj1.clear();
//some list interface's method
//can add or replace value to particular index.
//index shouldnt exceeds actual arraylist size
//if arraylist's size is 20, then can add or replace
value from 0 to 19th index,
//cannt add or replace value for more than 19th index
//arrlistObj1.add(22, "newtest1");
//arrlistObj1.aet(25, "newtest1");
arrlistObj1.add(1, "newtest1");
arrlistObj1.set(3, "newtest2");
//some list interface's method
//index should not exceeds arraylist size
System.out.println("value in 3rd position is " +
arrlistObj1.get(3));
//some list interface's method
//can get the index of particular value in arraylist
System.out.println("Index of test1 is " +
arrlistObj1.indexOf("test1"));
//size
int size = arrlistObj1.size();
System.out.println("Size of arraylist is " + size);
//to get or display all the entry in arraylist
//1. using iterator
Iterator itr = arrlistObj1.iterator();
while (itr.hasNext())
{
//if all the entry are same data type, then can
typecast and store in varibale as follows
//String entry = (String) itr.next();
//Double entry = (Double) itr.next();
//System.out.println("Iterator : Value = " + entry);
//if all the entry are different, then can store it
as follows
//Object obj = itr.next();
//System.out.println("Iterator : Value = " + obj);
System.out.println("Iterator : Value = " + itr.next());
}
//2. using toArray
Object[] obj = arrlistObj1.toArray();
for (int i = 0; i < arrlistObj1.size(); i++)
{
System.out.println("toArray : Value = " + obj[i]);
}
ArrayList arrlistObj2 = new ArrayList();
arrlistObj2.add("newtest");
//add all the entry from arrlistObj1 to arrlistObj2.
arrlistObj2.addAll(arrlistObj1);
arrlistObj1.remove("test1");
arrlistObj1.remove("test3");
//remove all entry of arrlistObj1 in arrlistObj2.
arrlistObj2.removeAll(arrlistObj1);
}
}
List - Vector sample
--------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
/**
* @author Vikneshwaran
* @version 1.0
* #################### Template #####################
* class class_name
* {
* List list_obj1 = new Vector();
* Vector veclist_obj1 = new Vector();
*
* veclist_obj1.add(...);
* veclist_obj1.remove(...);
* veclist_obj1.contains(...);
* int size = veclist_obj1.size();
* Iterator itr_obj = veclist_obj1.iterator();
* Object[] obj = veclist_obj1.toArray();
*
* veclist_obj1.add(.., ..);
* veclist_obj1.set(.., ..);
* veclist_obj1.get(...);
* veclist_obj1.indexOf(...);
* }
*
* ####################################################
*/
//1. Vector allows duplicate value
//2. Vector allows null value
//3. Vector allows different type of data types in single object
//4. Synchronized one
//5. stores the values in indexed way.
public class ListSample2
{
public static void main (String args[])
{
//since list is interface, object cannt be created as
follows
//List listObj = new List();
//using implemented class like ArrayList, Vector &
LinkedList, can do
List listObj1 = new Vector(); //dynamic binding or
runtime polymorphim
Vector veclistObj1 = new Vector();
//also can do
Vector veclist_obj1 = new Vector(10);//after size is
full, it will be increased to double size
Vector veclist_obj2 = new Vector(10, 5);//after size
is full, it will be increased by 2
//collection interface(Set, List) and Map interface
allows object only,
//primitive value cannt be added as follows
/*
int a = 50;
double b = 10.00;
long c = 100;
veclistObj1.add(10);
veclistObj1.add(10.00);
veclistObj1.add(test);
veclistObj1.add(a);
veclistObj1.add(b);
veclistObj1.add(c);
*/
//but we can add as follows
int a = 40;
double b = 50.00;
long c = 60;
String d = "test3";
veclistObj1.add(new Integer(10));
veclistObj1.add(new Double(20.00));
veclistObj1.add(new Long(30));
veclistObj1.add("test1");
veclistObj1.add(new String("test2"));
veclistObj1.add(new Integer(a));
veclistObj1.add(new Double(b));
veclistObj1.add(new Long(c));
veclistObj1.add(d);
//vector allows duplicate value
veclistObj1.add("test4");
veclistObj1.add("test4");
//vector allows null value
veclistObj1.add(null);
//remove the entry from vector
veclistObj1.remove("test2");
veclistObj1.remove(new Integer(10));
veclistObj1.remove(new Double(b));
//check the entry is already there or not
boolean cont = veclistObj1.contains("test4");//return
boolean true or false
//remove all the entry
//veclistObj1.clear();
//some list interface's method
//can add or replace value to particular index.
//index shouldnt exceeds actual arraylist size
//if arraylist's size is 20, then can add or replace
value from 0 to 19th index,
//cannt add or replace value for more than 19th index
//veclistObj1.add(22, "newtest1");
//veclistObj1.aet(25, "newtest1");
veclistObj1.add(1, "newtest1");
veclistObj1.set(3, "newtest2");
//some list interface's method
//index should not exceeds arraylist size
System.out.println("value in 3rd position is " +
veclistObj1.get(3));
//some list interface's method
//can get the index of particular value in arraylist
System.out.println("Index of test1 is " +
veclistObj1.indexOf("test1"));
//some vector class's method
veclistObj1.addElement("newelement1");
veclistObj1.setElementAt("newelement2", 1);
veclistObj1.setElementAt("newelement3", 2);
System.out.println("Element at 1 is " +
veclistObj1.elementAt(1));
veclistObj1.removeElementAt(1);
//size
int size = veclistObj1.size();
System.out.println("Size of vector is " + size);
//to get or display all the entry in vector
//1. using iterator
Iterator itr = veclistObj1.iterator();
while (itr.hasNext())
{
//if all the entry are same data type, then can
typecast and store in varibale as follows
//String entry = (String) itr.next();
//Double entry = (Double) itr.next();
//System.out.println("Iterator : Value = " + entry);
//if all the entry are different, then can store it
as follows
//Object obj = itr.next();
//System.out.println("Iterator : Value = " + obj);
System.out.println("Iterator : Value = " + itr.next());
}
//2. using toArray
Object[] obj = veclistObj1.toArray();
for (int i = 0; i < veclistObj1.size(); i++)
{
System.out.println("toArray : Value = " + obj[i]);
}
Vector veclistObj2 = new Vector();
veclistObj2.add("newtest");
//add all the entry from veclistObj1 to veclistObj2.
veclistObj2.addAll(veclistObj1);
veclistObj1.remove("test1");
veclistObj1.remove("test3");
//remove all entry of veclistObj1 in veclistObj2.
veclistObj2.removeAll(veclistObj1);
}
}
List - LinkedList sample
--------------------------------------------------------------------------------
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* @author Vikneshwaran
* @version 1.0
* #################### Template #####################
* class class_name
* {
* List list_obj1 = new LinkedList();
* LinkedList linklist_obj1 = new LinkedList();
*
* linklist_obj1.add(...);
* linklist_obj1.remove(...);
* linklist_obj1.contains(...);
* int size = linklist_obj1.size();
* Iterator itr_obj = linklist_obj1.iterator();
* Object[] obj = linklist_obj1.toArray();
*
* }
*
* ####################################################
*/
//1. LinkedList allows duplicate value
//2. LinkedList allows null value
//3. LinkedList allows different type of data types in
single object
//4. not Synchronized one
//5. stores the values in indexed way.
//6. can add or remove entry at front and end of LinkedList
public class ListSample3
{
public static void main (String args[])
{
//since list is interface, object cannt be created as
follows
//List listObj = new List();
//using implemented class like ArrayList, Vector &
LinkedList, can do
List listObj1 = new LinkedList(); //dynamic binding or
runtime polymorphim
LinkedList linklistObj1 = new LinkedList();
//cannt do
//LinkedList linklist_obj1 = new LinkedList(10);
//LinkedList linklist_obj2 = new LinkedList(10, 5);
//collection interface(Set, List) and Map interface
allows object only,
//primitive value cannt be added as follows
/*
int a = 50;
double b = 10.00;
long c = 100;
linklistObj1.add(10);
linklistObj1.add(10.00);
linklistObj1.add(test);
linklistObj1.add(a);
linklistObj1.add(b);
linklistObj1.add(c);
*/
//but can add as follows
int a = 40;
double b = 50.00;
long c = 60;
String d = "test3";
linklistObj1.add(new Integer(10));
linklistObj1.add(new Double(20.00));
linklistObj1.add(new Long(30));
linklistObj1.add("test1");
linklistObj1.add(new String("test2"));
linklistObj1.add(new Integer(a));
linklistObj1.add(new Double(b));
linklistObj1.add(new Long(c));
linklistObj1.add(d);
//vector allows duplicate value
linklistObj1.add("test4");
linklistObj1.add("test4");
//vector allows null value
linklistObj1.add(null);
//remove the entry from vector
linklistObj1.remove("test2");
linklistObj1.remove(new Integer(10));
linklistObj1.remove(new Double(b));
//check the entry is already there or not
boolean cont = linklistObj1.contains("test4");//return
boolean true or false
//remove all the entry
//linklistObj1.clear();
//some list interface's method
//can add or replace value to particular index.
//index shouldnt exceeds actual arraylist size
//if arraylist's size is 20, then can add or replace
value from 0 to 19th index,
//cannt add or replace value for more than 19th index
//linklistObj1.add(22, "newtest1");
//linklistObj1.aet(25, "newtest1");
linklistObj1.add(1, "newtest1");
linklistObj1.set(3, "newtest2");
//some list interface's method
//index should not exceeds arraylist size
System.out.println("value in 3rd position is " +
linklistObj1.get(3));
//some list interface's method
//can get the index of particular value in arraylist
System.out.println("Index of test1 is " +
linklistObj1.indexOf("test1"));
//some linkedlist class's methods
linklistObj1.addFirst("newelement1");
linklistObj1.addFirst("newelement2");
linklistObj1.addLast("newelement5");
linklistObj1.addLast("newelement6");
System.out.println("value at first is " +
linklistObj1.getFirst());
System.out.println("value at last is " +
linklistObj1.getLast());
linklistObj1.removeFirst();
linklistObj1.removeLast();
//size
int size = linklistObj1.size();
System.out.println("Size of vector is " + size);
//to get or display all the entry in linkedlist
//1. using iterator
Iterator itr = linklistObj1.iterator();
while (itr.hasNext())
{
//if all the entry are same data type, then can
typecast and store in varibale as follows
//String entry = (String) itr.next();
//Double entry = (Double) itr.next();
//System.out.println("Iterator : Value = " + entry);
//if all the entry are different, then can store it
as follows
//Object obj = itr.next();
//System.out.println("Iterator : Value = " + obj);
System.out.println("Iterator : Value = " + itr.next());
}
//2. using toArray
Object[] obj = linklistObj1.toArray();
for (int i = 0; i < linklistObj1.size(); i++)
{
System.out.println("toArray : Value in linkedlist =
" + obj[i]);
}
LinkedList linklistObj2 = new LinkedList();
linklistObj2.add("newtest");
//add all the entry from linklistObj1 to linklistObj2.
linklistObj2.addAll(linklistObj1);
linklistObj1.remove("test1");
linklistObj1.remove("test3");
//remove all entry of linklistObj1 in linklistObj2.
linklistObj2.removeAll(linklistObj1);
}
}
Map - HashMap sample
--------------------------------------------------------------------------------
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* @author Vikneshwaran
* @version 1.0
* #################### Template #####################
* class class_name
* {
* Map map_obj1 = new HashMap();
* HashMap hashmap_obj1 = new HashMap();
*
* hashmap_obj1.put(.., ..);
* hashmap_obj1.remove(...);
* hashmap_obj1.containsKey(...);
* hashmap_obj1.containsValue(...);
* int size = hashmap_obj1.size();
* Set set_obj = hashmap_obj1.KeySet();
* Iterator itr_obj = set_obj.iterator();
* Collection coll_obj = hashmap_obj1.Values();
* Object[] obj = coll_obj.toArray();
* }
*
* ####################################################
*/
//1. HashMap doesnt allows duplicate value
//2. HashMap doesnt allows null value
//3. HashMap allows different type of data types in single
object
//4. not Synchronized one
//5. Stores the entry in key value pair way
public class MapSample2
{
public static void main (String args[])
{
//since map is interface, object cannt be created as
follows
//Map mapObj = new Map();
//using implemented class like HashMap, HashTable and
Properties, can do
Map mapObj1 = new HashMap(); //dynamic binding or
runtime polymorphim
HashMap hashmapObj1 = new HashMap();
//can do as follows
HashMap hashmap_obj1 = new HashMap(10);//after the size
is full, it will be increased to double size
HashMap hashmap_obj2 = new HashMap(10, 5);//after the
size is full, it will be increased by 5
//collection interface(Set, List) and Map interface
allows object only,
//primitive value cannt be added as follows
/*
int a = 50;
double b = 10.00;
long c = 100;
hashmapObj1.put(10, "value1");
hashmapObj1.put(10.00, "value2");
hashmapObj1.put(test, "value3");
hashmapObj1.put(a, "value4");
hashmapObj1.put(b, "value5");
hashmapObj1.put(c, "value6");
*/
//but we can do as follows
int a = 40;
double b = 50.00;
long c = 60;
String d = "test3";
hashmapObj1.put(new Integer(10), "value1");
hashmapObj1.put(new Double(20.00), "value2");
hashmapObj1.put(new Long(30), "value3");
hashmapObj1.put("test1", "value4");
hashmapObj1.put(new String("test2"), "value5");
hashmapObj1.put(new Integer(a), "value6");
hashmapObj1.put(new Double(b), "value7");
hashmapObj1.put(new Long(c), "value8");
hashmapObj1.put(d, "value9");
//hashmap doesnt allows duplicate value, can add as
follows but it will store single entry only
hashmapObj1.put("test4", "value10");
hashmapObj1.put("test4", "value11");
//hashmap allows null value
hashmapObj1.put(null, "value12");
hashmapObj1.put(null, null);
//remove the entry
hashmapObj1.remove("test2");
hashmapObj1.remove(new Integer(10));
hashmapObj1.remove(new Double(b));
//check the key is already there or not
boolean cont1 =
hashmapObj1.containsKey("test4");//return boolean true or false
//check the value is already there or not
boolean cont2 =
hashmapObj1.containsValue("value4");//return boolean true or
false
//get the value for particular key
System.out.println("value of key test4 is " +
hashmapObj1.get("test4"));
//remove all the entry
//hashmapObj1.clear();
//size of the set
int size = hashmapObj1.size();
System.out.println("Size of HashMap is " + size);
//to get or display all the entry in hashmap
//1. using keyset and iterator
//get all the keys from hashmap
Set setObj = hashmapObj1.keySet();
Iterator itr = setObj.iterator();
while (itr.hasNext())
{
//if all the entry are same data type, then can
typecast and store in varibale as follows
//String entry = (String) itr.next();
//Double entry = (Double) itr.next();
//System.out.println("Iterator : Value = " + entry);
//if all the entry are different, then can store it
as follows
Object obj = itr.next();
System.out.println("Key in Hashmap = " + obj);
System.out.println("Value in Hashmap is " +
hashmapObj1.get(obj));
}
//get the all the values from hashmap
Collection collObj = hashmapObj1.values();
//2. using toArray
Object[] obj = collObj.toArray();
for (int i = 0; i < collObj.size(); i++)
{
System.out.println("Value in HashMap = " + obj[i]);
}
HashMap hashmapObj2 = new HashMap();
hashmapObj2.put("newtest", "newvalue1");
//add all the entry from hashmapObj1 to hashmapObj2.
hashmapObj2.putAll(hashmapObj1);
//removeall method is not available.
//hashmapObj2.removeAll(hashmapObj1);
}
}
Map - Hashtable sample
--------------------------------------------------------------------------------
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* @author Vikneshwaran
* @version 1.0
* #################### Template #####################
* class class_name
* {
* Map map_obj1 = new Hashtable();
* Hashable hashtable_obj1 = new Hashtable();
*
* hashtable_obj1.put(.., ..);
* hashtable_obj1.remove(...);
* hashtable_obj1.containsKey(...);
* hashtable_obj1.containsValue(...);
* int size = hashtable_obj1.size();
* Set set_obj = hashtable_obj1.KeySet();
* Iterator itr_obj = set_obj.iterator();
* Collection coll_obj = hashtable_obj1.Values();
* Object[] obj = coll_obj.toArray();
*
* }
*
* ####################################################
*/
//1. HashTable doesnt allows duplicate value
//2. HashTable doesnt allows null value
//3. HashTable allows different type of data types in single
object
//4. Synchronized one
//5. Stores the entry in key value pair way
public class MapSample1
{
public static void main (String args[])
{
//since map is interface, object cannt be created as
follows
//Map mapObj = new Map();
//using implemented class like HashMap, HashTable and
Properties, can do
Map mapObj1 = new Hashtable(); //dynamic binding or
runtime polymorphim
Hashtable hashtableObj1 = new Hashtable();
//can do as follows
Hashtable hashtable_obj1 = new Hashtable(10);//after
the size is full, it will be increased to double size
Hashtable hashtable_obj2 = new Hashtable(10,
5);//after the size is full, it will be increased by 5
//collection interface(Set, List) and Map interface
allows object only,
//primitive value cannt be added as follows
/*
int a = 50;
double b = 10.00;
long c = 100;
hashtableObj1.put(10, "value1");
hashtableObj1.put(10.00, "value2");
hashtableObj1.put(test, "value3");
hashtableObj1.put(a, "value4");
hashtableObj1.put(b, "value5");
hashtableObj1.put(c, "value6");
*/
//but can add as follows
int a = 40;
double b = 50.00;
long c = 60;
String d = "test3";
hashtableObj1.put(new Integer(10), "value1");
hashtableObj1.put(new Double(20.00), "value2");
hashtableObj1.put(new Long(30), "value3");
hashtableObj1.put("test1", "value4");
hashtableObj1.put(new String("test2"), "value5");
hashtableObj1.put(new Integer(a), "value6");
hashtableObj1.put(new Double(b), "value7");
hashtableObj1.put(new Long(c), "value8");
hashtableObj1.put(d, "value9");
//hashtable doesnt allows duplicate value, can add as
follows but it will store single entry only
hashtableObj1.put("test4", "value10");
hashtableObj1.put("test4", "value11");
//hashtable doesnt allows null value
//hashtableObj1.put(null, "value12");
//hashtableObj1.put(null, null);
//remove the entry
hashtableObj1.remove("test2");
hashtableObj1.remove(new Integer(10));
hashtableObj1.remove(new Double(b));
//check the key is already there or not
boolean cont1 =
hashtableObj1.containsKey("test4");//return boolean true or
false
//check the value is already there or not
boolean cont2 =
hashtableObj1.containsValue("value4");//return boolean true
or false
//get the value for particular key
System.out.println("value of key test4 is " +
hashtableObj1.get("test4"));
//remove all the entry
//hashtableObj1.clear();
//size of the set
int size = hashtableObj1.size();
System.out.println("Size of Hashtable is " + size);
//to get or display all the entry in hashmap
//1. using keyset and iterator
//get all the keys from hashmap
Set setObj = hashtableObj1.keySet();
Iterator itr = setObj.iterator();
while (itr.hasNext())
{
//if all the entry are same data type, then can
typecast and store in varibale as follows
//String entry = (String) itr.next();
//Double entry = (Double) itr.next();
//System.out.println("Iterator : Value = " + entry);
//if all the entry are different, then can store it
as follows
Object obj = itr.next();
System.out.println("Key in Hashtable = " + obj);
System.out.println("Value in Hashtable is " +
hashtableObj1.get(obj));
}
//get the all the values from hashmap
Collection collObj = hashtableObj1.values();
//2. using toArray
Object[] obj = collObj.toArray();
for (int i = 0; i < collObj.size(); i++)
{
System.out.println("Value in Hashtable = " + obj[i]);
}
Hashtable hashtableObj2 = new Hashtable();
hashtableObj2.put("newtest", "newvalue1");
//add all the entry from hashtableObj1 to hashtableObj2.
hashtableObj2.putAll(hashtableObj1);
//removeall method is not available.
//hashtableObj2.removeAll(hashtableObj1);
}
}
Map - Properties sample
--------------------------------------------------------------------------------
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* @author Vikneshwaran
* @version 1.0
* #################### Template #####################
* class class_name
* {
* Map map_obj1 = new Properties();
* Hashtable hashmap_obj1 = new Properties();
* Properties prop_obj1 = new Properties();
*
* prop_obj1.put(.., ..);
* prop_obj1.remove(...);
* prop_obj1.containsKey(...);
* prop_obj1.containsValue(...);
* int size = prop_obj1.size();
* Set set_obj = prop_obj1.KeySet();
* Iterator itr_obj = set_obj.iterator();
* Collection coll_obj = prop_obj1.Values();
* Object[] obj = coll_obj.toArray();
*
* }
*
* ####################################################
*/
//1. Properties doesnt allows duplicate value
//2. Properties doesnt allows null value
//3. Properties allows different type of data types in
single object
//4. not Synchronized one
//5. Stores the entry in key value pair way
public class MapSample3
{
public static void main (String args[])
{
//since map is interface, object cannt be created as
follows
//Map mapObj = new Map();
//using implemented class like HashMap, HashTable and
Properties, can do
Map mapObj1 = new Properties(); //dynamic binding or
runtime polymorphim
//Since its a subclass of hashtable,
Hashtable propObj2 = new Properties();
Properties propmapObj1 = new Properties();
//cannt do as follows
//Properties proppmap_obj1 = new Properties(10);
//Properties propmap_obj2 = new Properties(10, 5);
//collection interface(Set, List) and Map interface
allows object only,
//primitive value cannt be added as follows
/*
int a = 50;
double b = 10.00;
long c = 100;
propmapObj1.put(10, "value1");
propmapObj1.put(10.00, "value2");
propmapObj1.put(test, "value3");
propmapObj1.put(a, "value4");
propmapObj1.put(b, "value5");
propmapObj1.put(c, "value6");
*/
//but can add as follows
int a = 40;
double b = 50.00;
long c = 60;
String d = "test3";
propmapObj1.put(new Integer(10), "value1");
propmapObj1.put(new Double(20.00), "value2");
propmapObj1.put(new Long(30), "value3");
propmapObj1.put("test1", "value4");
propmapObj1.put(new String("test2"), "value5");
propmapObj1.put(new Integer(a), "value6");
propmapObj1.put(new Double(b), "value7");
propmapObj1.put(new Long(c), "value8");
propmapObj1.put(d, "value9");
//properties doesnt allows duplicate value, can add as
follows but it will store single entry only
propmapObj1.put("test4", "value10");
propmapObj1.put("test4", "value11");
//properties doesnt allows null value
//propmapObj1.put(null, "value12");
//propmapObj1.put(null, null);
//properties extra's methods
propmapObj1.setProperty("test5", "value11");
//setProperty can hold string key and String value only
//propmapObj1.setProperty(new Integer(10), "value11");
System.out.println("Value of property is " +
propmapObj1.getProperty("test5"));
System.out.println("Value of property is " +
propmapObj1.getProperty("test5", "default value"));
//remove the entry from properties using key
propmapObj1.remove("test2");
propmapObj1.remove(new Integer(10));
propmapObj1.remove(new Double(b));
//check the key is already there or not
boolean cont1 =
propmapObj1.containsKey("test4");//return boolean true or false
//check the value is already there or not
boolean cont2 =
propmapObj1.containsValue("value4");//return boolean true or
false
System.out.println("value of key test4 is " +
propmapObj1.get("test4"));
//remove all the entry
//propmapObj1.clear();
//size of the set
int size = propmapObj1.size();
System.out.println("Size of Properties is " + size);
//to get or display all the entry in hashmap
//1. using keyset and iterator
//get all the keys from hashmap
Set setObj = propmapObj1.keySet();
Iterator itr = setObj.iterator();
while (itr.hasNext())
{
//if all the entry are same data type, then can
typecast and store in varibale as follows
//String entry = (String) itr.next();
//Double entry = (Double) itr.next();
//System.out.println("Iterator : Value = " + entry);
//if all the entry are different, then can store it
as follows
Object key = itr.next();
System.out.println("Key in Properties is " + key);
System.out.println("Value in Properties is " +
propmapObj1.get(key));
}
//get the all the values from properties
Collection collObj = propmapObj1.values();
//2. using toArray
Object[] obj = collObj.toArray();
for (int i = 0; i < collObj.size(); i++)
{
System.out.println("Value in Properties = " + obj[i]);
}
Properties propmapObj2 = new Properties();
propmapObj2.put("newtest", "newvalue1");
//add all the entry from propmapObj1 to propmapObj2.
propmapObj2.putAll(propmapObj1);
//removeall method is not available.
//propmapObj2.removeAll(propmapObj1);
}
}
have any query
cal @9962590014
Is This Answer Correct ? | 2 Yes | 0 No |
What is sortedmap interface?
What are the interfaces defined by Java.lang package?
What is * argv?
What are the limitations for static method?
What is main string [] args?
Can we add two byte variables and assign the result to a byte variable ? b=b1+b2 where b,b1,b2 are byte types
Why java is not a pure object oriented language?
What is default size of arraylist in java?
Are constructors methods?
What is Co-Variant return type in method overriding?
Explain the private protected method modifier?
What is a values collection view ?