Collections in Java
Collections in Java
LOGESWARAN K
COLLECTIONS
COLLECTIONS
Collection represents a group of individual object as single unit or
entity .
Sometimes called a container.
All the operations that you perform on a data such as searching,
sorting, insertion, manipulation, deletion etc. can be performed by
Java Collections
The java.util package contains all the classes and interfaces for
Collection framework
COLLECTION FRAMEWORK
It defines several classes and interfaces which can be used to
represent and manipulated the group of object as a single entity.
It includes: Interfaces, Classes and Algorithms
HIERARCHY
METHODS OF COLLECTION INTERFACE
No. Method Description
Constructor Description
System.out.println(al);
System.out.println(af);
ArrayList ag=new ArrayList(10);
ag.add("Alpha");
ag.addAll(af);
boolean x=ag.contains("Alpha"); CONTD…
if(x)
System.out.println("Alpha is present in the Alpha is present in the list....
list....");
System.out.println(ag); [Alpha, 1, 2, 3, 4, 5, 6]
ag.add(6,"iit");
ag.add(8,"IT"); [Alpha, 1, 2, 3, 4, 5, iit, 6, IT, MIT,
ag.add("MIT"); Google, google, isro]
ag.add(10,"Google");
ag.add("google"); [Alpha, 1, 2, 3, 4, 5, iit, 6, IT, MIT,
ag.add(12,"isro"); Google, isro, 1]
System.out.println(ag);
ag.remove(11); //remove the element at the Size:13
index
ag.add(1); //duplicate allowed get(10):Google
System.out.println(ag);
System.out.println("Size:"+ag.size()); set(10):Google
System.out.println("get(10):"+ag.get(10));
System.out.println("set(10):"+ag.set(10,"face [Alpha, 1, 2, 3, 4, 5, iit, 6, IT, MIT,
book")); // set the value at the index and facebook, isro, 1]
return the old value
System.out.println(ag); indexOf():7
sum of integer array:21
List:[kongu, IT, II, B]
After str.removeAll(sub):[IT, II]
System.out.println("indexOf():"+ag.indexOf( CONTD…
6)); // return the index of 1st occurence of
the element
indexOf():7
Integer s[]=new Integer[al.size()]; //
create an array with size specified sum of integer array:21
s=al.toArray(s); // array list converted to
array List:[kongu, IT, II, B]
int sum=0;
for(int i=0;i<s.length;i++) After str.removeAll(sub):[IT, II]
{
sum=sum+s[i];
}
System.out.println("sum of integer
array:"+sum);
ArrayList<String>str=new
ArrayList<String>();
str.add("kongu");
str.add("IT");
str.add("II");
str.add("B");
CONTD…
System.out.println("List:"+str);
List sub=str.subList(1,3); // includes 2 but
List:[kongu, IT, II, B]
not 3
After str.removeAll(sub):[IT, II]
//System.out.println("Sub list:"+sub);
//str.removeAll(sub);
str.retainAll(sub);
System.out.println("After
str.removeAll(sub):"+str);
}
}
HASHSET CLASS
HashSet class is used to create a collection that uses a hash
table for storage
It inherits the Abstract Set class and implements Set
interface
List can contain duplicate elements whereas Set contains
unique elements only.
Points:
HashSet is an unordered collection
HashSet uses a mechanism called "Hashing" to store the
elements.
It uses a hashtable data structure to store the elements.
It contains unique elements.
It allows storing the null values.
It does not provide a mechanism to maintain the insertion order.
So the elements will be inserted based on the Hashcode.
It is a useful mechanism for the search operation.
By default, it uses 16 as the initial size of the hashtable.
It extends AbstractSet class and implements the Set interface.
It also implements the Cloneable and Serializable interface.
import java.util.*;
class hashset
{
public static void main(String r[])
{
HashSet<String> hs=new HashSet<String>();
hs.add("A");
hs.add("B");
hs.add("C");
hs.add(“A"); // duplicates not added.
Iterator<String> i=hs.iterator(); // use iterator to get objects
while(i.hasNext())
{
System.out.println(i.next());
}
System.out.println(hs);
System.out.println("hashcode:"+hs.hashCode());
}
}
CONSTRUCTORS OF HASHSET CLASS
Constructor Description
It is used to construct a default
HashSet()
HashSet.
HashSet(Collecti It is used to initialize the hash set by
on c) using the elements of the collection c.
It is used to initialize the capacity of the
HashSet(int
hash set to the given integer value
capacity)
capacity. The capacity grows
default
automatically as elements are added to
capacity:16
the HashSet.
HashSet(int
default fill ratio:0.75 (0.1 to 1.0)
capacity, float
fillRatio)
METHODS OF HASHSET CLASS
Method Description
void clear() It is used to remove all of the elements from this set.
boolean contains(Object It is used to return true if this set contains the specified
o) element.
It is used to adds the specified element to this set if it is
boolean add(Object o)
not already present.