bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Java/Java Data Structures
Java•Java Data Structures

Java Algorithms

Native lesson simulator

Binary search

Java Algorithms

Binary searchtarget = 1120317273114155256lomidhi

Check index 3: 7.

In the previous chapters, you learned how data structures (like ArrayList , HashMap , etc.) are used to store and organize data.

Algorithms are used to solve problems by sorting, searching, and manipulating data structures.

In Java, many useful algorithms are already built into the Collections class (found in the java.util package), so you don't have to write them from scratch.

Searching

To find elements in a list, Java provides helper methods. The most common is Collections.binarySearch() , which searches in a sorted list :

ArrayList

Sorting

Sorting is one of the most common algorithms. With ArrayList , you can use Collections.sort() to sort the elements:

Example

import java.util.*;
public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> numbers = new ArrayList<>();
    numbers.add(5);
    numbers.add(1);
    numbers.add(7);
    numbers.add(3);
    numbers.add(9);
    Collections.sort(numbers);
    System.out.println(numbers); // [1, 3, 5, 7, 9]
  }
}

You can also sort in reverse order with Collections.sort(list, Collections.reverseOrder()) :

ArrayList

Iterating

Iterating (looping) through elements is another common algorithm. You can use the for-each loop or the Iterator interface:

ArrayList
ArrayList

Other Useful Algorithms

The Collections class contains many more algorithms, such as:

  • Collections.max() - find the largest element
  • Collections.min() - find the smallest element
  • Collections.shuffle() - randomly shuffle elements
  • Collections.frequency() - count how many times an element appears
  • Collections.swap() - swap two elements in a list

In this example, we use Collections.max() and Collections.min() to find the largest and smallest element in an ArrayList :

Example

import java.util.*;
public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> numbers = new ArrayList<>();
    numbers.add(5);
    numbers.add(1);
    numbers.add(7);
    numbers.add(3);
    numbers.add(9);
    System.out.println("Max: " + Collections.max(numbers));
    System.out.println("Min: " + Collections.min(numbers));
  }
}

Example

import java.util.*;
public class Main {
  public static void main(String[] args) {
    ArrayList<String> cards = new ArrayList<>();
    cards.add("Ace");
    cards.add("King");
    cards.add("Queen");
    cards.add("Jack");
    Collections.shuffle(cards);
    System.out.println(cards);
  }
}

Randomly shuffle an ArrayList

Collections.frequency() counts how many times an element appears in a list:

Example

import java.util.*;
public class Main {
  public static void main(String[] args) {
    ArrayList<String> fruits = new ArrayList<>();
    fruits.add("Apple");
    fruits.add("Banana");
    fruits.add("Orange");
    fruits.add("Banana");
    fruits.add("Mango");
    int count = Collections.frequency(fruits, "Banana");
    System.out.println("Banana appears: " + count + " times");
  }
}

Collections.swap() swaps two elements in a list:

Example

import java.util.*;
public class Main {
  public static void main(String[] args) {
    ArrayList<String> fruits = new ArrayList<>();
    fruits.add("Apple");
    fruits.add("Banana");
    fruits.add("Orange");
    fruits.add("Mango");
    Collections.swap(fruits, 0, 2); // Swap first and third element
    System.out.println(fruits);
  }
}

Summary

  • An algorithm is a procedure to solve a problem.
  • Java provides built-in algorithms in the Collections class.
  • Common algorithms include searching , sorting , iterating , and finding min/max .
  • Algorithms work together with data structures (like ArrayList , HashSet , etc.) to make your programs more powerful and efficient.

Previous

Java Iterator

Next chapter

Java Advanced

Start with Java Wrapper Classes