# Java Algorithms

#### In this tutorial, we will learn about different algorithms provided by the Java collections framework with the help of examples.

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

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.

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

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

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

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

# Special 95% discount

## 2000+ Applied Machine Learning & Data Science Recipes

### Portfolio Projects for Aspiring Data Scientists: Tabular Text & Image Data Analytics as well as Time Series Forecasting in Python & R ## Two Machine Learning Fields

There are two sides to machine learning:

• Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
• Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.
`Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  `