import
java.util.Collection;
import
java.util.HashSet;
import
java.util.Iterator;
import
java.util.Set;
public
class
HashSetImpl<T> {
private
Set<T> hashSet;
public
HashSetImpl(Collection<?
extends
T> c)
{
hashSet =
new
HashSet<T>(c);
}
public
HashSetImpl(
int
initialCapacity,
float
loadFactor)
{
hashSet =
new
HashSet<T>(initialCapacity, loadFactor);
}
public
HashSetImpl(
int
initialCapacity)
{
hashSet =
new
HashSet<T>(initialCapacity);
}
public
HashSetImpl() { hashSet =
new
HashSet<T>(); }
public
Iterator<T> iterator()
{
return
hashSet.iterator();
}
public
boolean
add(T eobj) {
return
hashSet.add(eobj); }
public
boolean
contains(Object obj)
{
return
hashSet.contains(obj);
}
public
boolean
isEmpty() {
return
hashSet.isEmpty(); }
public
boolean
remove(Object obj)
{
return
hashSet.remove(obj);
}
public
int
size() {
return
hashSet.size(); }
public
void
clear() { hashSet.clear(); }
public
Object[] toArray() {
return
hashSet.toArray(); }
public
boolean
addAll(Collection<?
extends
T> c)
throws
UnsupportedOperationException,
ClassCastException, NullPointerException,
IllegalArgumentException
{
return
hashSet.addAll(c);
}
public
boolean
retainAll(Collection<?> c)
throws
UnsupportedOperationException,
ClassCastException, NullPointerException
{
return
hashSet.retainAll(c);
}
public
boolean
removeAll(Collection<?> c)
throws
UnsupportedOperationException,
NullPointerException, ClassCastException
{
return
hashSet.retainAll(c);
}
public
<T> T[] toArray(T[] a)
throws
ArrayStoreException, NullPointerException
{
return
hashSet.toArray(a);
}
}
class
Solution {
public
static
void
main(String arg[])
{
HashSet<String> hashSet =
new
HashSet<String>();
if
(hashSet.add(
"Mani"
))
System.out.println(
"element Mani added"
);
if
(hashSet.add(
"Rohit"
))
System.out.println(
"element Rohit added"
);
if
(hashSet.add(
"Manish"
))
System.out.println(
"element Manish added"
);
System.out.println(
"the size of set is "
+ hashSet.size());
if
(hashSet.contains(
"Sachin"
))
System.out.println(
"set contains Sachin"
);
else
System.out.println(
"set does not contain Sachin"
);
if
(hashSet.remove(
"Mani"
))
System.out.println(
"element 20 removed"
);
else
System.out.println(
"element 20 not removed"
);
System.out.println(
"the element of set are"
);
Iterator<String> iterator = hashSet.iterator();
while
(iterator.hasNext())
{
System.out.print(iterator.next() +
"\t"
);
}
System.out.println();
Set<String> removedSet =
new
HashSet<String>();
removedSet.add(
"Nikhil"
);
removedSet.add(
"Kapil"
);
System.out.println(
"the elements after removing"
);
hashSet.removeAll(removedSet);
Iterator<String> riterator = hashSet.iterator();
while
(riterator.hasNext()) {
System.out.print(riterator.next() +
"\t"
);
}
System.out.println();
hashSet.clear();
System.out.println(
"hashSet cleared"
);
if
(hashSet.isEmpty())
System.out.println(
"hashSet is empty"
);
else
System.out.println(
"hashSet is not empty"
);
}
}