Java程序 实现HashSet API

Java程序 实现HashSet API

HashSet类实现了Set接口,由一个实际上是HashMap实例的散列表支持。对于集合的迭代顺序没有任何保证,这意味着该类不保证元素在一段时间内的恒定顺序。这个类允许空元素的存在。该类还为基本操作提供了恒定的时间性能,如添加、删除、包含和大小,假设哈希函数在桶中正确地分散了元素。

HashSet的几个重要特征是。

  • 执行Set接口。
  • 由于它实现了Set接口,不允许有重复的值。
  • 你在HashSet中插入的对象并不保证以相同的顺序插入。对象是根据它们的哈希代码插入的。
  • HashSet中允许有NULL元素。
  • HashSet也实现了Serializable和Cloneable接口。

实现:

// Java Program to Implement HashSet API
  
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
  
public class HashSetImpl<T> {
    private Set<T> hashSet;
  
    // creating a set containing the elements in the
    // specified collection
    public HashSetImpl(Collection<? extends T> c)
    {
        hashSet = new HashSet<T>(c);
    }
  
    // constructing a set with given initialcapacity and
    // loadfactor
    public HashSetImpl(int initialCapacity,float loadFactor)
    {
        hashSet = new HashSet<T>(initialCapacity, loadFactor);
    }
  
    // specified initial capacity and default load factor
    // (0.75).
  
    public HashSetImpl(int initialCapacity)
    {
        hashSet = new HashSet<T>(initialCapacity);
    }
    
    // creating a new hashset with default capacity
    // 16 and load factor 0.75
    public HashSetImpl() { hashSet = new HashSet<T>(); }
  
    // returns an iterator over the elements in the set
    public Iterator<T> iterator()
    {
        return hashSet.iterator();
    }
  
    // adding the element into the hashset
    public boolean add(T eobj) { return hashSet.add(eobj); }
  
    // return true if this set contains the specified
    // element
    public boolean contains(Object obj)
    {
        return hashSet.contains(obj);
    }
  
    // returns true if the set is empty
    public boolean isEmpty() { return hashSet.isEmpty(); }
  
    // removes the specified element from this set if
    // present
    public boolean remove(Object obj)
    {
        return hashSet.remove(obj);
    }
  
    // returns the number of elements in set
    public int size() { return hashSet.size(); }
  
    // removes all elements from this set
    public void clear() { hashSet.clear(); }
  
    // Returns an array with all of the elements in this
    // set.
    public Object[] toArray() { return hashSet.toArray(); }
  
    // Adds all of the elements in the specified collection
    // to this set if
    // they're not already present
  
    public boolean addAll(Collection<? extends T> c)
               throws UnsupportedOperationException,
               ClassCastException, NullPointerException,
               IllegalArgumentException
    {
        return hashSet.addAll(c);
    }
  
    // Retains only the elements in this set that are
    // contained in the specified
    // collection
  
    public boolean retainAll(Collection<?> c)
               throws UnsupportedOperationException,
               ClassCastException, NullPointerException
    {
        return hashSet.retainAll(c);
    }
  
    // Removes from this set all of its elements that are
    // contained in the specified collection
  
    public boolean removeAll(Collection<?> c)
               throws UnsupportedOperationException,
               NullPointerException, ClassCastException
    {
        return hashSet.retainAll(c);
    }
  
    // Returns an array containing all of the elements in
    // this set; the runtime
    // type of the returned array is that of the specified
    // array
  
    public <T> T[] toArray(T[] a)
        throws ArrayStoreException, NullPointerException
    {
        return hashSet.toArray(a);
    }
}
  
class Solution {
  
    public static void main(String arg[])
    {
        HashSet<String> hashSet = new HashSet<String>();
        
        if (hashSet.add("Mani"))
            System.out.println("element Mani added");
        
        if (hashSet.add("Rohit"))
            System.out.println("element Rohit added");
        
        if (hashSet.add("Manish"))
            System.out.println("element Manish added");
  
        System.out.println("the size of set is "
                           + hashSet.size());
  
        if (hashSet.contains("Sachin"))
            System.out.println("set contains Sachin");
        else
            System.out.println("set does not contain Sachin");
  
        if (hashSet.remove("Mani"))
            System.out.println("element 20 removed");
        else
            System.out.println("element 20 not removed");
  
        System.out.println("the element of set are");
        
        Iterator<String> iterator = hashSet.iterator();
        
        while (iterator.hasNext()) 
        {
            System.out.print(iterator.next() + "\t");
        }
        System.out.println();
  
        Set<String> removedSet = new HashSet<String>();
        
        removedSet.add("Nikhil");
        removedSet.add("Kapil");
  
        System.out.println("the elements after removing");
        
        hashSet.removeAll(removedSet);
        
        Iterator<String> riterator = hashSet.iterator();
        
        while (riterator.hasNext()) {
            System.out.print(riterator.next() + "\t");
        }
        System.out.println();
  
        hashSet.clear();
        
        System.out.println("hashSet cleared");
        
        if (hashSet.isEmpty())
            System.out.println("hashSet is empty");
        else
            System.out.println("hashSet is not empty");
    }
}

输出

element Mani added
element Rohit added
element Manish added
the size of set is 3
set does not contain Sachin
element 20 removed
the element of set are
Rohit    Manish    
the elements after removing
Rohit    Manish    
hashSet cleared
hashSet is empty

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程