Collections Class methods and its usages

import java.util.*;

public class Main {
public static void main(String[] args) {
// sort(List list)
List numbers = new ArrayList<>(Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6, 5));
Collections.sort(numbers);
System.out.println(“Sorted list: ” + numbers);

    // reverse(List<T> list)
    List<String> names = new ArrayList<>(Arrays.asList("Alice", "Bob", "Charlie", "David"));
    Collections.reverse(names);
    System.out.println("Reversed list: " + names);

    // shuffle(List<?> list)
    List<Integer> shuffledNumbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
    Collections.shuffle(shuffledNumbers);
    System.out.println("Shuffled list: " + shuffledNumbers);

    // binarySearch(List<? extends Comparable<? super T>> list, T key)
    int index = Collections.binarySearch(numbers, 5);
    System.out.println("Index of element 5: " + index);

    // max(Collection<? extends T> coll) / min(Collection<? extends T> coll)
    Integer maxNumber = Collections.max(numbers);
    Integer minNumber = Collections.min(numbers);
    System.out.println("Max number: " + maxNumber);
    System.out.println("Min number: " + minNumber);

    // copy(List<? super T> dest, List<? extends T> src)
    List<Integer> copyOfNumbers = new ArrayList<>(numbers.size());
    Collections.copy(copyOfNumbers, numbers);
    System.out.println("Copied list: " + copyOfNumbers);

    // fill(List<? super T> list, T obj)
    List<Integer> filledList = new ArrayList<>(Collections.nCopies(5, 0)); // Initialize list with 5 zeroes
    Collections.fill(filledList, 9); // Fill the list with 9s
    System.out.println("Filled list: " + filledList);

    // replaceAll(List<T> list, T oldVal, T newVal)
    List<String> letters = new ArrayList<>(Arrays.asList("a", "b", "c", "b", "d", "b", "e"));
    Collections.replaceAll(letters, "b", "x");
    System.out.println("List after replacing 'b' with 'x': " + letters);

    // frequency(Collection<?> c, Object o)
    int frequency = Collections.frequency(letters, "x");
    System.out.println("Frequency of 'x': " + frequency);

    // unmodifiableCollection(Collection<? extends T> c) / unmodifiableList(List<? extends T> list) / unmodifiableSet(Set<? extends T> s)
    List<String> unmodifiableList = Collections.unmodifiableList(letters);
    // Trying to modify the unmodifiable list will result in UnsupportedOperationException
    // unmodifiableList.add("z"); // This will throw UnsupportedOperationException

    // singleton(T o)
    Set<String> singletonSet = Collections.singleton("singleton");
    System.out.println("Singleton set: " + singletonSet);

    // singletonList(T o) / singletonMap(K key, V value)
    List<String> singletonList = Collections.singletonList("singleton");
    System.out.println("Singleton list: " + singletonList);
    Map<String, Integer> singletonMap = Collections.singletonMap("key", 123);
    System.out.println("Singleton map: " + singletonMap);
}

}

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