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

host.anzo.commons.collection.ConcurrentHashSet Maven / Gradle / Ivy

There is a newer version: 1.29
Show newest version
package host.anzo.commons.collection;

import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author ANZO
 * @since 9/20/2021
 */
public class ConcurrentHashSet extends AbstractSet implements Set {
    private final ConcurrentMap map;

    private static final Object EMPTY_OBJECT = new Object();

    public ConcurrentHashSet(){
        map = new ConcurrentHashMap<>();
    }

    @Override
    public int size() {
        return map.size();
    }

    @Override
    public Iterator iterator(){
        return map.keySet().iterator();
    }

    @Override
    public boolean isEmpty(){
        return map.isEmpty();
    }

    @Override
    public boolean add(final E o){
        return map.put(o, ConcurrentHashSet.EMPTY_OBJECT) == null;
    }

    @Override
    public boolean contains(final Object o){
        return map.containsKey(o);
    }

    @Override
    public void clear(){
        map.clear();
    }

    @Override
    public boolean remove(final Object o){
        return map.remove(o) == ConcurrentHashSet.EMPTY_OBJECT;
    }

    public boolean addIfAbsent(final E o){
        final Object obj = map.putIfAbsent(o, ConcurrentHashSet.EMPTY_OBJECT);
        return obj == null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy