CodeGym /Java 博客 /随机的 /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++) {

}

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

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

}
}``````

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