CodeGym /Java Blog /Toto sisi /Java 中的集合類
John Squirrels
等級 41
San Francisco

Java 中的集合類

在 Toto sisi 群組發布
你好!在過去的幾節課中,我們對.的掌握有了很大的進步ArrayList。然而,到目前為止,我們只執行了最簡單的操作:刪除、插入和顯示。當然,這並未涵蓋開發人員在使用ArrayList. 還記得關於數組和類的課程Arrays嗎?Java 的創建者專門設計了這個類來處理程序員在使用數組時面臨的最常見任務。那又怎樣ArrayList?當然,有一些需要用它執行的常見任務列表。是不是都在具體的類中實現了,還是每次都得自己寫實現?當然,您不需要自己編寫所有內容。涉及集合的最常見操作已經在特殊靜態Collections類中實現。 收藏類 - 1 在 Java 中,一組數據結構通常被稱為集合。數據可以以許多不同的方式存儲。到目前為止,我們只研究了ArrayList數據存儲在數組中的類。我們稍後會熟悉其他集合。現在,只需要了解該類的Collections設計不僅可以與ArrayList,還有其他類型的集合(因此得名)。那麼,Collections在使用 時,班級實際上幫助完成了哪些任務ArrayList?第一個也是最明顯的是排序。在關於數組的課程中,我們考慮了一個數字示例。現在我們將考慮一個字符串示例。該類Collections實現了sort()對集合內容進行排序的方法:

public class Main {

   public static void main(java.lang.String[] args) {

       String mercury = new String("Mercury");
       String venus = new String("Venus");
       String earth = new String("Earth");
       String mars = new String("Mars");
       String jupiter = new String("Jupiter");
       String saturn = new String("Saturn");
       String uranus = new String("Uranus");
       String neptune = new String("Neptune");

       ArrayList<String> solarSystem = new ArrayList<>(Arrays.asList(mercury, venus, earth, mars,
               jupiter, saturn, uranus, neptune));
       Collections.sort(solarSystem);
       System.out.println(solarSystem);

   }
}
輸出: [Earth, Jupiter, Mars, Mercury, Neptune, Saturn, Uranus, Venus] 字符串按字母順序排序!但是為什麼要按字母順序呢?該類String實際上實現了控製字符串比較方式的邏輯(碰巧是按字母順序排列的)。對於您自己創建的類,您可以實現自己的比較邏輯,但我們將在其他課程中討論這一點。該類Collections還可以幫助您找到ArrayList. 這是使用min()max()方法完成的:

public static void main(java.lang.String[] args) {

   ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7));
   System.out.println(Collections.max(numbers));
   System.out.println(Collections.min(numbers));

}
Output: 7 1 當然,這比手動編寫代碼遍歷所有元素並找到最大/最小元素要方便得多:)另一個非常有用的方法是reverse()。如果我們必須“翻轉”列表以使元素以相反的順序排列,我們將如何做?自己編寫這樣的算法可能不會那麼容易:)幸運的是,該reverse()方法已經知道如何實現。假設我們不喜歡該sort()方法按字母順序對我們的行星進行排序的事實,並且我們想要顛倒它們的順序:從 Z 到 A:

public class Main {

   public static void main(java.lang.String[] args) {

       String mercury = new String("Mercury");
       String venus = new String("Venus");
       String earth = new String("Earth");
       String mars = new String("Mars");
       String jupiter = new String("Jupiter");
       String saturn = new String("Saturn");
       String uranus = new String("Uranus");
       String neptune = new String("Neptune");

       ArrayList<String> solarSystem = new ArrayList<>(Arrays.asList(mercury, venus, earth, mars,
               jupiter, saturn, uranus, neptune));
       Collections.sort(solarSystem);
       Collections.reverse(solarSystem);
       System.out.println(solarSystem);

   }
}
輸出: [Venus, Uranus, Saturn, Neptune, Mercury, Mars, Jupiter, Earth] 我們一直在談論排序,元素的順序等等。 但是如果我們有相反的目標呢? 例如,假設我們正在嘗試實現一個賓果遊戲。我們將 100 個數字添加到鼓中。它們應該一次一個地出現在屏幕上。第一個劃掉他票上所有數字的玩家獲勝。使用以下方法很容易實現shuffle()

public class Main {

