<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## [集合Set](https://lingcoder.gitee.io/onjava8/#/book/12-Collections?id=%e9%9b%86%e5%90%88set) **Set**不保存重復的元素。 如果試圖將相同對象的多個實例添加到**Set**中,那么它會阻止這種重復行為。**Set**最常見的用途是測試歸屬性,可以很輕松地詢問某個對象是否在一個**Set**中。因此,查找通常是**Set**最重要的操作,因此通常會選擇**HashSet**實現,該實現針對快速查找進行了優化。 **Set**具有與**Collection**相同的接口,因此沒有任何額外的功能,不像前面兩種不同類型的**List**那樣。實際上,**Set**就是一個**Collection**,只是行為不同。(這是繼承和多態思想的典型應用:表現不同的行為。)**Set**根據對象的“值”確定歸屬性,更復雜的內容將在[附錄:集合主題](https://lingcoder.gitee.io/onjava8/#/)中介紹。 下面是使用存放**Integer**對象的**HashSet**的示例: ~~~ // collections/SetOfInteger.java import java.util.*; public class SetOfInteger { public static void main(String[] args) { Random rand = new Random(47); Set<Integer> intset = new HashSet<>(); for(int i = 0; i < 10000; i++) intset.add(rand.nextInt(30)); System.out.println(intset); } } /* Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29] */ ~~~ 在 0 到 29 之間的 10000 個隨機整數被添加到**Set**中,因此可以想象每個值都重復了很多次。但是從結果中可以看到,每一個數只有一個實例出現在結果中。 早期 Java 版本中的**HashSet**產生的輸出沒有可辨別的順序。這是因為出于對速度的追求,**HashSet**使用了散列,請參閱[附錄:集合主題](https://lingcoder.gitee.io/onjava8/#/)一章。由**HashSet**維護的順序與**TreeSet**或**LinkedHashSet**不同,因為它們的實現具有不同的元素存儲方式。**TreeSet**將元素存儲在紅-黑樹數據結構中,而**HashSet**使用散列函數。**LinkedHashSet**因為查詢速度的原因也使用了散列,但是看起來使用了鏈表來維護元素的插入順序。看起來散列算法好像已經改變了,現在**Integer**按順序排序。但是,您不應該依賴此行為: ~~~ // collections/SetOfString.java import java.util.*; public class SetOfString { public static void main(String[] args) { Set<String> colors = new HashSet<>(); for(int i = 0; i < 100; i++) { colors.add("Yellow"); colors.add("Blue"); colors.add("Red"); colors.add("Red"); colors.add("Orange"); colors.add("Yellow"); colors.add("Blue"); colors.add("Purple"); } System.out.println(colors); } } /* Output: [Red, Yellow, Blue, Purple, Orange] */ ~~~ **String**對象似乎沒有排序。要對結果進行排序,一種方法是使用**TreeSet**而不是**HashSet**: ~~~ // collections/SortedSetOfString.java import java.util.*; public class SortedSetOfString { public static void main(String[] args) { Set<String> colors = new TreeSet<>(); for(int i = 0; i < 100; i++) { colors.add("Yellow"); colors.add("Blue"); colors.add("Red"); colors.add("Red"); colors.add("Orange"); colors.add("Yellow"); colors.add("Blue"); colors.add("Purple"); } System.out.println(colors); } } /* Output: [Blue, Orange, Purple, Red, Yellow] */ ~~~ 最常見的操作之一是使用`contains()`測試成員歸屬性,但也有一些其它操作,這可能會讓你想起在小學學過的維恩圖(譯者注:利用圖形的交合表示多個集合之間的邏輯關系): ~~~ // collections/SetOperations.java import java.util.*; public class SetOperations { public static void main(String[] args) { Set<String> set1 = new HashSet<>(); Collections.addAll(set1, "A B C D E F G H I J K L".split(" ")); set1.add("M"); System.out.println("H: " + set1.contains("H")); System.out.println("N: " + set1.contains("N")); Set<String> set2 = new HashSet<>(); Collections.addAll(set2, "H I J K L".split(" ")); System.out.println( "set2 in set1: " + set1.containsAll(set2)); set1.remove("H"); System.out.println("set1: " + set1); System.out.println( "set2 in set1: " + set1.containsAll(set2)); set1.removeAll(set2); System.out.println( "set2 removed from set1: " + set1); Collections.addAll(set1, "X Y Z".split(" ")); System.out.println( "'X Y Z' added to set1: " + set1); } } /* Output: H: true N: false set2 in set1: true set1: [A, B, C, D, E, F, G, I, J, K, L, M] set2 in set1: false set2 removed from set1: [A, B, C, D, E, F, G, M] 'X Y Z' added to set1: [A, B, C, D, E, F, G, M, X, Y, Z] */ ~~~ 這些方法名都是自解釋的,JDK 文檔中還有一些其它的方法。 能夠產生每個元素都唯一的列表是相當有用的功能。例如,假設想要列出上面的**SetOperations.java**文件中的所有單詞,通過使用本書后面介紹的`java.nio.file.Files.readAllLines()`方法,可以打開一個文件,并將其作為一個**List**讀取,每個**String**都是輸入文件中的一行: ~~~ // collections/UniqueWords.java import java.util.*; import java.nio.file.*; public class UniqueWords { public static void main(String[] args) throws Exception { List<String> lines = Files.readAllLines( Paths.get("SetOperations.java")); Set<String> words = new TreeSet<>(); for(String line : lines) for(String word : line.split("\\W+")) if(word.trim().length() > 0) words.add(word); System.out.println(words); } } /* Output: [A, B, C, Collections, D, E, F, G, H, HashSet, I, J, K, L, M, N, Output, Set, SetOperations, String, System, X, Y, Z, add, addAll, added, args, class, collections, contains, containsAll, false, from, import, in, java, main, new, out, println, public, remove, removeAll, removed, set1, set2, split, static, to, true, util, void] */ ~~~ 我們逐步瀏覽文件中的每一行,并使用`String.split()`將其分解為單詞,這里使用正則表達式**\\\\ W +**,這意味著它會依據一個或多個(即**+**)非單詞字母來拆分字符串(正則表達式將在[字符串](https://lingcoder.gitee.io/onjava8/#/)章節介紹)。每個結果單詞都會添加到**Set words**中。因為它是**TreeSet**,所以對結果進行排序。這里,排序是按*字典順序*(lexicographically)完成的,因此大寫和小寫字母位于不同的組中。如果想按*字母順序*(alphabetically)對其進行排序,可以向**TreeSet**構造器傳入**String.CASE\_INSENSITIVE\_ORDER**比較器(比較器是一個建立排序順序的對象): ~~~ // collections/UniqueWordsAlphabetic.java // Producing an alphabetic listing import java.util.*; import java.nio.file.*; public class UniqueWordsAlphabetic { public static void main(String[] args) throws Exception { List<String> lines = Files.readAllLines( Paths.get("SetOperations.java")); Set<String> words = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); for(String line : lines) for(String word : line.split("\\W+")) if(word.trim().length() > 0) words.add(word); System.out.println(words); } } /* Output: [A, add, addAll, added, args, B, C, class, collections, contains, containsAll, D, E, F, false, from, G, H, HashSet, I, import, in, J, java, K, L, M, main, N, new, out, Output, println, public, remove, removeAll, removed, Set, set1, set2, SetOperations, split, static, String, System, to, true, util, void, X, Y, Z] */ ~~~ **Comparator**比較器將在[數組](https://lingcoder.gitee.io/onjava8/#/)章節詳細介紹。
                  <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>

                              哎呀哎呀视频在线观看