Java Algorithms

The Java collections framework provides various algorithms that can be used to manipulate elements stored in data structures.

Algorithms in Java are static methods that can be used to perform various operations on collections.

Since algorithms can be used on various collections, these are also known as generic algorithms.

Let's see the implementation of different methods available in the collections framework.


1. Sorting Using sort()

The sort() method provided by the collections framework is used to sort elements. For example,

import java.util.ArrayList;
import java.util.Collections;

class Main {
    public static void main(String[] args) {

        // Creating an array list
        ArrayList<Integer> numbers = new ArrayList<>();

        // Add elements
        numbers.add(4);
        numbers.add(2);
        numbers.add(3);
        System.out.println("Unsorted ArrayList: " + numbers);

        // Using the sort() method
        Collections.sort(numbers);
        System.out.println("Sorted ArrayList: " + numbers);

    }
}

Output

Unsorted ArrayList: [4, 2, 3]
Sorted ArrayList: [2, 3, 4]

Here the sorting occurs in natural order (ascending order). However, we can customize the sorting order of the sort() method using the Comparator interface.

To learn more, visit Java Sorting.


2. Shuffling Using shuffle()

The shuffle() method of the Java collections framework is used to destroy any kind of order present in the data structure. It does just the opposite of the sorting. For example,

import java.util.ArrayList;
import java.util.Collections;

class Main {
    public static void main(String[] args) {

        // Creating an array list
        ArrayList<Integer> numbers = new ArrayList<>();

        // Add elements
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        System.out.println("Sorted ArrayList: " + numbers);

        // Using the shuffle() method
        Collections.shuffle(numbers);
        System.out.println("ArrayList using shuffle: " + numbers);

    }
}

Output

Sorted ArrayList: [1, 2, 3]
ArrayList using shuffle: [2, 1, 3]

When we run the program, the shuffle() method will return a random output.

The shuffling algorithm is mainly used in games where we want random output.


3. Routine Data Manipulation

In Java, the collections framework provides different methods that can be used to manipulate data.

  • reverse() - reverses the order of elements
  • fill() - replace every element in a collection with the specified value
  • copy() - creates a copy of elements from the specified source to destination
  • swap() - swaps the position of two elements in a collection
  • addAll() - adds all the elements of a collection to other collection

For example,

import java.util.Collections;
import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        System.out.println("ArrayList1: " + numbers);

        // Using reverse()
        Collections.reverse(numbers);
        System.out.println("Reversed ArrayList1: " + numbers);

        // Using swap()
        Collections.swap(numbers, 0, 1);
        System.out.println("ArrayList1 using swap(): " + numbers);

        ArrayList<Integer> newNumbers = new ArrayList<>();

        // Using addAll
        newNumbers.addAll(numbers);
        System.out.println("ArrayList2 using addAll(): " + newNumbers);

        // Using fill()
        Collections.fill(numbers, 0);
        System.out.println("ArrayList1 using fill(): " + numbers);

        // Using copy()
        Collections.copy(newNumbers, numbers);
        System.out.println("ArrayList2 using copy(): " + newNumbers);
    }
}

Output

ArrayList1: [1, 2]
Reversed ArrayList1: [2, 1]
ArrayList1 Using swap(): [1, 2]
ArrayList2 using addALl(): [1, 2]
ArrayList1 using fill(): [0, 0]
ArrayList2 using copy(): [0, 0]

Note: While performing the copy() method both the lists should be of the same size.


4. Searching Using binarySearch()

The binarySearch() method of the Java collections framework searches for the specified element. It returns the position of the element in the specified collections. For example,

import java.util.Collections;
import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);

        // Using binarySearch()
        int pos = Collections.binarySearch(numbers, 3);
        System.out.println("The position of 3 is " + pos);
    }
}

Output

The position of 3 is 2.

Note: The collection should be sorted before performing the binarySearch() method.

To know more, visit Java Binary Search.


5. Composition

  • frequency() - returns the count of the number of times an element is present in the collection
  • disjoint() - checks if two collections contain some common element

For example,

import java.util.Collections;
import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(2);
        System.out.println("ArrayList1: " + numbers);

        int count = Collections.frequency(numbers, 2);
        System.out.println("Count of 2: " + count);

        ArrayList<Integer> newNumbers = new ArrayList<>();
        newNumbers.add(5);
        newNumbers.add(6);
        System.out.println("ArrayList2: " + newNumbers);

        boolean value = Collections.disjoint(numbers, newNumbers);
        System.out.println("Two lists are disjoint: " + value);
    }
}

Output

ArrayList1: [1, 2, 3, 2]
Count of 2: 2
ArrayList2: [5, 6]
Two lists are disjoint: true

6. Finding Extreme Values

The min() and max() methods of the Java collections framework are used to find the minimum and the maximum elements, respectively. For example,

import java.util.Collections;
import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);

        // Using min()
        int min = Collections.min(numbers);
        System.out.println("Minimum Element: " + min);

        // Using max()
        int max = Collections.max(numbers);
        System.out.println("Maximum Element: " + max);
    }
}

Output

Minimum Element: 1
Maximum Element: 3
Did you find this article helpful?