   public static void main(java.lang.String[] args) {

       ArrayList<Integer> bingoDrum = new ArrayList<>(100);
       for (int i = 1; i <= 100; i++) {

           bingoDrum.add(i);// add the numbers 1 to 100 to the drum
       }

       Collections.shuffle(bingoDrum);// Mix it up
       System.out.println ("Your attention, please! Here are the first 10 numbers from the drum!");
       for (int i = 0; i < 10; i++) {

           System.out.println(bingoDrum.get(i));
       }

   }
}
輸出: 請注意!這是鼓的前 10 個數字!32 61 4 81 25 8 66 35 42 71 就這麼簡單!問題解決了,我們的遊戲部分就寫好了:) 現在讓我們想像一個不同的情況。之前,我們創建了一個solarSystem包含行星的列表。它似乎在所有方面都適合我們,但只有一個:您可以從中刪除項目並添加新項目!這顯然不是我們期望的行為:太陽系在我們的程序中應該是不變的。該類Collections有一個非常有趣的方法:unmodifiableList()。它從作為參數傳遞的列表中創建一個不可變列表。您不能在此列表中添加或刪除項目。在處理太陽系行星列表時,這正是我們想要的!

public class Main {

   public static void main(java.lang.String[] args) {

       String mercury = new String("Mercury");
       String venus = new String("Venus");
       String earth = new String("Earth");
       String mars = new String("Mars");
       String jupiter = new String("Jupiter");
       String saturn = new String("Saturn");
       String uranus = new String("Uranus");
       String neptune = new String("Neptune");

       List<String> solarSystem = Collections.unmodifiableList(new ArrayList<>(Arrays.asList(mercury, venus, earth, mars,
               jupiter, saturn, uranus, neptune)));
       solarSystem.add("Pluto");// Try to add a new element
   }
}
輸出: 線程“main”中的異常 java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Collections.java:1075) at Main.main(Main.java:21) 這是一個錯誤:你不能添加任何東西solarSystem!此處唯一需要注意的是此方法返回List<>(而不是ArrayList<>),因為此類型對於所有類型的列表都是通用的。另一種很容易發生的常見情況是程序員以錯誤的順序添加元素。如果出現這種情況,我們發現水星和海王星混淆了,我們可以使用以下swap()方法糾正這個錯誤:

public class Main {

   public static void main(java.lang.String[] args) {

       String mercury = new String("Mercury");
       String venus = new String("Venus");
       String earth = new String("Earth");
       String mars = new String("Mars");
       String jupiter = new String("Jupiter");
       String saturn = new String("Saturn");
       String uranus = new String("Uranus");
       String neptune = new String("Neptune");

       ArrayList<String> solarSystem = new ArrayList<>(Arrays.asList(neptune, venus, earth, mars
       , jupiter, saturn, uranus, mercury));// The planets are in the wrong order
       System.out.println(solarSystem);

       Collections.swap(solarSystem, solarSystem.indexOf(mercury), solarSystem.indexOf(neptune));
       System.out.println(solarSystem);

   }
}
swap()我們將我們的列表和需要交換的兩個元素的索引 傳遞給方法。請注意,該方法適用於索引,而不適用於引用。所以,這裡我們不得不使用ArrayList.indexOf()方法。輸出: [Neptune, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Mercury] [Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune] 最後,我們將熟悉一個非常有趣的方法disjoint()。 它檢查兩個集合是否相交,即它們是否至少有一個相同的元素。如果他們不這樣做,則返回 true。他們這樣做,然後它返回 false

public class Main {

   public static void main(java.lang.String[] args) {

       String mercury = new String("Mercury");
       String venus = new String("Venus");
       String earth = new String("Earth");
       String mars = new String("Mars");
       String jupiter = new String("Jupiter");
       String saturn = new String("Saturn");
       String uranus = new String("Uranus");
       String neptune = new String("Neptune");

       ArrayList<String> solarSystemPart1 = new ArrayList<>(Arrays.asList(mercury, venus, earth, mars));
       ArrayList<String> solarSystemPart2 = new ArrayList<>(Arrays.asList(jupiter, saturn, uranus, neptune));

       System.out.println(Collections.disjoint(solarSystemPart1, solarSystemPart2));

   }
}
如您所見,我們的兩個列表具有完全不同的元素,因此程序輸出true。這是一個有趣且非常有用的課程。就像Arrays,它為我們做了很多例行的、乏味的工作,讓我們專注於其他事情。
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION