Java Collections Class

Introduction

The Collections class in Java, part of the java.util package, provides utility methods for operating on collections, such as lists, sets, and maps.

Table of Contents

  1. What is the Collections Class?
  2. Common Methods
  3. Examples of Using the Collections Class
  4. Conclusion

1. What is the Collections Class?

The Collections class offers static methods to perform operations on collections, including sorting, searching, reversing, and more.

2. Common Methods

  • sort(List<T> list): Sorts the specified list into ascending order.
  • reverse(List<?> list): Reverses the order of the elements in the specified list.
  • shuffle(List<?> list): Randomly permutes the elements in the specified list.
  • binarySearch(List<? extends Comparable<? super T>> list, T key): Searches for the specified key in the sorted list using the binary search algorithm.
  • max(Collection<? extends T> coll): Returns the maximum element of the given collection.
  • min(Collection<? extends T> coll): Returns the minimum element of the given collection.
  • fill(List<? super T> list, T obj): Replaces all elements of the specified list with the specified element.
  • copy(List<? super T> dest, List<? extends T> src): Copies elements from the source list into the destination list.
  • disjoint(Collection<?> c1, Collection<?> c2): Returns true if the two specified collections have no elements in common.
  • frequency(Collection<?> c, Object o): Returns the number of occurrences of the specified element in the collection.

3. Examples of Using the Collections Class

Example 1: Sorting a List

This example demonstrates how to sort a list using the sort method.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SortExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(5, 3, 8, 1, 2);
        Collections.sort(numbers);
        System.out.println("Sorted list: " + numbers);
    }
}

Output:

Sorted list: [1, 2, 3, 5, 8]

Example 2: Reversing a List

This example shows how to reverse the order of elements in a list.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ReverseExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        Collections.reverse(numbers);
        System.out.println("Reversed list: " + numbers);
    }
}

Output:

Reversed list: [5, 4, 3, 2, 1]

Example 3: Shuffling a List

This example demonstrates how to shuffle the elements of a list.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ShuffleExample {
    public static void main(String[] args) {
        List<String> items = Arrays.asList("Apple", "Banana", "Cherry");
        Collections.shuffle(items);
        System.out.println("Shuffled list: " + items);
    }
}

Output:

Shuffled list: [Apple, Cherry, Banana]

Example 4: Binary Search

This example shows how to use binarySearch to find an element in a sorted list.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class BinarySearchExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        int index = Collections.binarySearch(numbers, 3);
        System.out.println("Index of 3: " + index);
    }
}

Output:

Index of 3: 2

Example 5: Finding Maximum and Minimum

This example demonstrates how to find the maximum and minimum elements in a collection.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MaxMinExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(5, 3, 8, 1, 2);
        int max = Collections.max(numbers);
        int min = Collections.min(numbers);
        System.out.println("Max: " + max + ", Min: " + min);
    }
}

Output:

Max: 8, Min: 1

Example 6: Filling a List

This example shows how to fill a list with a specific element.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class FillExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        Collections.fill(numbers, 0);
        System.out.println("Filled list: " + numbers);
    }
}

Output:

Filled list: [0, 0, 0, 0, 0]

Example 7: Copying a List

This example demonstrates how to copy elements from one list to another.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CopyExample {
    public static void main(String[] args) {
        List<Integer> src = Arrays.asList(1, 2, 3);
        List<Integer> dest = Arrays.asList(0, 0, 0, 0, 0);
        Collections.copy(dest, src);
        System.out.println("Destination list after copy: " + dest);
    }
}

Output:

Destination list after copy: [1, 2, 3, 0, 0]

Example 8: Checking Disjoint Collections

This example shows how to check if two collections have no elements in common.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class DisjointExample {
    public static void main(String[] args) {
        List<Integer> list1 = Arrays.asList(1, 2, 3);
        List<Integer> list2 = Arrays.asList(4, 5, 6);
        boolean areDisjoint = Collections.disjoint(list1, list2);
        System.out.println("Are the lists disjoint? " + areDisjoint);
    }
}

Output:

Are the lists disjoint? true

Example 9: Frequency of an Element

This example demonstrates how to find the frequency of an element in a collection.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class FrequencyExample {
    public static void main(String[] args) {
        List<String> items = Arrays.asList("Apple", "Banana", "Apple", "Cherry");
        int frequency = Collections.frequency(items, "Apple");
        System.out.println("Frequency of 'Apple': " + frequency);
    }
}

Output:

Frequency of 'Apple': 2

4. Conclusion

The Collections class in Java provides a variety of utility methods for managing and manipulating collections. These methods make it easy to perform common operations such as sorting, searching, and modifying collections in Java applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top