HashSet contains a non-repeating collection of items.
HashSet contains unsorted items.
HashSet allows you to quickly determine whether such an element exists or not (fast because it uses an index that is calculated from the hash code of the element).
HashSet has methods
Add,
Remove,
Contains and uses a hash implementation, so these methods take
1 action (the
Contains and
Remove methods in
ArrayList take
n actions.)
HashSet is not synchronized. If multiple threads access a hash set concurrently, and at least one of the threads modifies the
HashSet, it must be synchronized externally.
HashSet
implements
interface Set<E> is a collection of unique elements
Java
Creating a new Java console application ... and write the code
package org.example;
import java.util.Stack;
public class Program {
public static void main(String[] args)
{
HashSet<String> myHashSet1 = new HashSet<String>();
// Adding elements
myHashSet1.add("Car");
myHashSet1.add("House");
myHashSet1.add("Book");
// Check if the item is in the collection
Boolean hasElement = myHashSet1.contains("House");
// hasElement = true
// Go through all the elements
for(String str:myHashSet1)
{
System.out.println(str);
}
// element1 = Car
// element2 = Book
// element3 = House
// delete the element "Book"
myHashSet1.remove("Book");
// element1 = Car
// element3 = House
}
}
class HashSet<E> inside contains
HashMap:
Java
class HashSet<E>
{
HashMap<E,Object> map;
...
// In all public constructors, HashMap is created
public HashSet(...) {
map = new HashMap<>(...);
}
}
Java
class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, java.io.Serializable
{
HashMap<E,Object> map;
public HashSet() {
map = new HashMap<>();
}
public HashSet(Collection<? extends E> c) {
map = HashMap.newHashMap(Math.max(c.size(), 12));
addAll(c);
}
public HashSet(int initialCapacity, float loadFactor) {
map = new HashMap<>(initialCapacity, loadFactor);
}
public HashSet(int initialCapacity) {
map = new HashMap<>(initialCapacity);
}
private HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<>(initialCapacity, loadFactor);
}
public Iterator<E> iterator() {
return map.keySet().iterator();
}
public int size() {
return map.size();
}
public boolean isEmpty() {
return map.isEmpty();
}
public boolean contains(Object o) {
return map.containsKey(o);
}
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}
public void clear() {
map.clear();
}
public Object clone() {
try {
HashSet<E> newSet = (HashSet<E>) super.clone();
newSet.map = (HashMap<E, Object>) map.clone();
return newSet;
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}
@java.io.Serial
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
...
}
@java.io.Serial
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
...
}
public Spliterator<E> spliterator() {
return new HashMap.KeySpliterator<>(map, 0, -1, 0, 0);
}
@Override
public Object[] toArray() {
return map.keysToArray(new Object[map.size()]);
}
@Override
public <T> T[] toArray(T[] a) {
return map.keysToArray(map.prepareArray(a));
}
public static <T> HashSet<T> newHashSet(int numElements) {
if (numElements < 0) {
throw new IllegalArgumentException("Negative number of elements: " + numElements);
}
return new HashSet<>(HashMap.calculateHashMapCapacity(numElements));
}
}
Learn more about methods in the Java documentation: