1 排序操作
public class SortTest { public static void main(String[] args) { ArrayList nums = new ArrayList(); nums.add(5); nums.add(54); nums.add(15); nums.add(1); nums.add(35); // [5, 54, 15, 1, 35] System.out.println(nums); Collections.reverse(nums); // [35, 1, 15, 54, 5] System.out.println(nums); Collections.sort(nums); //[1, 5, 15, 35, 54] System.out.println(nums); //每次随机排序 Collections.shuffle(nums); System.out.println(nums); } }
2 查找、替换操作
public class SearchTest { public static void main(String[] args) { ArrayList nums = new ArrayList(); nums.add(2); nums.add(-5); nums.add(3); nums.add(0); System.out.println(nums); System.out.println(Collections.max(nums)); Collections.replaceAll(nums, 0, 1); System.out.println(nums); Collections.sort(nums); System.out.println(nums); } }
3 同步控制
public class SynchronizedTest { public static void main(String[] args) { Collection c=Collections.synchronizedCollection(new ArrayList()); List list=Collections.synchronizedList(new ArrayList()); } }
4 设置不可变集合
public class UnmodifiableTest { public static void main(String[] args) { // 创建空的、不可改变的对象 List unmodifiableList = Collections.emptyList(); Map scores = new HashMap(); scores.put("语文", 99); scores.put("数学", 98); Map unmodifiableMap = Collections.unmodifiableMap(scores); // java.lang.UnsupportedOperationException // unmodifiableList.add("1"); //unmodifiableMap.put("英语", 80); } }
5 斗地主小游戏
利用Collections工具类的排序操作可以写个斗地主游戏。当然这个简易版斗地主只是实现了洗牌、发牌功能。
public class ShowHand { // 支持玩家数 private final int PLAY_NUM = 3; private String[] types = { "方块", "草花", "红心", "黑桃" }; private String[] values = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" }; // 每局游戏中桌上剩下的牌,其数量不确定,且增删频繁,用LinkedList存储 private List<String> cards = new LinkedList<String>(); private String[] players = new String[PLAY_NUM]; // 玩家手中的牌 private List<String>[] playersCards = new List[PLAY_NUM]; /** * 初始化扑克牌,放入52张扑克牌 并且洗牌(随机排序) */ public void initCards() { for (int i = 0; i < types.length; i++) { for (int j = 0; j < values.length; j++) { cards.add(types[i] + values[j]); } } Collections.shuffle(cards); } /** * 初始化玩家,分配姓名 */ public void initPlayers(String... names) { if (names.length > PLAY_NUM || names.length < 2) { System.out.println("玩家数不对"); return; } else { for (int i = 0; i < names.length; i++) { players[i] = names[i]; } } } /** * 初始化玩家手中扑克牌:每个玩家开始时手中扑克牌数量为null 程序使用长度为0的LinkedList表示 */ public void initPlayerCards() { for (int i = 0; i < players.length; i++) { if (players[i] != null && !players[i].equals("")) { playersCards[i] = new LinkedList<String>(); } } } /** * 用于测试,输出全部桌上未派出扑克牌 */ public void showAllCards() { for (String card : cards) { System.out.println(card); } } /** * 派扑克牌 * * @param first * 最先派给谁 */ public void deliverCard(String first) { // first元素在数组中的下标 int firstPos = ArrayUtils.indexOf(players, first) - 1; for (int i = firstPos; i < PLAY_NUM; i++) { if (players[i] != null) { playersCards[i].add(cards.get(0)); cards.remove(0); } } for (int i = 0; i < firstPos; i++) { if (players[i] != null) { playersCards[i].add(cards.get(0)); cards.remove(0); } } } /** * 输出玩家手上的扑克牌 */ public void showPlayerCards() { for (int i = 0; i < PLAY_NUM; i++) { if (players[i] != null) { System.out.println(players[i] + ":"); for (String card : playersCards[i]) { System.out.print(card + "\t"); } } System.out.println(); } } public static void main(String[] args) { ShowHand sh = new ShowHand(); sh.initPlayers("小白", "小黄", "小黑"); sh.initCards(); sh.initPlayerCards(); sh.showAllCards(); System.out.println("----------"); sh.deliverCard("小黄"); sh.showPlayerCards(); sh.deliverCard("小黑"); sh.showPlayerCards(); } }