Java Collections Framework is a collection of interfaces and
classes which help in storing and processing the data efficiently. Collection
Framework provides an architecture to store and manipulates the group of
objects.
All collections frameworks contain the following:
· Interfaces: These are abstract data
types that represent collections.
· Classes: These are the concrete
implementations of the collection interfaces.
Advantages of Collections:
Reduces effort of implementing our own collection classes rather we can use core collection classes
Code quality is enhanced with the use of well tested collections
framework classes.
Reusability and Interoperability
Set: A Set is a Collection that cannot contain duplicate elements.The Set interface contains only methods inherited from Collection .
Since Set is an interface you need to instantiate a concrete
implementation of the interface in order to use it. You can choose between the
following Set implementations in the Java Collections API:
java.util.EnumSet
java.util.HashSet
java.util.LinkedHashSet
java.util.TreeSet
examples of how to create a Set instance:
Set setA = new EnumSet();
Set setB = new HashSet();
Set setC = new LinkedHashSet();
Set setD = new TreeSet();
Methods decleared in Set:
add( ):Adds an object to the collection
clear( ):Removes all objects from the collection
contains( ):Returns true if a specified object is an element
within the collection
isEmpty( ):Returns true if the collection has no elements
iterator( ):Returns an Iterator object for the collection which
may be used to retrieve an object
remove( ):Removes a specified object from the collection
size( ):Returns the number of elements in the collection
Sample program:
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class SetExample {
public static void main(String args[]) {
// Creating new Set
Set Set1 = new HashSet();
// Adding elements
Set1.add("A");
Set1.add("B");
Set1.add("C");
//Removing elements:
Set1.remove("A");
// Use of Iterator
Iterator iterator = Set1.iterator();
while (iterator.hasNext())
{
System.out.println("abc " + iterator.next());
}
// Use of for-each
for (Object object : Set1)
System.out.println("for-each loop: " + object);
// Clearing all the elements
Set1.clear();
System.out.println("Set1 is Empty: " + Set1.isEmpty());
// Checking the number of elements
System.out.println("Set1 has: " + Set1.size() + "
Elements");
}
}
ArrayList:
import java.util.ArrayList;
import java.util.Iterator;
public class SetExample {
public static void main(String args[]) {
ArrayList<String> alist=new
ArrayList<String>(); //creating arraylist
alist.add("Ajoy");//adding
object in arraylist
alist.add("Rahul");
alist.add("Rakesh");
alist.add("Sudhir");
Iterator itr=alist.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
ArrayList<String> alist2=new
ArrayList<String>();
alist2.add("Sudhir1");
alist2.add("Ajoy1");
alist.addAll(alist2);
Iterator itr1=alist.iterator();
while(itr1.hasNext()){
System.out.println(itr1.next());
}
ArrayList<String> alist3=new
ArrayList<String>();
alist3.add("Sudhir1");
alist3.add("Ajoy1");
alist.removeAll(alist3);
Iterator itr2=alist.iterator();
while(itr2.hasNext()){
System.out.println(itr2.next());
}
}
}
HashTable:
import java.util.Hashtable;
import java.util.Enumeration;
public class SetExample {
public static void main(String[] args) {
Enumeration names;
String key;
// Creating a Hashtable
Hashtable<String, String> hashtable = new
Hashtable<String, String>();
// Adding Key and Value pairs to Hashtable
hashtable.put("Key1","Ajoy");
hashtable.put("Key2","Rakesh");
hashtable.put("Key3","Rahul");
hashtable.put("Key4","Smith");
hashtable.put("Key5","Mayank");
names = hashtable.keys();
while(names.hasMoreElements())
{
key = (String) names.nextElement();
System.out.println("The key: "
+key+ " has Value: " + hashtable.get(key));
}
}
}
HashMap:
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
public class TestHashMap {
public static void main(String args[]) {
HashMap<Integer, String> h = new
HashMap<Integer, String>();
h.put(1, "Ajoy");
h.put(2, "Rakesh");
h.put(3, "Rahul");
h.put(4, "Smith");
h.put(5, "Mayank");
Set set = h.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry m =
(Map.Entry)i.next();
System.out.println("The key "+ m.getKey() + " has Value "+
m.getValue());
}
/* Get values based on key*/
String val= h.get(1);
System.out.println("Value at
index 1 is: "+val);
/* Remove values based on key*/
h.remove(2);
System.out.println("New Set after
reomoval");
Set set2 = h.entrySet();
Iterator i1 = set2.iterator();
while(i1.hasNext()) {
Map.Entry m1 =
(Map.Entry)i1.next();
System.out.println("The key "+ m1.getKey() + " has Value "+
m1.getValue());
}
}
}