📜  Java HashSet

📅  最后修改于: 2020-10-12 09:41:10             🧑  作者: Mango

Java HashSet

Java HashSet类用于创建使用哈希表进行存储的集合。它继承了AbstractSet类并实现Set接口。

关于Java HashSet类的要点是:

  • HashSet通过使用一种称为哈希的机制来存储元素
  • HashSet仅包含唯一元素。
  • HashSet允许为空值。
  • HashSet类不同步。
  • HashSet不会保持插入顺序。在这里,元素是根据其哈希码插入的。
  • HashSet是搜索操作的最佳方法。
  • HashSet的初始默认容量为16,而负载系数为0.75。

列表和集合之间的区别

列表可以包含重复元素,而Set仅包含唯一元素。

HashSet类的层次结构

HashSet类扩展了实现Set接口的AbstractSet类。 Set接口按层次结构顺序继承Collection和Iterable接口。

HashSet类声明

我们来看一下java.util.HashSet类的声明。

public class HashSet extends AbstractSet implements Set, Cloneable, Serializable

Java HashSet类的构造方法

SN Constructor Description
1) HashSet() It is used to construct a default HashSet.
2) HashSet(int capacity) It is used to initialize the capacity of the hash set to the given integer value capacity. The capacity grows automatically as elements are added to the HashSet.
3) HashSet(int capacity, float loadFactor) It is used to initialize the capacity of the hash set to the given integer value capacity and the specified load factor.
4) HashSet(Collection c) It is used to initialize the hash set by using the elements of the collection c.

Java HashSet类的方法

Java HashSet类的各种方法如下:

SN Modifier & Type Method Description
1) boolean add(E e) It is used to add the specified element to this set if it is not already present.
2) void clear() It is used to remove all of the elements from the set.
3) object clone() It is used to return a shallow copy of this HashSet instance: the elements themselves are not cloned.
4) boolean contains(Object o) It is used to return true if this set contains the specified element.
5) boolean isEmpty() It is used to return true if this set contains no elements.
6) Iterator iterator() It is used to return an iterator over the elements in this set.
7) boolean remove(Object o) It is used to remove the specified element from this set if it is present.
8) int size() It is used to return the number of elements in the set.
9) Spliterator spliterator() It is used to create a late-binding and fail-fast Spliterator over the elements in the set.

Java HashSet示例

让我们看一个简单的HashSet示例。注意,元素在无序集合中进行迭代。

import java.util.*;
class HashSet1{
 public static void main(String args[]){
  //Creating HashSet and adding elements
    HashSet set=new HashSet();
   set.add("One");  
   set.add("Two");  
   set.add("Three"); 
   set.add("Four");
   set.add("Five");
   Iterator i=set.iterator();
   while(i.hasNext())
   {
   System.out.println(i.next());
   }
 }
}
Five
One
Four
Two
Three

Java HashSet示例忽略重复的元素

在此示例中,我们看到HashSet不允许重复的元素。

import java.util.*;
class HashSet2{
 public static void main(String args[]){
  //Creating HashSet and adding elements
  HashSet set=new HashSet();
  set.add("Ravi");
  set.add("Vijay");
  set.add("Ravi");
  set.add("Ajay");
  //Traversing elements
  Iterator itr=set.iterator();
  while(itr.hasNext()){
   System.out.println(itr.next());
  }
 }
}
       Ajay
       Vijay
       Ravi

Java HashSet示例删除元素

在这里,我们看到了删除元素的不同方法。

import java.util.*;
class HashSet3{
 public static void main(String args[]){
  HashSet set=new HashSet();
   set.add("Ravi");
   set.add("Vijay");
   set.add("Arun");
   set.add("Sumit");
   System.out.println("An initial list of elements: "+set);
   //Removing specific element from HashSet
   set.remove("Ravi");
   System.out.println("After invoking remove(object) method: "+set);
   HashSet set1=new HashSet();
   set1.add("Ajay");
   set1.add("Gaurav");
   set.addAll(set1);
   System.out.println("Updated List: "+set);
   //Removing all the new elements from HashSet
   set.removeAll(set1);
   System.out.println("After invoking removeAll() method: "+set);
   //Removing elements on the basis of specified condition
   set.removeIf(str->str.contains("Vijay"));  
   System.out.println("After invoking removeIf() method: "+set);
   //Removing all the elements available in the set
   set.clear();
   System.out.println("After invoking clear() method: "+set);
 }
}
An initial list of elements: [Vijay, Ravi, Arun, Sumit]
After invoking remove(object) method: [Vijay, Arun, Sumit]
Updated List: [Vijay, Arun, Gaurav, Sumit, Ajay]
After invoking removeAll() method: [Vijay, Arun, Sumit]
After invoking removeIf() method: [Arun, Sumit]
After invoking clear() method: []

来自其他集合的Java HashSet

import java.util.*;
class HashSet4{
 public static void main(String args[]){
   ArrayList list=new ArrayList();
   list.add("Ravi");
   list.add("Vijay");
   list.add("Ajay");
   
   HashSet set=new HashSet(list);
   set.add("Gaurav");
   Iterator i=set.iterator();
   while(i.hasNext())
   {
   System.out.println(i.next());
   }
 }
}
Vijay
Ravi
Gaurav
Ajay

Java HashSet示例:书

让我们看一个HashSet示例,其中我们要添加书籍以进行设置并打印所有书籍。

import java.util.*;
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
public class HashSetExample {
public static void main(String[] args) {
HashSet set=new HashSet();
//Creating Books
Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);
Book b3=new Book(103,"Operating System","Galvin","Wiley",6);
//Adding Books to HashSet
set.add(b1);
set.add(b2);
set.add(b3);
//Traversing HashSet
for(Book b:set){
System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);
}
}
}

输出:

101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6

你可能也会喜欢: