CodeGym /Java Blog /Toto sisi /Java 中的集合類
John Squirrels

San Francisco

# Java 中的集合類

``````
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);

}
}
``````

``````
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);

}
}
``````

``````
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));
}

}
}
``````

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

``````
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));

}
}
``````

TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION