Collections
Collections
ArrayList class
LinkedList class
List Interface
HashSet class
Once an array is created we can neither increase or decrease the length of the array.
3 . There is no underlying data structure for arrays, i.e., there are no readymade method support for
arrays.
Collection: if we want to represent group of objects as a single entity then we can go use collection
framework.
Collection(Interface)
1.5
If we want to represent group of objects where duplicates are allowed and insertion order is preserved
then we can use List object.
Ex:
package pack1;
import java.util.ArrayList;
Ex2:
package pack1;
import java.util.ArrayList;
}
Ex3:using generics
package pack1;
import java.util.ArrayList;
String s1=al.get(2);
System.out.println(s1);
System.out.println(al.get(2));
}
}
output:
[hyderabad, pune, pune]
pune
pune
if we want to represent group of objects where duplicates are not allowed and insertion order is not
preserved then use HashSet.
If we want to represent group of objects where duplicates are not allowed but insertion order is
preserved then use LinkedHashSet.
package pack1;
import java.util.HashSet;
Cursors/Iterators :
i. Enumeration
ii. Iterator
iii. ListIterator
Enumeration is used to iterate through the elements of legacy classes like Vector,
Properties classes.
Ex1:
package pack1;
import java.util.Enumeration;
import java.util.Vector;
while( e.hasMoreElements())
{
Integer I=e.nextElement();
if (I%2==0)
System.out.println(I);
}
}
the limitation of Enumeration is that it can be used only
for legacy classes(old classes) like Vector, Properties.
It cannot be used for other classes like ArrayList.
Using Enumeration we can only read, we cannot remove.
To overcome these limitations we need to use Iterator.
Ex:
package pack1;
import java.util.ArrayList;
import java.util.Iterator;
Iterator<Integer> Itr=al.iterator();
while(Itr.hasNext())
{
Integer I=Itr.next();
if (I%2==0)
System.out.println(I);
else
Itr.remove();
}
System.out.println(al);
}
output:
2
4
6
8
10
[2, 4, 6, 8, 10]
Limitations of Iterator:
i>though it can be used for every collection object, using
Iterator we can move only in forward direction we cannot
move in reverse direction.
ii> using Iterator we can only remove elements, we
cannot add or replace.
Ex:
package pack1;
import java.util.LinkedList;
import java.util.ListIterator;
ListIterator<String> litr=ll.listIterator();
while(litr.hasNext())
{
String s=litr.next();
if (s.equals("kosmik"))
litr.set("bigclasses");
}
System.out.println(ll);
}
Output:
package pack1;
import java.util.HashSet;
Ex2:
package pack1;
import java.util.HashSet;
import java.util.Iterator;
Iterator<String> itr=hs.iterator();
while(itr.hasNext())
{
String s=itr.next();
if(s.equals("kphb"))
itr.remove();
}
System.out.println(hs);//[hyd, secbad]
TreeSet:
If we are adding objects and if we are depending
upon default natural sorting order then the
objects being added should be homogeneous and
comparable.
An object is said to be comparable if the
corresponding class implements Comparable
interface.
String class and Integer class implements
Comparable interface.
Ex:
If we depend upon default sorting order, then
add comparable objects directly.
package pack1;
import java.util.TreeSet;
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetEx1 {
}
class MyComparator implements Comparator
{
@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
Integer I1=(Integer)o1;
Integer I2=(Integer)o2;
return -I1.compareTo(I2);
}
}
Output:
[90, 70, 30, 20, 10]