Utilities
Collections Utility Class
When working with Lists, Sets, and other collections, you frequently need to sort, shuffle, or find extreme values. The Collections utility class provides static methods for these operations, plus ways to create thread-safe and unmodifiable collection views.
Collections class
A utility class in java.util providing static methods for collection manipulation, distinct from the Collection interface.
Sorting Collections
Sort lists using natural ordering or custom comparators.
Sort.java
Replay: real traced execution (multi-file project)
// Collections.sort examples
import java.util.*;
public class Sort {
public static void main(String[] args) {
System.out.println("Sort list:");
List<Integer> nums = new ArrayList<>(Arrays.asList(5, 2, 8, 1, 9, 3));
System.out.println("Original: " + nums);
Collections.sort(nums);
System.out.println("Sorted: " + nums);
System.out.println("\nSort strings:");
List<String> fruits = new ArrayList<>(Arrays.asList("banana", "apple", "cherry", "date"));
System.out.println("Original: " + fruits);
Collections.sort(fruits);
System.out.println("Sorted: " + fruits);
System.out.println("\nSort with comparator:");
List<String> words = new ArrayList<>(Arrays.asList("cat", "elephant", "dog", "butterfly"));
System.out.println("Original: " + words);
Collections.sort(words, (a, b) -> a.length() - b.length());
System.out.println("By length: " + words);
System.out.println("\nReverse order:");
List<Integer> values = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println("Original: " + values);
Collections.sort(values, Collections.reverseOrder());
System.out.println("Reversed: " + values);
System.out.println("\nCase-insensitive sort:");
List<String> names = new ArrayList<>(Arrays.asList("alice", "Bob", "CHARLIE", "david"));
System.out.println("Original: " + names);
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
System.out.println("Sorted: " + names);
System.out.println("\nSort custom objects:");
List<Student> students = new ArrayList<>(Arrays.asList(
new Student("Alice", 85),
new Student("Bob", 92),
new Student("Charlie", 78),
new Student("David", 95)
));
System.out.println("Original:");
students.forEach(System.out::println);
Collections.sort(students, (a, b) -> Integer.compare(a.grade, b.grade));
System.out.println("\nSorted by grade:");
students.forEach(System.out::println);
System.out.println("\nStable sort:");
List<Student> students2 = new ArrayList<>(Arrays.asList(
new Student("Alice", 85),
new Student("Bob", 85),
new Student("Charlie", 92),
new Student("David", 85)
));
System.out.println("Original:");
students2.forEach(System.out::println);
Collections.sort(students2, (a, b) -> Integer.compare(a.grade, b.grade));
System.out.println("\nSorted (stable):");
students2.forEach(System.out::println);
System.out.println("\nMultiple sort criteria:");
List<Student> students3 = new ArrayList<>(Arrays.asList(
new Student("Alice", 85),
new Student("Bob", 92),
new Student("Charlie", 85),
new Student("David", 92)
));
Comparator<Student> byGradeThenName =
Comparator.comparing((Student s) -> s.grade)
.thenComparing(s -> s.name);
Collections.sort(students3, byGradeThenName);
System.out.println("Sorted by grade, then name:");
students3.forEach(System.out::println);
}
static class Student {
String name;
int grade;
Student(String name, int grade) {
this.name = name;
this.grade = grade;
}
@Override
public String toString() {
return String.format(" %s: %d", name, grade);
}
}
}
nums ← [5, 2, 8, 1, 9, 3], fruits ← [banana, apple, cherry, date]
5public class Sort {6 public static void main(String[] args) {7 System.out.println("Sort list:");8 List<Integer> nums→ [5, 2, 8, 1, 9, 3] = new ArrayList<>(Arrays.asList(5, 2, 8, 1, 9, 3));9 System.out.println("Original: " + nums[5, 2, 8, 1, 9, 3]);1011 Collections.sort(nums→ [1, 2, 3, 5, 8, 9]);12 System.out.println("Sorted: " + nums[1, 2, 3, 5, 8, 9]);13 System.out.println("\nSort strings:");14 List<String> fruits→ [banana, apple, cherry, date] = new ArrayList<>(Arrays.asList("banana", "apple", "cherry", "date"));15 System.out.println("Original: " + fruits[banana, apple, cherry, date]);1617 Collections.sort(fruits→ [apple, banana, cherry, date]);18 System.out.println("Sorted: " + fruits[apple, banana, cherry, date]);19 System.out.println("\nSort with comparator:");20 List<String> words→ [cat, elephant, dog, butterfly] = new ArrayList<>(Arrays.asList("cat", "elephant", "dog", "butterfly"));21 System.out.println("Original: " + words[cat, elephant, dog, butterfly]);2223 Collections.sort(words→ [cat, dog, elephant, butterfly], (a, b) -> a.length() - b.length());24 System.out.println("By length: " + words[cat, dog, elephant, butterfly]);25 System.out.println("\nReverse order:");26 List<Integer> values→ [1, 2, 3, 4, 5] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));27 System.out.println("Original: " + values[1, 2, 3, 4, 5]);2829 Collections.sort(values→ [5, 4, 3, 2, 1], Collections.reverseOrder());30 System.out.println("Reversed: " + values[5, 4, 3, 2, 1]);31 System.out.println("\nCase-insensitive sort:");32 List<String> names→ [alice, Bob, CHARLIE, david] = new ArrayList<>(Arrays.asList("alice", "Bob", "CHARLIE", "david"));33 System.out.println("Original: " + names[alice, Bob, CHARLIE, david]);3435 Collections.sort(names[alice, Bob, CHARLIE, david], String.CASE_INSENSITIVE_ORDER);36 System.out.println("Sorted: " + names[alice, Bob, CHARLIE, david]);37 System.out.println("\nSort custom objects:");38 List<Student> students = new ArrayList<>(Arrays.asList(39 new Student("Alice", 85),40 new Student("Bob", 92),41 new Student("Charlie", 78),42 new Student("David", 95)43 ));outputSort list: Original: [5, 2, 8, 1, 9, 3] Sorted: [1, 2, 3, 5, 8, 9] Sort strings: Original: [banana, apple, cherry, date] Sorted: [apple, banana, cherry, date] Sort with comparator: Original: [cat, elephant, dog, butterfly] By length: [cat, dog, elephant, butterfly] Reverse order: Original: [1, 2, 3, 4, 5] Reversed: [5, 4, 3, 2, 1] Case-insensitive sort: Original: [alice, Bob, CHARLIE, david] Sorted: [alice, Bob, CHARLIE, david] Sort custom objects:this.name ← Alice, this.grade ← 85
pass 1 of 1286Student(String nameAlice, int grade85) {87 this.name→ Alice = nameAlice;88 this.grade→ 85 = grade85;89}All 12 passes — pass 1 is the card above pass namegradethis.namethis.gradestudentsstudents2students3byGradeThenName1 Alice 85 Alice 85 — — — — 2 Bob 92 Bob 92 — — — — 3 Charlie 78 Charlie 78 — — — — 4 David 95 David 95 [ Alice: 85, Bob: 92, Charlie: 78, David: 95] — — — 5 Alice 85 Alice 85 — — — — 6 Bob 85 Bob 85 — — — — 7 Charlie 92 Charlie 92 — — — — 8 David 85 David 85 — [ Alice: 85, Bob: 85, Charlie: 92, David: 85] — — 9 Alice 85 Alice 85 — — — — 10 Bob 92 Bob 92 — — — — 11 Charlie 85 Charlie 85 — — — — 12 David 92 David 92 — — [ Alice: 85, Bob: 92, Charlie: 85, David: 92] ⟨Comparator lambda A⟩
Natural ordering
The default sort order defined by the element's Comparable implementation, like alphabetical for strings.
Reversing Collections
Reverse the order of elements in a list.
Reverse.java
Replay: real traced execution (multi-file project)
// Collections.reverse and rotate examples
import java.util.*;
public class Reverse {
public static void main(String[] args) {
System.out.println("Reverse list:");
List<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println("Original: " + nums);
Collections.reverse(nums);
System.out.println("Reversed: " + nums);
System.out.println("\nReverse strings:");
List<String> words = new ArrayList<>(Arrays.asList("first", "second", "third", "fourth"));
System.out.println("Original: " + words);
Collections.reverse(words);
System.out.println("Reversed: " + words);
System.out.println("\nReverse twice:");
List<Integer> values = new ArrayList<>(Arrays.asList(10, 20, 30, 40));
System.out.println("Original: " + values);
Collections.reverse(values);
System.out.println("Reversed: " + values);
Collections.reverse(values);
System.out.println("Reversed again: " + values);
System.out.println("\nRotate list:");
List<Integer> rotate = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println("Original: " + rotate);
Collections.rotate(rotate, 2);
System.out.println("Rotate +2: " + rotate);
Collections.rotate(rotate, -2);
System.out.println("Rotate -2: " + rotate);
System.out.println("\nRotate examples:");
List<String> days = new ArrayList<>(Arrays.asList("Mon", "Tue", "Wed", "Thu", "Fri"));
System.out.println("Days: " + days);
Collections.rotate(days, 1);
System.out.println("Rotate 1: " + days);
Collections.rotate(days, -3);
System.out.println("Rotate -3: " + days);
System.out.println("\nSwap elements:");
List<Integer> list = new ArrayList<>(Arrays.asList(10, 20, 30, 40, 50));
System.out.println("Original: " + list);
Collections.swap(list, 0, 4);
System.out.println("Swap 0,4: " + list);
Collections.swap(list, 1, 3);
System.out.println("Swap 1,3: " + list);
System.out.println("\nPalindrome check:");
List<Integer> pal1 = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 1));
List<Integer> pal2 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println("List 1: " + pal1);
System.out.println("Palindrome? " + isPalindrome(pal1));
System.out.println("List 2: " + pal2);
System.out.println("Palindrome? " + isPalindrome(pal2));
System.out.println("\nCycle through list:");
List<String> queue = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));
System.out.println("Queue: " + queue);
for (int i = 0; i < 3; i++) {
String first = queue.remove(0);
queue.add(first);
System.out.println("Cycle: " + queue);
}
System.out.println("\nReverse sublist:");
List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
System.out.println("Original: " + numbers);
List<Integer> sublist = numbers.subList(2, 6);
Collections.reverse(sublist);
System.out.println("Reverse [2,6): " + numbers);
}
static <T> boolean isPalindrome(List<T> list) {
List<T> reversed = new ArrayList<>(list);
Collections.reverse(reversed);
return list.equals(reversed);
}
}
nums ← [1, 2, 3, 4, 5], words ← [first, second, third, fourth]
5public class Reverse {6 public static void main(String[] args) {7 System.out.println("Reverse list:");8 List<Integer> nums→ [1, 2, 3, 4, 5] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));9 System.out.println("Original: " + nums[1, 2, 3, 4, 5]);1011 Collections.reverse(nums→ [5, 4, 3, 2, 1]);12 System.out.println("Reversed: " + nums[5, 4, 3, 2, 1]);13 System.out.println("\nReverse strings:");14 List<String> words→ [first, second, third, fourth] = new ArrayList<>(Arrays.asList("first", "second", "third", "fourth"));15 System.out.println("Original: " + words[first, second, third, fourth]);1617 Collections.reverse(words→ [fourth, third, second, first]);18 System.out.println("Reversed: " + words[fourth, third, second, first]);19 System.out.println("\nReverse twice:");20 List<Integer> values→ [10, 20, 30, 40] = new ArrayList<>(Arrays.asList(10, 20, 30, 40));21 System.out.println("Original: " + values[10, 20, 30, 40]);2223 Collections.reverse(values→ [40, 30, 20, 10]);24 System.out.println("Reversed: " + values[40, 30, 20, 10]);2526 Collections.reverse(values→ [10, 20, 30, 40]);27 System.out.println("Reversed again: " + values[10, 20, 30, 40]);28 System.out.println("\nRotate list:");29 List<Integer> rotate→ [1, 2, 3, 4, 5] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));30 System.out.println("Original: " + rotate[1, 2, 3, 4, 5]);3132 Collections.rotate(rotate→ [4, 5, 1, 2, 3], 2);33 System.out.println("Rotate +2: " + rotate[4, 5, 1, 2, 3]);3435 Collections.rotate(rotate→ [1, 2, 3, 4, 5], -2);36 System.out.println("Rotate -2: " + rotate[1, 2, 3, 4, 5]);37 System.out.println("\nRotate examples:");38 List<String> days→ [Mon, Tue, Wed, Thu, Fri] = new ArrayList<>(Arrays.asList("Mon", "Tue", "Wed", "Thu", "Fri"));3940 System.out.println("Days: " + days[Mon, Tue, Wed, Thu, Fri]);41 Collections.rotate(days→ [Fri, Mon, Tue, Wed, Thu], 1);42 System.out.println("Rotate 1: " + days[Fri, Mon, Tue, Wed, Thu]);4344 Collections.rotate(days→ [Wed, Thu, Fri, Mon, Tue], -3);45 System.out.println("Rotate -3: " + days[Wed, Thu, Fri, Mon, Tue]);46 System.out.println("\nSwap elements:");47 List<Integer> list→ [10, 20, 30, 40, 50] = new ArrayList<>(Arrays.asList(10, 20, 30, 40, 50));48 System.out.println("Original: " + list[10, 20, 30, 40, 50]);4950 Collections.swap(list→ [50, 20, 30, 40, 10], 0, 4);51 System.out.println("Swap 0,4: " + list[50, 20, 30, 40, 10]);5253 Collections.swap(list→ [50, 40, 30, 20, 10], 1, 3);54 System.out.println("Swap 1,3: " + list[50, 40, 30, 20, 10]);55 System.out.println("\nPalindrome check:");56 List<Integer> pal1→ [1, 2, 3, 2, 1] = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 1));57 List<Integer> pal2→ [1, 2, 3, 4, 5] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));5859 System.out.println("List 1: " + pal1[1, 2, 3, 2, 1]);60 System.out.println("Palindrome? " + isPalindrome(pal1[1, 2, 3, 2, 1]));outputReverse list: Original: [1, 2, 3, 4, 5] Reversed: [5, 4, 3, 2, 1] Reverse strings: Original: [first, second, third, fourth] Reversed: [fourth, third, second, first] Reverse twice: Original: [10, 20, 30, 40] Reversed: [40, 30, 20, 10] Reversed again: [10, 20, 30, 40] Rotate list: Original: [1, 2, 3, 4, 5] Rotate +2: [4, 5, 1, 2, 3] Rotate -2: [1, 2, 3, 4, 5] Rotate examples: Days: [Mon, Tue, Wed, Thu, Fri] Rotate 1: [Fri, Mon, Tue, Wed, Thu] Rotate -3: [Wed, Thu, Fri, Mon, Tue] Swap elements: Original: [10, 20, 30, 40, 50] Swap 0,4: [50, 20, 30, 40, 10] Swap 1,3: [50, 40, 30, 20, 10] Palindrome check: List 1: [1, 2, 3, 2, 1]reversed ← [1, 2, 3, 2, 1]
pass 1 of 282static <T> boolean isPalindrome(List<T> list[1, 2, 3, 2, 1]) {83 List<T> reversed→ [1, 2, 3, 2, 1] = new ArrayList<>(list);84 Collections.reverse(reversed[1, 2, 3, 2, 1]);85 return list.equals(reversed[1, 2, 3, 2, 1]);86}System.out.println("Palindrome? " + isPalindrome(pal1));
59System.out.println("List 1: " + pal1);60System.out.println("Palindrome? " + isPalindrome(pal1[1, 2, 3, 2, 1]));6162System.out.println("List 2: " + pal2[1, 2, 3, 4, 5]);63System.out.println("Palindrome? " + isPalindrome(pal2[1, 2, 3, 4, 5]));64System.out.println("\nCycle through list:");outputPalindrome? true List 2: [1, 2, 3, 4, 5]reversed ← [1, 2, 3, 4, 5]
pass 2 of 282static <T> boolean isPalindrome(List<T> list[1, 2, 3, 4, 5]) {83 List<T> reversed→ [1, 2, 3, 4, 5] = new ArrayList<>(list);84 Collections.reverse(reversed→ [5, 4, 3, 2, 1]);85 return list.equals(reversed[5, 4, 3, 2, 1]);86}queue ← [A, B, C, D, E]
62System.out.println("List 2: " + pal2);63System.out.println("Palindrome? " + isPalindrome(pal2[1, 2, 3, 4, 5]));64System.out.println("\nCycle through list:");65List<String> queue→ [A, B, C, D, E] = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));6667System.out.println("Queue: " + queue[A, B, C, D, E]);68for (int i = 0; i < 3; i++) {outputPalindrome? false Cycle through list: Queue: [A, B, C, D, E]first ← A
pass 1 of 367System.out.println("Queue: " + queue);68for (int i0 = 0; i < 3; i++) {69 String first→ A = queue.remove(0);70 queue.add(firstA);71 System.out.println("Cycle: " + queue[B, C, D, E, A]);72}outputCycle: [B, C, D, E, A]All 3 passes — pass 1 is the card above pass iqueuefirst1 0 [B, C, D, E, A] A 2 1 [C, D, E, A, B] B 3 2 [D, E, A, B, C] C numbers ← [1, 2, 3, 4, 5, 6, 7, 8], sublist ← [3, 4, 5, 6]
72 }73 System.out.println("\nReverse sublist:");74 List<Integer> numbers→ [1, 2, 3, 4, 5, 6, 7, 8] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));75 System.out.println("Original: " + numbers[1, 2, 3, 4, 5, 6, 7, 8]);7677 List<Integer> sublist→ [3, 4, 5, 6] = numbers.subList(2, 6);78 Collections.reverse(sublist→ [6, 5, 4, 3]);79 System.out.println("Reverse [2,6): " + numbers[1, 2, 6, 5, 4, 3, 7, 8]);80}output Reverse sublist: Original: [1, 2, 3, 4, 5, 6, 7, 8] Reverse [2,6): [1, 2, 6, 5, 4, 3, 7, 8]
Shuffling Collections
Randomly reorder elements for games, sampling, or testing.
Shuffle.java
Replay: real traced execution (multi-file project)
// Collections.shuffle and fill examples
import java.util.*;
public class Shuffle {
public static void main(String[] args) {
int seed = 42;
System.out.println("Shuffle list:");
List<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
System.out.println("Original: " + nums);
Collections.shuffle(nums, new Random(seed));
System.out.println("Shuffled: " + nums);
System.out.println("\nShuffle with seed:");
List<Integer> nums1 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
List<Integer> nums2 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
Random rand = new Random(seed);
Collections.shuffle(nums1, rand);
rand = new Random(seed);
Collections.shuffle(nums2, rand);
System.out.println("Shuffle 1: " + nums1);
System.out.println("Shuffle 2: " + nums2);
System.out.println("Same? " + nums1.equals(nums2));
System.out.println("\nShuffle deck:");
List<String> deck = new ArrayList<>();
String[] suits = {"♠", "♥", "♦", "♣"};
String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for (String suit : suits) {
for (String rank : ranks) {
deck.add(rank + suit);
}
}
System.out.println("Deck size: " + deck.size());
System.out.println("First 13: " + deck.subList(0, 13));
Collections.shuffle(deck, new Random(seed + 1));
System.out.println("After shuffle: " + deck.subList(0, 13));
System.out.println("\nDeal cards:");
Collections.shuffle(deck, new Random(seed + 2));
for (int player = 0; player < 4; player++) {
List<String> hand = deck.subList(player * 5, (player + 1) * 5);
System.out.println("Player " + (player + 1) + ": " + hand);
}
System.out.println("\nFill list:");
List<Integer> values = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println("Original: " + values);
Collections.fill(values, 0);
System.out.println("Filled with 0: " + values);
System.out.println("\nFill strings:");
List<String> words = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
System.out.println("Original: " + words);
Collections.fill(words, "X");
System.out.println("Filled: " + words);
System.out.println("\nInitialize with fill:");
List<Boolean> flags = new ArrayList<>(10);
for (int i = 0; i < 10; i++) flags.add(null);
Collections.fill(flags, true);
System.out.println("Flags: " + flags);
System.out.println("\nRandom sample:");
List<Integer> population = new ArrayList<>();
for (int i = 1; i <= 100; i++) {
population.add(i);
}
Collections.shuffle(population, new Random(seed + 3));
List<Integer> sample = population.subList(0, 10);
Collections.sort(sample);
System.out.println("Random sample of 10: " + sample);
System.out.println("\nLottery numbers:");
List<Integer> lottery = new ArrayList<>();
for (int i = 1; i <= 49; i++) {
lottery.add(i);
}
Collections.shuffle(lottery, new Random(seed + 4));
List<Integer> picked = new ArrayList<>(lottery.subList(0, 6));
Collections.sort(picked);
System.out.println("Lottery numbers: " + picked);
System.out.println("\nShuffle multiple times:");
List<String> items = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));
System.out.println("Original: " + items);
for (int i = 0; i < 3; i++) {
Collections.shuffle(items, new Random(seed + 5 + i));
System.out.println("Shuffle " + (i + 1) + ": " + items);
}
}
}
// Collections.shuffle and fill examples
import java.util.*;
public class Shuffle {
public static void main(String[] args) {
int seed = 7;
System.out.println("Shuffle list:");
List<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
System.out.println("Original: " + nums);
Collections.shuffle(nums, new Random(seed));
System.out.println("Shuffled: " + nums);
System.out.println("\nShuffle with seed:");
List<Integer> nums1 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
List<Integer> nums2 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
Random rand = new Random(seed);
Collections.shuffle(nums1, rand);
rand = new Random(seed);
Collections.shuffle(nums2, rand);
System.out.println("Shuffle 1: " + nums1);
System.out.println("Shuffle 2: " + nums2);
System.out.println("Same? " + nums1.equals(nums2));
System.out.println("\nShuffle deck:");
List<String> deck = new ArrayList<>();
String[] suits = {"♠", "♥", "♦", "♣"};
String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for (String suit : suits) {
for (String rank : ranks) {
deck.add(rank + suit);
}
}
System.out.println("Deck size: " + deck.size());
System.out.println("First 13: " + deck.subList(0, 13));
Collections.shuffle(deck, new Random(seed + 1));
System.out.println("After shuffle: " + deck.subList(0, 13));
System.out.println("\nDeal cards:");
Collections.shuffle(deck, new Random(seed + 2));
for (int player = 0; player < 4; player++) {
List<String> hand = deck.subList(player * 5, (player + 1) * 5);
System.out.println("Player " + (player + 1) + ": " + hand);
}
System.out.println("\nFill list:");
List<Integer> values = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println("Original: " + values);
Collections.fill(values, 0);
System.out.println("Filled with 0: " + values);
System.out.println("\nFill strings:");
List<String> words = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
System.out.println("Original: " + words);
Collections.fill(words, "X");
System.out.println("Filled: " + words);
System.out.println("\nInitialize with fill:");
List<Boolean> flags = new ArrayList<>(10);
for (int i = 0; i < 10; i++) flags.add(null);
Collections.fill(flags, true);
System.out.println("Flags: " + flags);
System.out.println("\nRandom sample:");
List<Integer> population = new ArrayList<>();
for (int i = 1; i <= 100; i++) {
population.add(i);
}
Collections.shuffle(population, new Random(seed + 3));
List<Integer> sample = population.subList(0, 10);
Collections.sort(sample);
System.out.println("Random sample of 10: " + sample);
System.out.println("\nLottery numbers:");
List<Integer> lottery = new ArrayList<>();
for (int i = 1; i <= 49; i++) {
lottery.add(i);
}
Collections.shuffle(lottery, new Random(seed + 4));
List<Integer> picked = new ArrayList<>(lottery.subList(0, 6));
Collections.sort(picked);
System.out.println("Lottery numbers: " + picked);
System.out.println("\nShuffle multiple times:");
List<String> items = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));
System.out.println("Original: " + items);
for (int i = 0; i < 3; i++) {
Collections.shuffle(items, new Random(seed + 5 + i));
System.out.println("Shuffle " + (i + 1) + ": " + items);
}
}
}
// Collections.shuffle and fill examples
import java.util.*;
public class Shuffle {
public static void main(String[] args) {
int seed = 12345;
System.out.println("Shuffle list:");
List<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
System.out.println("Original: " + nums);
Collections.shuffle(nums, new Random(seed));
System.out.println("Shuffled: " + nums);
System.out.println("\nShuffle with seed:");
List<Integer> nums1 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
List<Integer> nums2 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
Random rand = new Random(seed);
Collections.shuffle(nums1, rand);
rand = new Random(seed);
Collections.shuffle(nums2, rand);
System.out.println("Shuffle 1: " + nums1);
System.out.println("Shuffle 2: " + nums2);
System.out.println("Same? " + nums1.equals(nums2));
System.out.println("\nShuffle deck:");
List<String> deck = new ArrayList<>();
String[] suits = {"♠", "♥", "♦", "♣"};
String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for (String suit : suits) {
for (String rank : ranks) {
deck.add(rank + suit);
}
}
System.out.println("Deck size: " + deck.size());
System.out.println("First 13: " + deck.subList(0, 13));
Collections.shuffle(deck, new Random(seed + 1));
System.out.println("After shuffle: " + deck.subList(0, 13));
System.out.println("\nDeal cards:");
Collections.shuffle(deck, new Random(seed + 2));
for (int player = 0; player < 4; player++) {
List<String> hand = deck.subList(player * 5, (player + 1) * 5);
System.out.println("Player " + (player + 1) + ": " + hand);
}
System.out.println("\nFill list:");
List<Integer> values = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println("Original: " + values);
Collections.fill(values, 0);
System.out.println("Filled with 0: " + values);
System.out.println("\nFill strings:");
List<String> words = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
System.out.println("Original: " + words);
Collections.fill(words, "X");
System.out.println("Filled: " + words);
System.out.println("\nInitialize with fill:");
List<Boolean> flags = new ArrayList<>(10);
for (int i = 0; i < 10; i++) flags.add(null);
Collections.fill(flags, true);
System.out.println("Flags: " + flags);
System.out.println("\nRandom sample:");
List<Integer> population = new ArrayList<>();
for (int i = 1; i <= 100; i++) {
population.add(i);
}
Collections.shuffle(population, new Random(seed + 3));
List<Integer> sample = population.subList(0, 10);
Collections.sort(sample);
System.out.println("Random sample of 10: " + sample);
System.out.println("\nLottery numbers:");
List<Integer> lottery = new ArrayList<>();
for (int i = 1; i <= 49; i++) {
lottery.add(i);
}
Collections.shuffle(lottery, new Random(seed + 4));
List<Integer> picked = new ArrayList<>(lottery.subList(0, 6));
Collections.sort(picked);
System.out.println("Lottery numbers: " + picked);
System.out.println("\nShuffle multiple times:");
List<String> items = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));
System.out.println("Original: " + items);
for (int i = 0; i < 3; i++) {
Collections.shuffle(items, new Random(seed + 5 + i));
System.out.println("Shuffle " + (i + 1) + ": " + items);
}
}
}
seed ← 42, nums ← [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], nums1 ← [1, 2, 3, 4, 5]
5public class Shuffle {6 public static void main(String[] args) {7 int seed→ 42 = 42; //@seed=7, 1234589 System.out.println("Shuffle list:");10 List<Integer> nums→ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));11 System.out.println("Original: " + nums[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);1213 Collections.shuffle(nums→ [5, 7, 3, 2, 8, 10, 9, 6, 4, 1], new Random(seed));14 System.out.println("Shuffled: " + nums[5, 7, 3, 2, 8, 10, 9, 6, 4, 1]);15 System.out.println("\nShuffle with seed:");16 List<Integer> nums1→ [1, 2, 3, 4, 5] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));17 List<Integer> nums2→ [1, 2, 3, 4, 5] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));1819 Random rand→ ⟨Random A⟩ = new Random(seed);20 Collections.shuffle(nums1→ [2, 3, 4, 5, 1], rand⟨Random A⟩);2122 rand→ ⟨Random B⟩ = new Random(seed);23 Collections.shuffle(nums2→ [2, 3, 4, 5, 1], rand⟨Random B⟩);2425 System.out.println("Shuffle 1: " + nums1[2, 3, 4, 5, 1]);26 System.out.println("Shuffle 2: " + nums2[2, 3, 4, 5, 1]);27 System.out.println("Same? " + nums1.equals(nums2[2, 3, 4, 5, 1]));28 System.out.println("\nShuffle deck:");29 List<String> deck→ [] = new ArrayList<>();30 String[] suits = {"♠", "♥", "♦", "♣"};31 String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};outputShuffle list: Original: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Shuffled: [5, 7, 3, 2, 8, 10, 9, 6, 4, 1] Shuffle with seed: Shuffle 1: [2, 3, 4, 5, 1] Shuffle 2: [2, 3, 4, 5, 1] Same? true Shuffle deck:for (String suit : suits)
pass 1 of 433for (String suit♠ : suits) {34 for (String rank : ranks) {All 4 passes — pass 1 is the card above pass suit1 ♠ 2 ♥ 3 ♦ 4 ♣ for (String rank : ranks)
pass 1 of 5233for (String suit : suits) {34 for (String rankA : ranks) {35 deck.add(rankA + suit♠);36 }52 passes — pass 1 is the card above pass ranksuit1 A ♠ 2 2 ♠ 3 3 ♠ 4 4 ♠ 5 5 ♠ 6 6 ♠ 7 7 ♠ 8 8 ♠ 9 9 ♠ ⋯ 41 more passes ⋯ 51 Q ♣ 52 K ♣ deck ← [10♥, 5♣, 6♣, 2♦, 3♥, 7♣, 3♣, 5♦, 10♦, 2♥, 6♦, 8♣, 10♣, 4♦, 9♣, J♣, 6♥, Q♣, 4♣, A♥, 2♣, Q♠, A♣, K♠, 8♠, 4♥, K♥, J♦, 8♦, 9♦, K♦, 2♠, 9♥, 9♠, J♥, Q♦, 10♠, 5♠, 7♠, K♣, 4♠, 3♦, 7♦, 7♥, A♦, 8♥, J♠, 6♠, Q♥, 5♥, 3♠, A♠]
39System.out.println("Deck size: " + deck.size());40System.out.println("First 13: " + deck.subList(0, 13));4142Collections.shuffle(deck→ [10♥, 5♣, 6♣, 2♦, 3♥, 7♣, 3♣, 5♦, 10♦, 2♥, 6♦, 8♣, 10♣, 4♦, 9♣, J♣, 6♥, Q♣, 4♣, A♥, 2♣, Q♠, A♣, K♠, 8♠, 4♥, K♥, J♦, 8♦, 9♦, K♦, 2♠, 9♥, 9♠, J♥, Q♦, 10♠, 5♠, 7♠, K♣, 4♠, 3♦, 7♦, 7♥, A♦, 8♥, J♠, 6♠, Q♥, 5♥, 3♠, A♠], new Random(seed + 1));43System.out.println("After shuffle: " + deck.subList(0, 13));44System.out.println("\nDeal cards:");45Collections.shuffle(deck→ [7♦, K♥, 3♥, 6♠, 4♥, Q♠, A♦, 5♠, 4♦, 9♥, Q♥, 6♦, Q♦, 5♦, 5♣, J♣, 4♣, J♠, 8♦, 7♣, 3♣, K♠, A♠, 4♠, 3♦, A♥, K♣, 6♣, 2♠, J♦, Q♣, 2♣, 10♥, 2♦, 2♥, 7♥, 3♠, 5♥, 8♥, J♥, 9♠, A♣, 8♣, 7♠, K♦, 9♣, 9♦, 10♠, 10♦, 10♣, 6♥, 8♠], new Random(seed + 2));outputDeck size: 52 First 13: [A♠, 2♠, 3♠, 4♠, 5♠, 6♠, 7♠, 8♠, 9♠, 10♠, J♠, Q♠, K♠] After shuffle: [10♥, 5♣, 6♣, 2♦, 3♥, 7♣, 3♣, 5♦, 10♦, 2♥, 6♦, 8♣, 10♣] Deal cards:hand ← [7♦, K♥, 3♥, 6♠, 4♥]
pass 1 of 447for (int player0 = 0; player < 4; player++) {48 List<String> hand→ [7♦, K♥, 3♥, 6♠, 4♥] = deck.subList(player0 * 5, (player + 1) * 5);49 System.out.println("Player " + (player0 + 1) + ": " + hand[7♦, K♥, 3♥, 6♠, 4♥]);50}outputPlayer 1: [7♦, K♥, 3♥, 6♠, 4♥]All 4 passes — pass 1 is the card above pass playerhand1 0 [7♦, K♥, 3♥, 6♠, 4♥] 2 1 [Q♠, A♦, 5♠, 4♦, 9♥] 3 2 [Q♥, 6♦, Q♦, 5♦, 5♣] 4 3 [J♣, 4♣, J♠, 8♦, 7♣] values ← [1, 2, 3, 4, 5], words ← [a, b, c, d], flags ← []
50}51System.out.println("\nFill list:");52List<Integer> values→ [1, 2, 3, 4, 5] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));53System.out.println("Original: " + values[1, 2, 3, 4, 5]);5455Collections.fill(values→ [0, 0, 0, 0, 0], 0);56System.out.println("Filled with 0: " + values[0, 0, 0, 0, 0]);57System.out.println("\nFill strings:");58List<String> words→ [a, b, c, d] = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));59System.out.println("Original: " + words[a, b, c, d]);6061Collections.fill(words→ [X, X, X, X], "X");62System.out.println("Filled: " + words[X, X, X, X]);63System.out.println("\nInitialize with fill:");64List<Boolean> flags→ [] = new ArrayList<>(10);65for (int i = 0; i < 10; i++) flags.add(null);output Fill list: Original: [1, 2, 3, 4, 5] Filled with 0: [0, 0, 0, 0, 0] Fill strings: Original: [a, b, c, d] Filled: [X, X, X, X] Initialize with fill:for (int i = 0; i < 10; i++)
pass 1 of 1064List<Boolean> flags = new ArrayList<>(10);65for (int i0 = 0; i < 10; i++) flags.add(null);All 10 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 flags ← [true, true, true, true, true, true, true, true, true, true]
67Collections.fill(flags→ [true, true, true, true, true, true, true, true, true, true], true);68System.out.println("Flags: " + flags[true, true, true, true, true, true, true, true, true, true]);69System.out.println("\nRandom sample:");70List<Integer> population→ [] = new ArrayList<>();71for (int i = 1; i <= 100; i++) {outputFlags: [true, true, true, true, true, true, true, true, true, true] Random sample:for (int i = 1; i <= 100; i++)
pass 1 of 10070List<Integer> population = new ArrayList<>();71for (int i1 = 1; i <= 100; i++) {72 population.add(i1);73}100 passes — pass 1 is the card above pass i1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 ⋯ 89 more passes ⋯ 99 99 100 100 population ← [72, 34, 29, 20, 56, 38, 46, 8, 50, 95, 74, 33, 32, 82, 6, 51, 65, 93, 11, 16, 35, 94, 13, 79, 81, 45, 41, 4, 87, 62, 17, 48, 47, 26, 96, 73, 90, 85, 66, 57, 60, 75, 42, 3, 63, 77, 7, 76, 84, 53, 18, 43, 69, 97, 37, 59, 12, 92, 54, 22, 89, 83, 19, 78, 9, 27, 64, 58, 71, 80, 100, 14, 44, 91, 5, 21, 15, 61, 98, 23, 68, 55, 99, 86, 67, 88, 49, 31, 36, 1, 70, 39, 25, 2, 52, 40, 30, 24, 28, 10]
75Collections.shuffle(population→ [72, 34, 29, 20, 56, 38, 46, 8, 50, 95, 74, 33, 32, 82, 6, 51, 65, 93, 11, 16, 35, 94, 13, 79, 81, 45, 41, 4, 87, 62, 17, 48, 47, 26, 96, 73, 90, 85, 66, 57, 60, 75, 42, 3, 63, 77, 7, 76, 84, 53, 18, 43, 69, 97, 37, 59, 12, 92, 54, 22, 89, 83, 19, 78, 9, 27, 64, 58, 71, 80, 100, 14, 44, 91, 5, 21, 15, 61, 98, 23, 68, 55, 99, 86, 67, 88, 49, 31, 36, 1, 70, 39, 25, 2, 52, 40, 30, 24, 28, 10], new Random(seed + 3));76List<Integer> sample→ [72, 34, 29, 20, 56, 38, 46, 8, 50, 95] = population.subList(0, 10);77Collections.sort(sample→ [8, 20, 29, 34, 38, 46, 50, 56, 72, 95]);7879System.out.println("Random sample of 10: " + sample[8, 20, 29, 34, 38, 46, 50, 56, 72, 95]);80System.out.println("\nLottery numbers:");81List<Integer> lottery→ [] = new ArrayList<>();82for (int i = 1; i <= 49; i++) {outputRandom sample of 10: [8, 20, 29, 34, 38, 46, 50, 56, 72, 95] Lottery numbers:for (int i = 1; i <= 49; i++)
pass 1 of 4981List<Integer> lottery = new ArrayList<>();82for (int i1 = 1; i <= 49; i++) {83 lottery.add(i1);84}49 passes — pass 1 is the card above pass i1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 ⋯ 38 more passes ⋯ 48 48 49 49 lottery ← [33, 14, 9, 43, 4, 27, 16, 36, 42, 49, 10, 2, 18, 48, 31, 47, 32, 30, 40, 5, 37, 26, 6, 21, 8, 19, 17, 25, 46, 20, 24, 11, 39, 38, 13, 34, 1, 41, 15, 35, 44, 22, 3, 45, 23, 12, 7, 28, 29]
86Collections.shuffle(lottery→ [33, 14, 9, 43, 4, 27, 16, 36, 42, 49, 10, 2, 18, 48, 31, 47, 32, 30, 40, 5, 37, 26, 6, 21, 8, 19, 17, 25, 46, 20, 24, 11, 39, 38, 13, 34, 1, 41, 15, 35, 44, 22, 3, 45, 23, 12, 7, 28, 29], new Random(seed + 4));87List<Integer> picked→ [33, 14, 9, 43, 4, 27] = new ArrayList<>(lottery.subList(0, 6));88Collections.sort(picked→ [4, 9, 14, 27, 33, 43]);8990System.out.println("Lottery numbers: " + picked[4, 9, 14, 27, 33, 43]);91System.out.println("\nShuffle multiple times:");92List<String> items→ [A, B, C, D, E] = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));9394System.out.println("Original: " + items[A, B, C, D, E]);95for (int i = 0; i < 3; i++) {outputLottery numbers: [4, 9, 14, 27, 33, 43] Shuffle multiple times: Original: [A, B, C, D, E]items ← [C, A, E, B, D]
pass 1 of 394System.out.println("Original: " + items);95for (int i0 = 0; i < 3; i++) {96 Collections.shuffle(items→ [C, A, E, B, D], new Random(seed + 5 + i));97 System.out.println("Shuffle " + (i0 + 1) + ": " + items[C, A, E, B, D]);98}outputShuffle 1: [C, A, E, B, D]All 3 passes — pass 1 is the card above pass iitems1 0 [A, B, C, D, E] → [C, A, E, B, D] 2 1 [C, A, E, B, D] → [E, D, A, C, B] 3 2 [E, D, A, C, B] → [C, E, A, D, B]
seed ← 7, nums ← [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], nums1 ← [1, 2, 3, 4, 5]
5public class Shuffle {6 public static void main(String[] args) {7 int seed→ 7 = 7;89 System.out.println("Shuffle list:");10 List<Integer> nums→ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));11 System.out.println("Original: " + nums[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);1213 Collections.shuffle(nums→ [1, 2, 10, 4, 8, 5, 9, 6, 3, 7], new Random(seed));14 System.out.println("Shuffled: " + nums[1, 2, 10, 4, 8, 5, 9, 6, 3, 7]);15 System.out.println("\nShuffle with seed:");16 List<Integer> nums1→ [1, 2, 3, 4, 5] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));17 List<Integer> nums2→ [1, 2, 3, 4, 5] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));1819 Random rand→ ⟨Random A⟩ = new Random(seed);20 Collections.shuffle(nums1→ [5, 4, 1, 3, 2], rand⟨Random A⟩);2122 rand→ ⟨Random B⟩ = new Random(seed);23 Collections.shuffle(nums2→ [5, 4, 1, 3, 2], rand⟨Random B⟩);2425 System.out.println("Shuffle 1: " + nums1[5, 4, 1, 3, 2]);26 System.out.println("Shuffle 2: " + nums2[5, 4, 1, 3, 2]);27 System.out.println("Same? " + nums1.equals(nums2[5, 4, 1, 3, 2]));28 System.out.println("\nShuffle deck:");29 List<String> deck→ [] = new ArrayList<>();30 String[] suits = {"♠", "♥", "♦", "♣"};31 String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};outputShuffle list: Original: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Shuffled: [1, 2, 10, 4, 8, 5, 9, 6, 3, 7] Shuffle with seed: Shuffle 1: [5, 4, 1, 3, 2] Shuffle 2: [5, 4, 1, 3, 2] Same? true Shuffle deck:for (String suit : suits)
pass 1 of 433for (String suit♠ : suits) {34 for (String rank : ranks) {All 4 passes — pass 1 is the card above pass suit1 ♠ 2 ♥ 3 ♦ 4 ♣ for (String rank : ranks)
pass 1 of 5233for (String suit : suits) {34 for (String rankA : ranks) {35 deck.add(rankA + suit♠);36 }52 passes — pass 1 is the card above pass ranksuit1 A ♠ 2 2 ♠ 3 3 ♠ 4 4 ♠ 5 5 ♠ 6 6 ♠ 7 7 ♠ 8 8 ♠ 9 9 ♠ ⋯ 41 more passes ⋯ 51 Q ♣ 52 K ♣ deck ← [Q♦, K♥, 8♠, A♠, 4♣, 10♠, K♠, A♥, 5♥, 4♥, J♣, 8♣, 2♥, 10♣, 2♠, 3♠, A♣, 8♥, 8♦, Q♠, J♠, 5♣, 9♦, K♦, 2♦, 4♠, Q♥, 3♥, 10♦, 6♦, 5♠, Q♣, 6♥, 9♥, 7♣, 7♠, 7♥, 3♣, 6♣, 10♥, 9♠, 9♣, J♥, 6♠, A♦, 4♦, 7♦, J♦, 3♦, 5♦, K♣, 2♣]
39System.out.println("Deck size: " + deck.size());40System.out.println("First 13: " + deck.subList(0, 13));4142Collections.shuffle(deck→ [Q♦, K♥, 8♠, A♠, 4♣, 10♠, K♠, A♥, 5♥, 4♥, J♣, 8♣, 2♥, 10♣, 2♠, 3♠, A♣, 8♥, 8♦, Q♠, J♠, 5♣, 9♦, K♦, 2♦, 4♠, Q♥, 3♥, 10♦, 6♦, 5♠, Q♣, 6♥, 9♥, 7♣, 7♠, 7♥, 3♣, 6♣, 10♥, 9♠, 9♣, J♥, 6♠, A♦, 4♦, 7♦, J♦, 3♦, 5♦, K♣, 2♣], new Random(seed + 1));43System.out.println("After shuffle: " + deck.subList(0, 13));44System.out.println("\nDeal cards:");45Collections.shuffle(deck→ [7♥, Q♣, 3♣, 9♥, 4♦, 3♥, 2♥, 10♦, 5♥, 2♠, 8♦, 4♣, 4♠, 6♥, Q♦, K♥, 7♠, 5♠, A♠, K♣, 6♦, 6♣, J♠, 4♥, 7♣, J♣, A♦, J♥, 2♦, 10♥, 8♠, 8♣, 5♦, A♥, 2♣, 9♠, Q♠, 3♠, A♣, 9♣, J♦, Q♥, 8♥, 10♠, K♠, 7♦, 6♠, K♦, 5♣, 3♦, 9♦, 10♣], new Random(seed + 2));outputDeck size: 52 First 13: [A♠, 2♠, 3♠, 4♠, 5♠, 6♠, 7♠, 8♠, 9♠, 10♠, J♠, Q♠, K♠] After shuffle: [Q♦, K♥, 8♠, A♠, 4♣, 10♠, K♠, A♥, 5♥, 4♥, J♣, 8♣, 2♥] Deal cards:hand ← [7♥, Q♣, 3♣, 9♥, 4♦]
pass 1 of 447for (int player0 = 0; player < 4; player++) {48 List<String> hand→ [7♥, Q♣, 3♣, 9♥, 4♦] = deck.subList(player0 * 5, (player + 1) * 5);49 System.out.println("Player " + (player0 + 1) + ": " + hand[7♥, Q♣, 3♣, 9♥, 4♦]);50}outputPlayer 1: [7♥, Q♣, 3♣, 9♥, 4♦]All 4 passes — pass 1 is the card above pass playerhand1 0 [7♥, Q♣, 3♣, 9♥, 4♦] 2 1 [3♥, 2♥, 10♦, 5♥, 2♠] 3 2 [8♦, 4♣, 4♠, 6♥, Q♦] 4 3 [K♥, 7♠, 5♠, A♠, K♣] values ← [1, 2, 3, 4, 5], words ← [a, b, c, d], flags ← []
50}51System.out.println("\nFill list:");52List<Integer> values→ [1, 2, 3, 4, 5] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));53System.out.println("Original: " + values[1, 2, 3, 4, 5]);5455Collections.fill(values→ [0, 0, 0, 0, 0], 0);56System.out.println("Filled with 0: " + values[0, 0, 0, 0, 0]);57System.out.println("\nFill strings:");58List<String> words→ [a, b, c, d] = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));59System.out.println("Original: " + words[a, b, c, d]);6061Collections.fill(words→ [X, X, X, X], "X");62System.out.println("Filled: " + words[X, X, X, X]);63System.out.println("\nInitialize with fill:");64List<Boolean> flags→ [] = new ArrayList<>(10);65for (int i = 0; i < 10; i++) flags.add(null);output Fill list: Original: [1, 2, 3, 4, 5] Filled with 0: [0, 0, 0, 0, 0] Fill strings: Original: [a, b, c, d] Filled: [X, X, X, X] Initialize with fill:for (int i = 0; i < 10; i++)
pass 1 of 1064List<Boolean> flags = new ArrayList<>(10);65for (int i0 = 0; i < 10; i++) flags.add(null);All 10 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 flags ← [true, true, true, true, true, true, true, true, true, true]
67Collections.fill(flags→ [true, true, true, true, true, true, true, true, true, true], true);68System.out.println("Flags: " + flags[true, true, true, true, true, true, true, true, true, true]);69System.out.println("\nRandom sample:");70List<Integer> population→ [] = new ArrayList<>();71for (int i = 1; i <= 100; i++) {outputFlags: [true, true, true, true, true, true, true, true, true, true] Random sample:for (int i = 1; i <= 100; i++)
pass 1 of 10070List<Integer> population = new ArrayList<>();71for (int i1 = 1; i <= 100; i++) {72 population.add(i1);73}100 passes — pass 1 is the card above pass i1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 ⋯ 89 more passes ⋯ 99 99 100 100 population ← [11, 30, 7, 19, 83, 41, 59, 75, 31, 26, 88, 86, 68, 79, 12, 81, 91, 13, 46, 95, 66, 78, 9, 8, 64, 73, 37, 65, 44, 23, 3, 87, 61, 96, 36, 43, 76, 93, 54, 1, 27, 39, 53, 57, 15, 98, 42, 4, 58, 6, 29, 18, 5, 25, 20, 90, 99, 51, 63, 60, 85, 97, 77, 94, 67, 62, 56, 69, 72, 49, 33, 45, 55, 21, 50, 28, 92, 52, 24, 100, 84, 32, 38, 35, 71, 40, 34, 89, 48, 74, 16, 2, 17, 80, 82, 47, 22, 10, 70, 14]
75Collections.shuffle(population→ [11, 30, 7, 19, 83, 41, 59, 75, 31, 26, 88, 86, 68, 79, 12, 81, 91, 13, 46, 95, 66, 78, 9, 8, 64, 73, 37, 65, 44, 23, 3, 87, 61, 96, 36, 43, 76, 93, 54, 1, 27, 39, 53, 57, 15, 98, 42, 4, 58, 6, 29, 18, 5, 25, 20, 90, 99, 51, 63, 60, 85, 97, 77, 94, 67, 62, 56, 69, 72, 49, 33, 45, 55, 21, 50, 28, 92, 52, 24, 100, 84, 32, 38, 35, 71, 40, 34, 89, 48, 74, 16, 2, 17, 80, 82, 47, 22, 10, 70, 14], new Random(seed + 3));76List<Integer> sample→ [11, 30, 7, 19, 83, 41, 59, 75, 31, 26] = population.subList(0, 10);77Collections.sort(sample→ [7, 11, 19, 26, 30, 31, 41, 59, 75, 83]);7879System.out.println("Random sample of 10: " + sample[7, 11, 19, 26, 30, 31, 41, 59, 75, 83]);80System.out.println("\nLottery numbers:");81List<Integer> lottery→ [] = new ArrayList<>();82for (int i = 1; i <= 49; i++) {outputRandom sample of 10: [7, 11, 19, 26, 30, 31, 41, 59, 75, 83] Lottery numbers:for (int i = 1; i <= 49; i++)
pass 1 of 4981List<Integer> lottery = new ArrayList<>();82for (int i1 = 1; i <= 49; i++) {83 lottery.add(i1);84}49 passes — pass 1 is the card above pass i1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 ⋯ 38 more passes ⋯ 48 48 49 49 lottery ← [36, 17, 12, 39, 40, 29, 25, 14, 48, 26, 43, 16, 20, 21, 15, 23, 47, 2, 11, 10, 19, 13, 22, 31, 32, 35, 27, 42, 4, 38, 33, 49, 9, 37, 3, 6, 1, 7, 41, 28, 30, 18, 24, 8, 46, 34, 5, 45, 44]
86Collections.shuffle(lottery→ [36, 17, 12, 39, 40, 29, 25, 14, 48, 26, 43, 16, 20, 21, 15, 23, 47, 2, 11, 10, 19, 13, 22, 31, 32, 35, 27, 42, 4, 38, 33, 49, 9, 37, 3, 6, 1, 7, 41, 28, 30, 18, 24, 8, 46, 34, 5, 45, 44], new Random(seed + 4));87List<Integer> picked→ [36, 17, 12, 39, 40, 29] = new ArrayList<>(lottery.subList(0, 6));88Collections.sort(picked→ [12, 17, 29, 36, 39, 40]);8990System.out.println("Lottery numbers: " + picked[12, 17, 29, 36, 39, 40]);91System.out.println("\nShuffle multiple times:");92List<String> items→ [A, B, C, D, E] = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));9394System.out.println("Original: " + items[A, B, C, D, E]);95for (int i = 0; i < 3; i++) {outputLottery numbers: [12, 17, 29, 36, 39, 40] Shuffle multiple times: Original: [A, B, C, D, E]items ← [C, D, E, A, B]
pass 1 of 394System.out.println("Original: " + items);95for (int i0 = 0; i < 3; i++) {96 Collections.shuffle(items→ [C, D, E, A, B], new Random(seed + 5 + i));97 System.out.println("Shuffle " + (i0 + 1) + ": " + items[C, D, E, A, B]);98}outputShuffle 1: [C, D, E, A, B]All 3 passes — pass 1 is the card above pass iitems1 0 [A, B, C, D, E] → [C, D, E, A, B] 2 1 [C, D, E, A, B] → [A, C, B, D, E] 3 2 [A, C, B, D, E] → [E, C, D, B, A]
seed ← 12345, nums ← [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], nums1 ← [1, 2, 3, 4, 5]
5public class Shuffle {6 public static void main(String[] args) {7 int seed→ 12345 = 12345;89 System.out.println("Shuffle list:");10 List<Integer> nums→ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));11 System.out.println("Original: " + nums[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);1213 Collections.shuffle(nums→ [4, 3, 1, 6, 9, 10, 7, 8, 5, 2], new Random(seed));14 System.out.println("Shuffled: " + nums[4, 3, 1, 6, 9, 10, 7, 8, 5, 2]);15 System.out.println("\nShuffle with seed:");16 List<Integer> nums1→ [1, 2, 3, 4, 5] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));17 List<Integer> nums2→ [1, 2, 3, 4, 5] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));1819 Random rand→ ⟨Random A⟩ = new Random(seed);20 Collections.shuffle(nums1→ [4, 5, 1, 3, 2], rand⟨Random A⟩);2122 rand→ ⟨Random B⟩ = new Random(seed);23 Collections.shuffle(nums2→ [4, 5, 1, 3, 2], rand⟨Random B⟩);2425 System.out.println("Shuffle 1: " + nums1[4, 5, 1, 3, 2]);26 System.out.println("Shuffle 2: " + nums2[4, 5, 1, 3, 2]);27 System.out.println("Same? " + nums1.equals(nums2[4, 5, 1, 3, 2]));28 System.out.println("\nShuffle deck:");29 List<String> deck→ [] = new ArrayList<>();30 String[] suits = {"♠", "♥", "♦", "♣"};31 String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};outputShuffle list: Original: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Shuffled: [4, 3, 1, 6, 9, 10, 7, 8, 5, 2] Shuffle with seed: Shuffle 1: [4, 5, 1, 3, 2] Shuffle 2: [4, 5, 1, 3, 2] Same? true Shuffle deck:for (String suit : suits)
pass 1 of 433for (String suit♠ : suits) {34 for (String rank : ranks) {All 4 passes — pass 1 is the card above pass suit1 ♠ 2 ♥ 3 ♦ 4 ♣ for (String rank : ranks)
pass 1 of 5233for (String suit : suits) {34 for (String rankA : ranks) {35 deck.add(rankA + suit♠);36 }52 passes — pass 1 is the card above pass ranksuit1 A ♠ 2 2 ♠ 3 3 ♠ 4 4 ♠ 5 5 ♠ 6 6 ♠ 7 7 ♠ 8 8 ♠ 9 9 ♠ ⋯ 41 more passes ⋯ 51 Q ♣ 52 K ♣ deck ← [2♥, 4♦, 3♣, 8♦, 7♦, Q♦, 9♣, J♥, 10♠, K♠, 7♥, J♦, 2♠, 6♠, 9♠, 4♠, 9♥, 10♥, 10♣, Q♥, 7♣, 2♣, K♥, Q♠, 9♦, 3♥, 5♥, 8♠, 6♦, K♣, 3♦, A♣, 6♣, 5♠, Q♣, 6♥, A♦, 5♣, 8♥, J♣, 3♠, 4♣, 4♥, 7♠, 8♣, A♥, 2♦, 5♦, K♦, 10♦, A♠, J♠]
39System.out.println("Deck size: " + deck.size());40System.out.println("First 13: " + deck.subList(0, 13));4142Collections.shuffle(deck→ [2♥, 4♦, 3♣, 8♦, 7♦, Q♦, 9♣, J♥, 10♠, K♠, 7♥, J♦, 2♠, 6♠, 9♠, 4♠, 9♥, 10♥, 10♣, Q♥, 7♣, 2♣, K♥, Q♠, 9♦, 3♥, 5♥, 8♠, 6♦, K♣, 3♦, A♣, 6♣, 5♠, Q♣, 6♥, A♦, 5♣, 8♥, J♣, 3♠, 4♣, 4♥, 7♠, 8♣, A♥, 2♦, 5♦, K♦, 10♦, A♠, J♠], new Random(seed + 1));43System.out.println("After shuffle: " + deck.subList(0, 13));44System.out.println("\nDeal cards:");45Collections.shuffle(deck→ [7♦, 7♠, Q♣, Q♥, 8♠, 4♦, 2♠, 5♠, 2♦, 4♣, 4♥, 6♣, 8♥, K♦, 2♥, A♥, 3♣, K♣, 6♥, 10♦, K♠, 6♠, A♣, 3♥, 5♣, 7♣, 4♠, A♠, 3♠, J♥, 5♥, K♥, J♣, 9♦, 10♣, 8♣, J♦, Q♠, 2♣, 3♦, 9♥, Q♦, 7♥, 9♣, 9♠, 6♦, 5♦, 10♥, J♠, 8♦, 10♠, A♦], new Random(seed + 2));outputDeck size: 52 First 13: [A♠, 2♠, 3♠, 4♠, 5♠, 6♠, 7♠, 8♠, 9♠, 10♠, J♠, Q♠, K♠] After shuffle: [2♥, 4♦, 3♣, 8♦, 7♦, Q♦, 9♣, J♥, 10♠, K♠, 7♥, J♦, 2♠] Deal cards:hand ← [7♦, 7♠, Q♣, Q♥, 8♠]
pass 1 of 447for (int player0 = 0; player < 4; player++) {48 List<String> hand→ [7♦, 7♠, Q♣, Q♥, 8♠] = deck.subList(player0 * 5, (player + 1) * 5);49 System.out.println("Player " + (player0 + 1) + ": " + hand[7♦, 7♠, Q♣, Q♥, 8♠]);50}outputPlayer 1: [7♦, 7♠, Q♣, Q♥, 8♠]All 4 passes — pass 1 is the card above pass playerhand1 0 [7♦, 7♠, Q♣, Q♥, 8♠] 2 1 [4♦, 2♠, 5♠, 2♦, 4♣] 3 2 [4♥, 6♣, 8♥, K♦, 2♥] 4 3 [A♥, 3♣, K♣, 6♥, 10♦] values ← [1, 2, 3, 4, 5], words ← [a, b, c, d], flags ← []
50}51System.out.println("\nFill list:");52List<Integer> values→ [1, 2, 3, 4, 5] = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));53System.out.println("Original: " + values[1, 2, 3, 4, 5]);5455Collections.fill(values→ [0, 0, 0, 0, 0], 0);56System.out.println("Filled with 0: " + values[0, 0, 0, 0, 0]);57System.out.println("\nFill strings:");58List<String> words→ [a, b, c, d] = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));59System.out.println("Original: " + words[a, b, c, d]);6061Collections.fill(words→ [X, X, X, X], "X");62System.out.println("Filled: " + words[X, X, X, X]);63System.out.println("\nInitialize with fill:");64List<Boolean> flags→ [] = new ArrayList<>(10);65for (int i = 0; i < 10; i++) flags.add(null);output Fill list: Original: [1, 2, 3, 4, 5] Filled with 0: [0, 0, 0, 0, 0] Fill strings: Original: [a, b, c, d] Filled: [X, X, X, X] Initialize with fill:for (int i = 0; i < 10; i++)
pass 1 of 1064List<Boolean> flags = new ArrayList<>(10);65for (int i0 = 0; i < 10; i++) flags.add(null);All 10 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 flags ← [true, true, true, true, true, true, true, true, true, true]
67Collections.fill(flags→ [true, true, true, true, true, true, true, true, true, true], true);68System.out.println("Flags: " + flags[true, true, true, true, true, true, true, true, true, true]);69System.out.println("\nRandom sample:");70List<Integer> population→ [] = new ArrayList<>();71for (int i = 1; i <= 100; i++) {outputFlags: [true, true, true, true, true, true, true, true, true, true] Random sample:for (int i = 1; i <= 100; i++)
pass 1 of 10070List<Integer> population = new ArrayList<>();71for (int i1 = 1; i <= 100; i++) {72 population.add(i1);73}100 passes — pass 1 is the card above pass i1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 ⋯ 89 more passes ⋯ 99 99 100 100 population ← [67, 6, 22, 93, 7, 80, 23, 92, 95, 9, 34, 89, 2, 36, 48, 24, 60, 66, 43, 42, 59, 75, 14, 5, 51, 28, 26, 58, 90, 39, 19, 97, 10, 41, 81, 56, 18, 87, 15, 21, 70, 1, 69, 85, 71, 44, 33, 40, 82, 52, 37, 79, 72, 86, 50, 55, 68, 11, 83, 30, 16, 84, 3, 64, 54, 57, 38, 4, 77, 8, 32, 12, 47, 13, 91, 94, 98, 78, 61, 53, 17, 100, 25, 96, 76, 45, 88, 74, 73, 31, 27, 46, 20, 63, 62, 49, 35, 65, 99, 29]
75Collections.shuffle(population→ [67, 6, 22, 93, 7, 80, 23, 92, 95, 9, 34, 89, 2, 36, 48, 24, 60, 66, 43, 42, 59, 75, 14, 5, 51, 28, 26, 58, 90, 39, 19, 97, 10, 41, 81, 56, 18, 87, 15, 21, 70, 1, 69, 85, 71, 44, 33, 40, 82, 52, 37, 79, 72, 86, 50, 55, 68, 11, 83, 30, 16, 84, 3, 64, 54, 57, 38, 4, 77, 8, 32, 12, 47, 13, 91, 94, 98, 78, 61, 53, 17, 100, 25, 96, 76, 45, 88, 74, 73, 31, 27, 46, 20, 63, 62, 49, 35, 65, 99, 29], new Random(seed + 3));76List<Integer> sample→ [67, 6, 22, 93, 7, 80, 23, 92, 95, 9] = population.subList(0, 10);77Collections.sort(sample→ [6, 7, 9, 22, 23, 67, 80, 92, 93, 95]);7879System.out.println("Random sample of 10: " + sample[6, 7, 9, 22, 23, 67, 80, 92, 93, 95]);80System.out.println("\nLottery numbers:");81List<Integer> lottery→ [] = new ArrayList<>();82for (int i = 1; i <= 49; i++) {outputRandom sample of 10: [6, 7, 9, 22, 23, 67, 80, 92, 93, 95] Lottery numbers:for (int i = 1; i <= 49; i++)
pass 1 of 4981List<Integer> lottery = new ArrayList<>();82for (int i1 = 1; i <= 49; i++) {83 lottery.add(i1);84}49 passes — pass 1 is the card above pass i1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 ⋯ 38 more passes ⋯ 48 48 49 49 lottery ← [40, 44, 35, 36, 39, 26, 49, 15, 22, 28, 6, 8, 21, 33, 7, 43, 5, 10, 47, 17, 23, 1, 9, 18, 19, 30, 12, 45, 29, 14, 24, 16, 32, 48, 46, 37, 13, 3, 42, 11, 31, 2, 34, 38, 25, 20, 27, 41, 4]
86Collections.shuffle(lottery→ [40, 44, 35, 36, 39, 26, 49, 15, 22, 28, 6, 8, 21, 33, 7, 43, 5, 10, 47, 17, 23, 1, 9, 18, 19, 30, 12, 45, 29, 14, 24, 16, 32, 48, 46, 37, 13, 3, 42, 11, 31, 2, 34, 38, 25, 20, 27, 41, 4], new Random(seed + 4));87List<Integer> picked→ [40, 44, 35, 36, 39, 26] = new ArrayList<>(lottery.subList(0, 6));88Collections.sort(picked→ [26, 35, 36, 39, 40, 44]);8990System.out.println("Lottery numbers: " + picked[26, 35, 36, 39, 40, 44]);91System.out.println("\nShuffle multiple times:");92List<String> items→ [A, B, C, D, E] = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));9394System.out.println("Original: " + items[A, B, C, D, E]);95for (int i = 0; i < 3; i++) {outputLottery numbers: [26, 35, 36, 39, 40, 44] Shuffle multiple times: Original: [A, B, C, D, E]items ← [A, C, E, D, B]
pass 1 of 394System.out.println("Original: " + items);95for (int i0 = 0; i < 3; i++) {96 Collections.shuffle(items→ [A, C, E, D, B], new Random(seed + 5 + i));97 System.out.println("Shuffle " + (i0 + 1) + ": " + items[A, C, E, D, B]);98}outputShuffle 1: [A, C, E, D, B]All 3 passes — pass 1 is the card above pass iitems1 0 [A, B, C, D, E] → [A, C, E, D, B] 2 1 [A, C, E, D, B] → [D, C, B, A, E] 3 2 [D, C, B, A, E] → [E, A, C, D, B]
Frequency and Statistics
Count occurrences and find min/max values.
Frequency.java
Replay: real traced execution (multi-file project)
// Collections.frequency and other counting methods
import java.util.*;
public class Frequency {
public static void main(String[] args) {
System.out.println("Frequency:");
List<String> words = Arrays.asList("apple", "banana", "apple", "cherry", "banana", "apple");
System.out.println("Words: " + words);
System.out.println("'apple': " + Collections.frequency(words, "apple"));
System.out.println("'banana': " + Collections.frequency(words, "banana"));
System.out.println("'cherry': " + Collections.frequency(words, "cherry"));
System.out.println("'grape': " + Collections.frequency(words, "grape"));
System.out.println("\nCount duplicates:");
List<Integer> nums = Arrays.asList(1, 2, 2, 3, 3, 3, 4, 4, 4, 4);
Set<Integer> unique = new HashSet<>(nums);
System.out.println("Numbers: " + nums);
for (int n : unique) {
int count = Collections.frequency(nums, n);
System.out.println(n + " appears " + count + " times");
}
System.out.println("\nDisjoint check:");
List<Integer> list1 = Arrays.asList(1, 2, 3, 4);
List<Integer> list2 = Arrays.asList(5, 6, 7, 8);
List<Integer> list3 = Arrays.asList(3, 4, 5, 6);
System.out.println("List 1: " + list1);
System.out.println("List 2: " + list2);
System.out.println("List 3: " + list3);
System.out.println("1 and 2 disjoint? " + Collections.disjoint(list1, list2));
System.out.println("1 and 3 disjoint? " + Collections.disjoint(list1, list3));
System.out.println("\nBinary search:");
List<Integer> sorted = new ArrayList<>(Arrays.asList(1, 3, 5, 7, 9, 11, 13));
System.out.println("List: " + sorted);
System.out.println("Search 7: " + Collections.binarySearch(sorted, 7));
System.out.println("Search 8: " + Collections.binarySearch(sorted, 8));
System.out.println("\nIndex of sublist:");
List<Integer> main = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
List<Integer> sub1 = Arrays.asList(3, 4, 5);
List<Integer> sub2 = Arrays.asList(5, 6, 7);
List<Integer> sub3 = Arrays.asList(9, 10);
System.out.println("Main: " + main);
System.out.println("Sub [3,4,5]: " + Collections.indexOfSubList(main, sub1));
System.out.println("Sub [5,6,7]: " + Collections.indexOfSubList(main, sub2));
System.out.println("Sub [9,10]: " + Collections.indexOfSubList(main, sub3));
System.out.println("\nLast index of sublist:");
List<Integer> repeated = Arrays.asList(1, 2, 3, 1, 2, 3, 1, 2, 3);
List<Integer> pattern = Arrays.asList(1, 2, 3);
System.out.println("List: " + repeated);
System.out.println("Pattern: " + pattern);
System.out.println("First occurrence: " + Collections.indexOfSubList(repeated, pattern));
System.out.println("Last occurrence: " + Collections.lastIndexOfSubList(repeated, pattern));
System.out.println("\nReplace all:");
List<String> items = new ArrayList<>(Arrays.asList("a", "b", "c", "b", "d", "b"));
System.out.println("Original: " + items);
boolean changed = Collections.replaceAll(items, "b", "X");
System.out.println("Replaced 'b' -> 'X': " + items);
System.out.println("Changed? " + changed);
System.out.println("\nMin and max:");
List<Integer> values = Arrays.asList(42, 17, 93, 8, 56, 31);
System.out.println("Values: " + values);
System.out.println("Min: " + Collections.min(values));
System.out.println("Max: " + Collections.max(values));
System.out.println("\nMin/max with comparator:");
List<String> names = Arrays.asList("Alice", "Bob", "Christopher", "Di");
System.out.println("Names: " + names);
System.out.println("Shortest: " + Collections.min(names, Comparator.comparingInt(String::length)));
System.out.println("Longest: " + Collections.max(names, Comparator.comparingInt(String::length)));
System.out.println("\nMost frequent element:");
List<String> data = Arrays.asList("a", "b", "a", "c", "a", "b", "d", "a");
System.out.println("Data: " + data);
Set<String> uniqueData = new HashSet<>(data);
String mostFrequent = null;
int maxFreq = 0;
for (String item : uniqueData) {
int freq = Collections.frequency(data, item);
if (freq > maxFreq) {
maxFreq = freq;
mostFrequent = item;
}
}
System.out.println("Most frequent: '" + mostFrequent + "' (" + maxFreq + " times)");
}
}
words ← [apple, banana, apple, cherry, banana, apple], nums ← [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
5public class Frequency {6 public static void main(String[] args) {7 System.out.println("Frequency:");8 List<String> words→ [apple, banana, apple, cherry, banana, apple] = Arrays.asList("apple", "banana", "apple", "cherry", "banana", "apple");910 System.out.println("Words: " + words[apple, banana, apple, cherry, banana, apple]);11 System.out.println("'apple': " + Collections.frequency(words[apple, banana, apple, cherry, banana, apple], "apple"));12 System.out.println("'banana': " + Collections.frequency(words[apple, banana, apple, cherry, banana, apple], "banana"));13 System.out.println("'cherry': " + Collections.frequency(words[apple, banana, apple, cherry, banana, apple], "cherry"));14 System.out.println("'grape': " + Collections.frequency(words[apple, banana, apple, cherry, banana, apple], "grape"));15 System.out.println("\nCount duplicates:");16 List<Integer> nums→ [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] = Arrays.asList(1, 2, 2, 3, 3, 3, 4, 4, 4, 4);1718 Set<Integer> unique→ [1, 2, 3, 4] = new HashSet<>(nums);19 System.out.println("Numbers: " + nums[1, 2, 2, 3, 3, 3, 4, 4, 4, 4]);20 for (int n : unique) {outputFrequency: Words: [apple, banana, apple, cherry, banana, apple] 'apple': 3 'banana': 2 'cherry': 1 'grape': 0 Count duplicates: Numbers: [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]count ← 1
pass 1 of 419System.out.println("Numbers: " + nums);20for (int n1 : unique[1, 2, 3, 4]) {21 int count→ 1 = Collections.frequency(nums[1, 2, 2, 3, 3, 3, 4, 4, 4, 4], n1);22 System.out.println(n1 + " appears " + count1 + " times");23}output1 appears 1 timesAll 4 passes — pass 1 is the card above pass ncount1 1 1 2 2 2 3 3 3 4 4 4 list1 ← [1, 2, 3, 4], list2 ← [5, 6, 7, 8], list3 ← [3, 4, 5, 6]
23}24System.out.println("\nDisjoint check:");25List<Integer> list1→ [1, 2, 3, 4] = Arrays.asList(1, 2, 3, 4);26List<Integer> list2→ [5, 6, 7, 8] = Arrays.asList(5, 6, 7, 8);27List<Integer> list3→ [3, 4, 5, 6] = Arrays.asList(3, 4, 5, 6);2829System.out.println("List 1: " + list1[1, 2, 3, 4]);30System.out.println("List 2: " + list2[5, 6, 7, 8]);31System.out.println("List 3: " + list3[3, 4, 5, 6]);3233System.out.println("1 and 2 disjoint? " + Collections.disjoint(list1[1, 2, 3, 4], list2[5, 6, 7, 8]));34System.out.println("1 and 3 disjoint? " + Collections.disjoint(list1[1, 2, 3, 4], list3[3, 4, 5, 6]));35System.out.println("\nBinary search:");36List<Integer> sorted→ [1, 3, 5, 7, 9, 11, 13] = new ArrayList<>(Arrays.asList(1, 3, 5, 7, 9, 11, 13));3738System.out.println("List: " + sorted[1, 3, 5, 7, 9, 11, 13]);39System.out.println("Search 7: " + Collections.binarySearch(sorted[1, 3, 5, 7, 9, 11, 13], 7));40System.out.println("Search 8: " + Collections.binarySearch(sorted[1, 3, 5, 7, 9, 11, 13], 8));41System.out.println("\nIndex of sublist:");42List<Integer> main→ [1, 2, 3, 4, 5, 6, 7, 8] = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);43List<Integer> sub1→ [3, 4, 5] = Arrays.asList(3, 4, 5);44List<Integer> sub2→ [5, 6, 7] = Arrays.asList(5, 6, 7);45List<Integer> sub3→ [9, 10] = Arrays.asList(9, 10);4647System.out.println("Main: " + main[1, 2, 3, 4, 5, 6, 7, 8]);48System.out.println("Sub [3,4,5]: " + Collections.indexOfSubList(main[1, 2, 3, 4, 5, 6, 7, 8], sub1[3, 4, 5]));49System.out.println("Sub [5,6,7]: " + Collections.indexOfSubList(main[1, 2, 3, 4, 5, 6, 7, 8], sub2[5, 6, 7]));50System.out.println("Sub [9,10]: " + Collections.indexOfSubList(main[1, 2, 3, 4, 5, 6, 7, 8], sub3[9, 10]));51System.out.println("\nLast index of sublist:");52List<Integer> repeated→ [1, 2, 3, 1, 2, 3, 1, 2, 3] = Arrays.asList(1, 2, 3, 1, 2, 3, 1, 2, 3);53List<Integer> pattern→ [1, 2, 3] = Arrays.asList(1, 2, 3);5455System.out.println("List: " + repeated[1, 2, 3, 1, 2, 3, 1, 2, 3]);56System.out.println("Pattern: " + pattern[1, 2, 3]);57System.out.println("First occurrence: " + Collections.indexOfSubList(repeated[1, 2, 3, 1, 2, 3, 1, 2, 3], pattern[1, 2, 3]));58System.out.println("Last occurrence: " + Collections.lastIndexOfSubList(repeated[1, 2, 3, 1, 2, 3, 1, 2, 3], pattern[1, 2, 3]));59System.out.println("\nReplace all:");60List<String> items→ [a, b, c, b, d, b] = new ArrayList<>(Arrays.asList("a", "b", "c", "b", "d", "b"));61System.out.println("Original: " + items[a, b, c, b, d, b]);6263boolean changed→ true = Collections.replaceAll(items→ [a, X, c, X, d, X], "b", "X");64System.out.println("Replaced 'b' -> 'X': " + items[a, X, c, X, d, X]);65System.out.println("Changed? " + changedtrue);66System.out.println("\nMin and max:");67List<Integer> values→ [42, 17, 93, 8, 56, 31] = Arrays.asList(42, 17, 93, 8, 56, 31);6869System.out.println("Values: " + values[42, 17, 93, 8, 56, 31]);70System.out.println("Min: " + Collections.min(values[42, 17, 93, 8, 56, 31]));71System.out.println("Max: " + Collections.max(values[42, 17, 93, 8, 56, 31]));72System.out.println("\nMin/max with comparator:");73List<String> names→ [Alice, Bob, Christopher, Di] = Arrays.asList("Alice", "Bob", "Christopher", "Di");7475System.out.println("Names: " + names[Alice, Bob, Christopher, Di]);76System.out.println("Shortest: " + Collections.min(names[Alice, Bob, Christopher, Di], Comparator.comparingInt(String::length)));77System.out.println("Longest: " + Collections.max(names[Alice, Bob, Christopher, Di], Comparator.comparingInt(String::length)));78System.out.println("\nMost frequent element:");79List<String> data→ [a, b, a, c, a, b, d, a] = Arrays.asList("a", "b", "a", "c", "a", "b", "d", "a");8081System.out.println("Data: " + data[a, b, a, c, a, b, d, a]);8283Set<String> uniqueData→ [a, b, c, d] = new HashSet<>(data);84String mostFrequent→ null = null;85int maxFreq→ 0 = 0;output Disjoint check: List 1: [1, 2, 3, 4] List 2: [5, 6, 7, 8] List 3: [3, 4, 5, 6] 1 and 2 disjoint? true 1 and 3 disjoint? false Binary search: List: [1, 3, 5, 7, 9, 11, 13] Search 7: 3 Search 8: -5 Index of sublist: Main: [1, 2, 3, 4, 5, 6, 7, 8] Sub [3,4,5]: 2 Sub [5,6,7]: 4 Sub [9,10]: -1 Last index of sublist: List: [1, 2, 3, 1, 2, 3, 1, 2, 3] Pattern: [1, 2, 3] First occurrence: 0 Last occurrence: 6 Replace all: Original: [a, b, c, b, d, b] Replaced 'b' -> 'X': [a, X, c, X, d, X] Changed? true Min and max: Values: [42, 17, 93, 8, 56, 31] Min: 8 Max: 93 Min/max with comparator: Names: [Alice, Bob, Christopher, Di] Shortest: Di Longest: Christopher Most frequent element: Data: [a, b, a, c, a, b, d, a]freq ← 4
pass 1 of 487for (String itema : uniqueData[a, b, c, d]) {88 int freq→ 4 = Collections.frequency(data[a, b, a, c, a, b, d, a], itema);89 if (freq > maxFreq) {All 4 passes — pass 1 is the card above pass itemfreqmaxFreqmostFrequent1 a 4 0 → 4 a 2 b 2 — — 3 c 1 — — 4 d 1 — — maxFreq ← 4, mostFrequent ← a
88int freq = Collections.frequency(data, item);89if (freq4 > maxFreq0) {90 maxFreq→ 4 = freq4;91 mostFrequent→ a = itema;92}System.out.println("Most frequent: '" + mostFrequent + "' (" + maxFreq…
95 System.out.println("Most frequent: '" + mostFrequenta + "' (" + maxFreq4 + " times)");96}outputMost frequent: 'a' (4 times)
Frequency
Collections.frequency() counts how many times an element appears, useful for finding modes or duplicates.
Unmodifiable Collections
Create read-only views to protect data from modification.
Unmodifiable.java
Replay: real traced execution (multi-file project)
// Unmodifiable collections
import java.util.*;
public class Unmodifiable {
public static void main(String[] args) {
System.out.println("Unmodifiable list:");
List<String> mutable = new ArrayList<>(Arrays.asList("a", "b", "c"));
List<String> immutable = Collections.unmodifiableList(mutable);
System.out.println("Mutable: " + mutable);
System.out.println("Immutable: " + immutable);
// Modify mutable (reflects in immutable)
mutable.add("d");
System.out.println("After adding to mutable:");
System.out.println("Mutable: " + mutable);
System.out.println("Immutable: " + immutable);
// Try to modify immutable
try {
immutable.add("e");
} catch (UnsupportedOperationException e) {
System.out.println("Cannot modify immutable: " + e.getClass().getSimpleName());
}
System.out.println("\nUnmodifiable set:");
Set<Integer> mutableSet = new HashSet<>(Arrays.asList(1, 2, 3));
Set<Integer> immutableSet = Collections.unmodifiableSet(mutableSet);
System.out.println("Set: " + immutableSet);
try {
immutableSet.add(4);
} catch (UnsupportedOperationException e) {
System.out.println("Cannot add to unmodifiable set");
}
System.out.println("\nUnmodifiable map:");
Map<String, Integer> mutableMap = new HashMap<>();
mutableMap.put("Alice", 85);
mutableMap.put("Bob", 92);
Map<String, Integer> immutableMap = Collections.unmodifiableMap(mutableMap);
System.out.println("Map: " + immutableMap);
try {
immutableMap.put("Charlie", 78);
} catch (UnsupportedOperationException e) {
System.out.println("Cannot modify unmodifiable map");
}
System.out.println("\nDefensive copy:");
List<String> original = new ArrayList<>(Arrays.asList("x", "y", "z"));
List<String> defensiveCopy = Collections.unmodifiableList(new ArrayList<>(original));
System.out.println("Original: " + original);
System.out.println("Defensive: " + defensiveCopy);
// Modify original (doesn't affect defensive copy)
original.add("w");
System.out.println("After modifying original:");
System.out.println("Original: " + original);
System.out.println("Defensive: " + defensiveCopy);
System.out.println("\nEmpty collections:");
List<String> emptyList = Collections.emptyList();
Set<Integer> emptySet = Collections.emptySet();
Map<String, Integer> emptyMap = Collections.emptyMap();
System.out.println("Empty list: " + emptyList);
System.out.println("Empty set: " + emptySet);
System.out.println("Empty map: " + emptyMap);
try {
emptyList.add("item");
} catch (UnsupportedOperationException e) {
System.out.println("Empty collections are immutable");
}
System.out.println("\nSingleton collections:");
List<String> singletonList = Collections.singletonList("only");
Set<Integer> singletonSet = Collections.singleton(42);
Map<String, Integer> singletonMap = Collections.singletonMap("key", 100);
System.out.println("Singleton list: " + singletonList);
System.out.println("Singleton set: " + singletonSet);
System.out.println("Singleton map: " + singletonMap);
try {
singletonList.add("another");
} catch (UnsupportedOperationException e) {
System.out.println("Singleton collections are immutable");
}
System.out.println("\nChecked collections:");
List<String> checkedList = Collections.checkedList(
new ArrayList<>(), String.class
);
checkedList.add("valid");
System.out.println("Checked list: " + checkedList);
// Type checking at runtime
@SuppressWarnings("unchecked")
List raw = checkedList;
try {
raw.add(123); // Wrong type
} catch (ClassCastException e) {
System.out.println("Type checking prevented adding Integer to String list");
}
System.out.println("\nSynchronized collections:");
List<Integer> syncList = Collections.synchronizedList(new ArrayList<>());
syncList.add(1);
syncList.add(2);
syncList.add(3);
// Must synchronize on iteration
synchronized (syncList) {
for (int n : syncList) {
System.out.print(n + " ");
}
}
System.out.println("\nSynchronized collections are thread-safe");
}
}
mutable ← [a, b, c], immutable ← [a, b, c]
5public class Unmodifiable {6 public static void main(String[] args) {7 System.out.println("Unmodifiable list:");8 List<String> mutable→ [a, b, c] = new ArrayList<>(Arrays.asList("a", "b", "c"));9 List<String> immutable→ [a, b, c] = Collections.unmodifiableList(mutable[a, b, c]);1011 System.out.println("Mutable: " + mutable[a, b, c]);12 System.out.println("Immutable: " + immutable[a, b, c]);1314 // Modify mutable (reflects in immutable)15 mutable.add("d");16 System.out.println("After adding to mutable:");17 System.out.println("Mutable: " + mutable[a, b, c, d]);18 System.out.println("Immutable: " + immutable[a, b, c, d]);outputUnmodifiable list: Mutable: [a, b, c] Immutable: [a, b, c] After adding to mutable: Mutable: [a, b, c, d] Immutable: [a, b, c, d]catch (UnsupportedOperationException e)
22 immutable.add("e");23} catch (UnsupportedOperationException ejava.lang.UnsupportedOperationException) {24 System.out.println("Cannot modify immutable: " + e.getClass().getSimpleName());25}outputCannot modify immutable: UnsupportedOperationExceptionmutableSet ← [1, 2, 3], immutableSet ← [1, 2, 3]
25}26System.out.println("\nUnmodifiable set:");27Set<Integer> mutableSet→ [1, 2, 3] = new HashSet<>(Arrays.asList(1, 2, 3));28Set<Integer> immutableSet→ [1, 2, 3] = Collections.unmodifiableSet(mutableSet[1, 2, 3]);2930System.out.println("Set: " + immutableSet[1, 2, 3]);output Unmodifiable set: Set: [1, 2, 3]catch (UnsupportedOperationException e)
33 immutableSet.add(4);34} catch (UnsupportedOperationException ejava.lang.UnsupportedOperationException) {35 System.out.println("Cannot add to unmodifiable set");36}outputCannot add to unmodifiable setmutableMap ← {}, immutableMap ← {Bob=92, Alice=85}
36}37System.out.println("\nUnmodifiable map:");38Map<String, Integer> mutableMap→ {} = new HashMap<>();39mutableMap.put("Alice", 85);40mutableMap.put("Bob", 92);4142Map<String, Integer> immutableMap→ {Bob=92, Alice=85} = Collections.unmodifiableMap(mutableMap{Bob=92, Alice=85});4344System.out.println("Map: " + immutableMap{Bob=92, Alice=85});output Unmodifiable map: Map: {Bob=92, Alice=85}catch (UnsupportedOperationException e)
47 immutableMap.put("Charlie", 78);48} catch (UnsupportedOperationException ejava.lang.UnsupportedOperationException) {49 System.out.println("Cannot modify unmodifiable map");50}outputCannot modify unmodifiable maporiginal ← [x, y, z], defensiveCopy ← [x, y, z], emptyList ← []
50}51System.out.println("\nDefensive copy:");52List<String> original→ [x, y, z] = new ArrayList<>(Arrays.asList("x", "y", "z"));53List<String> defensiveCopy→ [x, y, z] = Collections.unmodifiableList(new ArrayList<>(original));5455System.out.println("Original: " + original[x, y, z]);56System.out.println("Defensive: " + defensiveCopy[x, y, z]);5758// Modify original (doesn't affect defensive copy)59original.add("w");60System.out.println("After modifying original:");61System.out.println("Original: " + original[x, y, z, w]);62System.out.println("Defensive: " + defensiveCopy[x, y, z]);63System.out.println("\nEmpty collections:");64List<String> emptyList→ [] = Collections.emptyList();65Set<Integer> emptySet→ [] = Collections.emptySet();66Map<String, Integer> emptyMap→ {} = Collections.emptyMap();6768System.out.println("Empty list: " + emptyList[]);69System.out.println("Empty set: " + emptySet[]);70System.out.println("Empty map: " + emptyMap{});output Defensive copy: Original: [x, y, z] Defensive: [x, y, z] After modifying original: Original: [x, y, z, w] Defensive: [x, y, z] Empty collections: Empty list: [] Empty set: [] Empty map: {}catch (UnsupportedOperationException e)
73 emptyList.add("item");74} catch (UnsupportedOperationException ejava.lang.UnsupportedOperationException) {75 System.out.println("Empty collections are immutable");76}outputEmpty collections are immutablesingletonList ← [only], singletonSet ← [42], singletonMap ← {key=100}
76}77System.out.println("\nSingleton collections:");78List<String> singletonList→ [only] = Collections.singletonList("only");79Set<Integer> singletonSet→ [42] = Collections.singleton(42);80Map<String, Integer> singletonMap→ {key=100} = Collections.singletonMap("key", 100);8182System.out.println("Singleton list: " + singletonList[only]);83System.out.println("Singleton set: " + singletonSet[42]);84System.out.println("Singleton map: " + singletonMap{key=100});output Singleton collections: Singleton list: [only] Singleton set: [42] Singleton map: {key=100}catch (UnsupportedOperationException e)
87 singletonList.add("another");88} catch (UnsupportedOperationException ejava.lang.UnsupportedOperationException) {89 System.out.println("Singleton collections are immutable");90}outputSingleton collections are immutablecheckedList ← [], raw ← [valid]
90}91System.out.println("\nChecked collections:");92List<String> checkedList→ [] = Collections.checkedList(93 new ArrayList<>(), String.class94);9596checkedList.add("valid");97System.out.println("Checked list: " + checkedList[valid]);9899// Type checking at runtime100@SuppressWarnings("unchecked")101List raw→ [valid] = checkedList;102try {output Checked collections: Checked list: [valid]catch (ClassCastException e)
103 raw.add(123); // Wrong type104} catch (ClassCastException ejava.lang.ClassCastException: Attempt to insert class java.lang.Integer element into collection with element type class java.lang.String) {105 System.out.println("Type checking prevented adding Integer to String list");106}outputType checking prevented adding Integer to String listsyncList ← []
106}107System.out.println("\nSynchronized collections:");108List<Integer> syncList→ [] = Collections.synchronizedList(new ArrayList<>());109110syncList.add(1);111syncList.add(2);112syncList.add(3);output Synchronized collections:for (int n : syncList)
pass 1 of 3115synchronized (syncList) {116 for (int n1 : syncList[1, 2, 3]) {117 System.out.print(n1 + " ");118 }output1All 3 passes — pass 1 is the card above pass n1 1 2 2 3 3 System.out.println(" Synchronized collections are thread-safe");
119 }120 System.out.println("\nSynchronized collections are thread-safe");121}output Synchronized collections are thread-safe
@seealso arrays_util
Unmodifiable view
A wrapper that throws UnsupportedOperationException on modification attempts, protecting the underlying collection.
Exercise: Practical.java
Find the most common word in a list and create a read-only leaderboard