All Downloads are FREE. Search and download functionalities are using the official Maven repository.

java.util.HashSet Maven / Gradle / Ivy

There is a newer version: 0.96-beta4
Show newest version
package java.util;

/**
 * This class implements the Set interface, backed by a {@link java.util.HashMap}.
 * 
 * 
 */
public class HashSet extends AbstractCollection implements Set
{

	private List list;

	/**
	 * Constructs a new, empty set
	 */
	public HashSet()
	{
		list= new ArrayList();
	}

	public HashSet(Collection source)
	{
		this();
		for (Object object : source)
		{
			add((E) object);
		}
	}

	/**
	 * Adds the specified element to this set if it is not already present.
	 */
	public boolean add(E element)
	{
		if (!list.contains(element))
		{
			list.add(element);
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * Removes all of the elements from this collection (optional operation).
	 */
	public void clear()
	{
		list.clear();
	}

	/**
	 * Returns true if this set contains the specified element.
	 */
	public boolean contains(Object element)
	{
		return list.contains(element);
	}

	/**
	 * Returns an iterator over the elements in this set.
	 */
	public Iterator iterator()
	{
		return list.iterator();
	}

	/**
	 * Returns true if this collection contains no elements.
	 */
	public boolean isEmpty()
	{
		return list.isEmpty();
	}

	/**
	 * Removes all of the elements from this collection (optional operation).
	 */
	public boolean remove(Object elem)
	{
		return list.remove(elem);
	}

	/**
	 * Returns the number of elements in this set (its cardinality).
	 */
	public int size()
	{
		return list.size();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy