java.util.AbstractSet
The code you provided is the source code for the java.util.AbstractSet
class in Java, which is an abstract base class that provides skeletal implementations of the java.util.Set
interface.
Here is a brief description of the methods in this class:
public abstract class java.util.AbstractSet<E>
extends java.util.AbstractCollection<E>
implements java.util.Set<E> {}
protected java.util.AbstractSet()
: This is the constructor for the class. It creates a new abstract set.public boolean equals(java.lang.Object)
: This method determines whether the specified object is equal to this set.public int hashCode()
: This method returns the hash code value for this set.public boolean removeAll(java.util.Collection<?>)
: This method removes all of the elements in the specified collection from this set. If the set does not contain any of the elements in the specified collection, the set is unchanged.
In addition to these methods, the java.util.AbstractSet
class also provides implementations of several other methods in the java.util.Set
interface, including add
, addAll
, clear
, contains
, containsAll
, isEmpty
, iterator
, remove
, retainAll
, and size
. However, these methods are not explicitly listed in the code you provided, since they are inherited from the java.util.Set
interface and implemented in this class.
Implemented Example
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
public class MySmallSet <E> extends AbstractSet <E>
{
private E[] elements;
private int size;
@SuppressWarnings("unchecked")
public MySmallSet(int capacity)
{
elements = (E[]) new Object[capacity];
size = 0;
}
@Override
public Iterator < E > iterator()
{
return new Iterator < E > ()
{
private int cursor = 0;
@Override
public boolean hasNext()
{
return cursor < size;
}
@Override
public E next()
{
if (!hasNext())
{
throw new NoSuchElementException();
}
return elements[cursor++];
}
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
};
}
@Override
public int size()
{
return size;
}
@Override
public boolean contains(Object o)
{
if (o == null)
{
for (int i = 0; i < size; i++)
{
if (elements[i] == null)
{
return true;
}
}
}
else
{
for (int i = 0; i < size; i++)
{
if (o.equals(elements[i]))
{
return true;
}
}
}
return false;
}
@Override
public boolean add(E e)
{
if (contains(e))
{
return false;
}
if (size == elements.length)
{
throw new IllegalStateException("Set is full");
}
elements[size++] = e;
return true;
}
@Override
public boolean remove(Object o)
{
for (int i = 0; i < size; i++)
{
if (o == null ? elements[i] == null : o.equals(elements[i]))
{
int numMoved = size - i - 1;
if (numMoved > 0)
{
System.arraycopy(elements, i + 1, elements, i, numMoved);
}
elements[--size] = null;
return true;
}
}
return false;
}
@Override
public void clear()
{
for (int i = 0; i < size; i++)
{
elements[i] = null;
}
size = 0;
}
public static void main(String[] args)
{
Set < String > set = new MySmallSet < > (5);
set.add("apple");
set.add("banana");
set.add("cherry");
set.add("banana"); // This will not be added
set.add("date"); // This will not be added
System.out.println(set.contains("apple")); // true
System.out.println(set.contains("banana")); // true
System.out.println(set.contains("cherry")); // true
System.out.println(set.contains("date")); // false
System.out.println(set.contains("eggplant")); // false
set.remove("cherry");
System.out.println(set.contains("cherry")); // false
System.out.println(set.size()); // 2
}
}
In this example, we create a new instance of MySmallSet
with a capacity of 5 elements and add a few strings to the set. Since "banana" is already in the set, adding it again will have no effect. Similarly, adding "date" will not be successful since the set is already at its capacity.
true
true
true
true
false
false
3
We then test whether the set contains various strings using the contains
method, and remove "cherry" from the set using the remove
method. Finally, we print the size of the set to verify that it contains only two elements after removing "cherry".
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment