# Java Algorithms

In this tutorial, we will learn about various algorithms furnished by the Java collections framework with the help of examples.

The Java collections system gives different algorithms that can be used to control components stored in data structures.

Algorithms in Java are static techniques that can be used to perform different procedure on assortments.

Since algorithms can be used on different assortments, these are otherwise called generic algorithms.

How about we see the execution of various techniques accessible 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<>();

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 arranging happens in natural order (ascending order). However, we can alter the arranging order of the sort() technique using the Comparator interface.

## 2. Shuffling Using shuffle()

The shuffle() method for the Java collections system is used to annihilate any sort of order present in the data structure. It does just the opposite of the arranging. For instance,

``````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<>();

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<>();
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<>();

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 through Using binarySearch()

The binarySearch() method for the Java collections framework searches for the predefined component. It returns the position of the component in the predetermined collections. For instance,

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

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

// 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.

## 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<>();
System.out.println("ArrayList1: " + numbers);

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

ArrayList<Integer> newNumbers = new ArrayList<>();
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 for the Java collections system are used to locate the base and the maximum components, respectively. For instance,

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

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

// 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``````

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.