How to store objects in a HashSet

Below program shows how objects can be stored in a Collection. Here all Objects stored in HashSet are of same type…in this example we are storing objects of class Abc, similarly any objects can be stored in a Collection.

import java.util.HashSet;
import java.util.Iterator;

//Any objects can be stored in Set, or any other Collection
class Abc
{
private int i,j;
Abc(int i,int j)
{
this.i = i;
this.j = j;
}

public void display()
{
System.out.println(“Values are:”+i+” “+j);
}
}

public class SetObjectDemo {
public static void main(String[] args) {

HashSet hs = new HashSet();

//HashSet is internally implemented using hash table
Abc oa,ob,oc,od,oe;

oa = new Abc(12,89);
ob = new Abc(21,98);
oc = new Abc(33,58);
od = new Abc(68,89);
oe = new Abc(32,26);

hs.add(oa);
hs.add(ob);
hs.add(oc);
hs.add(od);
hs.add(oe);

System.out.println(” :”+hs);

Iterator itr = hs.iterator();
while(itr.hasNext())
{
Abc tmp = (Abc)itr.next();
tmp.display();
}

}

}

You may also like How to store objects in a hashset generic class