HashSet類的用法

來源:互聯網
上載者:User

HashSet是實現Set介面的一個類,具有以下的特點:
Ø         不能保證元素的排列順序,順序有可能發生變化。
Ø         另外HashSet不是同步的,如果多個線程同時訪問一個Set,只要有一個線程修改Set中的值,就必須進行同步處理,通常通過同步封裝這個Set的對象來完成同步,如果不存在這樣的對象,可以使用Collections.synchronizedSet()方法完成。
Set s = Collections.synchronizedSet(new HashSet(...));
Ø         元素值可以是null。
這裡使用例子來介紹各個方法的用法。
package com.li.common;
 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
 
public class HashSetTest {
 
       /**
        * @param args
        */
       public static void main(String[] args) {
              // TODO Auto-generated method stub
              HashSetTest test = new HashSetTest();
              test.testHashSet();
       }
       public void testHashSet(){
              //執行個體化HashSet對象
              HashSet hs = new HashSet();
             
              System.out.println("添加第一個元素");
              hs.add(new String("第一個元素"));
             
              System.out.println("建立一個ArrayList對象,添加兩個元素");
              ArrayList list = new ArrayList();
              list.add("第二個元素");
              list.add("第三個元素");
             
              System.out.println("把ArrayList對象添加到HashSet中");
              hs.addAll(list);
             
              System.out.println("在HashSet中添加一個元素");
              hs.add("第四個元素");
             
              System.out.println("添加一個null元素");
              hs.add(null);  
             
              System.out.println("/n通過轉換成數組遍曆的結果:");
              System.out.println("HashSet中的資料如下:");
              this.show2(hs);            
 
              System.out.println("/n通過得到Iterator遍曆的結果:");
              hs.remove("第一個元素");
              System.out.println("刪除/"第一個元素/"之後:");
              this.show1(hs);
             
              System.out.println("HashSet中元素的個數為:"+hs.size());
              if(hs.isEmpty()){
                     System.out.println("HashSet是空的");
              }
              else{
                     System.out.println("HashSet不是空的");
              }
 
              System.out.println("清空所有的元素:");
              hs.clear();
              if(hs.isEmpty()){
                     System.out.println("HashSet是空的");
              }
              else{
                     System.out.println("HashSet不是空的");
              }
       }
      
       /*
        * 得到Iterator,然後遍曆輸出
        */
       public void show1(HashSet hs){
              Iterator i = hs.iterator();
              while(i.hasNext()){
                     String temp = (String)i.next();
                     System.out.println(temp);
              }           
       }
      
       /*
        * 轉換成數組,遍曆並輸出HashSet中的元素
        */
       public void show2(HashSet hs){
              Object o[] = hs.toArray();
              for(int i=0;i<o.length;i++){
                     System.out.println((String)o[i]);
              }
       }
 
}

 

         Java中的集合(Collection)有兩類,一類是List,再有一類是Set。你知道它們的區別嗎?前者集合內的元素是有序的,元素可以重複;後者元素無序,但元素不可重複。

          1、如果兩個對象相同,那麼它們的hashCode值一定要相同;

          2、如果兩個對象的hashCode相同,它們並不一定相同

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.