<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # `HashSet`和`HashMap`之間的區別 > 原文: [https://beginnersbook.com/2014/08/hashset-vs-hashmap-java/](https://beginnersbook.com/2014/08/hashset-vs-hashmap-java/) 在本文中,我們將討論[`HashSet`](https://beginnersbook.com/2013/12/hashset-class-in-java-with-example/)和[`HashMap`](https://beginnersbook.com/2013/12/hashmap-in-java-with-example/)類之間的差異。 ## `HashSet` vs `HashMap` **差異:** | `HashSet` | `HashMap` | | --- | --- | | `HashSet`類實現`Set`接口 | `HashMap`類實現了`Map`接口 | | 在`HashSet`中,我們存儲對象(元素或值),例如如果我們有一個字符串元素的`HashSet`,那么它可以描述一組`HashSet`元素:`{"Hello", "Hi", "Bye", "Run"}` | `HashMap`用于存儲鍵值對。簡而言之,它保持了鍵和鍵的映射。(`HashMap`類大致相當于`Hashtable`,除了它是不同步的并且允許空值。)如果`HashMap`元素具有整數鍵和`String`類型的值,則可以表示`HashMap`元素:`{1->"Hello", 2->"Hi", 3->"Bye", 4->"Run"}` | | `HashSet`不允許重復元素,這意味著您無法在`HashSet`中存儲重復值。 | `HashMap`不允許重復鍵,但它允許重復值。 | | `HashSet`允許具有單個空值。 | `HashMap`允許單個`null`鍵和任意數量的空值。 | **相似之處:** 1)`HashMap`和`HashSet`都不同步,這意味著它們不適合線程安全操作`unitl`,除非明確同步。這是你可以明確地同步它們的方法: `HashSet`: ```java Set s = Collections.synchronizedSet(new HashSet(...)); ``` `HashMap`: ```java Map m = Collections.synchronizedMap(new HashMap(...)); ``` 2)這兩個類都不保證其元素的順序會隨著時間的推移保持不變。 3)如果查看`HashSet`的源代碼,您可能會發現它由`HashMap`備份。所以基本上它在內部使用`HashMap`進行所有操作。 4)它們都為基本操作提供恒定的時間性能,例如添加,刪除元素等。 #### `HashSet`示例 ```java import java.util.HashSet; class HashSetDemo{ public static void main(String[] args) { // Create a HashSet HashSet<String> hset = new HashSet<String>(); //add elements to HashSet hset.add("AA"); hset.add("BB"); hset.add("CC"); hset.add("DD"); // Displaying HashSet elements System.out.println("HashSet contains: "); for(String temp : hset){ System.out.println(temp); } } } ``` **輸出:** ```java HashSet contains: AA BB CC DD ``` #### `HashMap`示例 ```java import java.util.HashMap; class HashMapDemo{ public static void main(String[] args) { // Create a HashMap HashMap<Integer, String> hmap = new HashMap<Integer, String>(); //add elements to HashMap hmap.put(1, "AA"); hmap.put(2, "BB"); hmap.put(3, "CC"); hmap.put(4, "DD"); // Displaying HashMap elements System.out.println("HashMap contains: "+hmap); } } ``` **輸出:** ```java HashMap contains: {1=AA, 2=BB, 3=CC, 4=DD} ``` #### 參考: [`HashSet` javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html) [`HashMap` javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看