In: Computer Science
/**
* Interface for a set data type that supports adding, removing and contains operations.
* @param <T> the type parameter for the elements of the set
*/
interface SetADT<T> {
/**
* Add a new element to the set.
* @param el the new element to add to the set
* @return true if the element has been added to the set, false if it was already in the set
* @throws IllegalArgumentException if the new element passed to the method is null
*/
public boolean add(T el);
/**
* Return true if the element is in the set, false otherwise.
* @param el the element to check for
* @return true if the element is in the set, false if it is not
*/
public boolean contains(T el);
/**
* Remove element from the set.
* @param el the element to remove from the set
* @return true if the element has been removed, false otherwise (if it was not in the set)
*/
public boolean remove(T el);
}
public class MyHashSet<T> implements SetADT<T>{
/**
* Add a new element to the set.
* @param el the new element to add to the set
* @return true if the element has been added to the set, false if
it was already in the set
* @throws IllegalArgumentException if the new element passed to the
method is null
*/
@Override
public boolean add(T el) {
// TODO Auto-generated method stub
return false;
}
/**
* Return true if the element is in the set, false otherwise.
* @param el the element to check for
* @return true if the element is in the set, false if it is
not
*/
@Override
public boolean contains(T el) {
// TODO Auto-generated method stub
return false;
}
/**
* Remove element from the set.
* @param el the element to remove from the set
* @return true if the element has been removed, false otherwise (if
it was not in the set)
*/
@Override
public boolean remove(T el) {
// TODO Auto-generated method stub
return false;
}
public static void main (String[] args) {
}
}
import java.util.LinkedHashMap;
interface setADT<T>{
public boolean add(T el);
public boolean contains(T el);
public boolean remove(T el);
}
public class MyHashSet<T> implements setADT<T> {
//I am using LinkedHashMap HashTable data structure to store the values.
//HashTable data structure.which can be used to store the elements
//and implements the functionalities of set.
LinkedHashMap<T,T> hash = new LinkedHashMap();
@Override
public boolean add(T el) {
// TODO Auto-generated method stub
//hash.get(el) will returnss null if el not present in it otherwise value corresponding to key in HashTable.
//if el is not in hash then we add el to set and returns true.otherwise false;
if(hash.get(el) == null) {
hash.put(el,el);
return true;
}
return false;
}
@Override
public boolean contains(T el) {
// TODO Auto-generated method stub
//if el in hash then condition is false not of false is true.so it will returns true if value present in it.
//otherwise false.
return !(hash.get(el) == null);
}
@Override
public boolean remove(T el) {
// TODO Auto-generated method stub
//if el in hash it will removes from hash then returns true.other wise false.
if(hash.get(el) != null) {
hash.remove(el);
return true;
}
return false;
}
public static void main(String[] args) {
//test cases
//object creation.
setADT<Integer> o = new MyHashSet<Integer>();
System.out.println(o.add(10));
System.out.println(o.add(20));
System.out.println(o.add(50));
System.out.println(o.remove(10));
System.out.println(o.contains(20));
}
}
/*
* output
true
true
true
true
true
*
*/