Algorithms in Java

In addition to implementing collection classes, the Java collections framework also provides a number of useful algorithms. In the following sections, you will see how to use these algorithms and how to write your own algorithms that work well with the collections framework.

1. Why Generic Algorithms?

Generic collection interfaces have a great advantage—you only need to imple­ment your algorithms once. For example, consider a simple algorithm to compute the maximum element in a collection. Traditionally, programmers would implement such an algorithm as a loop. Here is how you can find the largest element of an array.

if (a.length == 0) throw new NoSuchElementException();

T largest = a[0];

for (int i = 1; i < a.length; i++)

if (largest.compareTo(a[i]) < 0)

largest = a[i];

Of course, to find the maximum of an array list, you would write the code slightly differently.

if (v.size() == 0) throw new NoSuchElementException();

T largest = v.get(0);

for (int i = 1; i < v.size(); i++)

if (largest.compareTo(v.get(i)) < 0)

largest = v.get(i);

What about a linked list? You don’t have efficient random access in a linked list, but you can use an iterator.

if (l.isEmpty()) throw new NoSuchElementException();

Iterator<T> iter = l.iterator();

T largest =; while (iter.hasNext())


T next =;

if (largest.compareTo(next) < 0)

largest = next;


These loops are tedious to write, and just a bit error-prone. Is there an off- by-one error? Do the loops work correctly for empty containers? For containers with only one element? You don’t want to test and debug this code every time, but you also don’t want to implement a whole slew of methods, such as these:

static <T extends Comparable> T max(T[] a)

static <T extends Comparable> T max(ArrayList<T> v)

static <T extends Comparable> T max(LinkedList<T> l)

That’s where the collection interfaces come in. Think of the minimal collection interface that you need to efficiently carry out the algorithm. Random access with get and set comes higher in the food chain than simple iteration. As you have seen in the computation of the maximum element in a linked list, random access is not required for this task. Computing the maximum can be done simply by iterating through the elements. Therefore, you can implement the max method to take any object that implements the Collection interface.

public static <T extends Comparable> T max(Collection<T> c)


if (c.isEmpty()) throw new NoSuchElementException();

Iterator<T> iter = c.iterator();

T largest =;

while (iter.hasNext())


T next =;

if (largest.compareTo(next) < 0)

largest = next;


return largest;


Now you can compute the maximum of a linked list, an array list, or an array, with a single method.

That’s a powerful concept. In fact, the standard C++ library has dozens of useful algorithms, each operating on a generic collection. The Java library is not quite so rich, but it does contain the basics: sorting, binary search, and some utility algorithms.

2. Sorting and Shuffling

Computer old-timers will sometimes reminisce about how they had to use punched cards and to actually program, by hand, algorithms for sorting. Nowadays, of course, sorting algorithms are part of the standard library for most programming languages, and the Java programming language is no exception.

The sort method in the Collections class sorts a collection that implements the List interface.

var staff = new LinkedList<String>();

fill collection


This method assumes that the list elements implement the Comparable interface. If you want to sort the list in some other way, you can use the sort method of the List interface and pass a Comparator object. Here is how you can sort a list of employees by salary:


If you want to sort a list in descending order, use the static convenience method Comparator.reverseOrder(). It returns a comparator that returns b.compareTo(a). For example,


sorts the elements in the list staff in reverse order, according to the ordering given by the compareTo method of the element type. Similarly,

staff.sort(Comparator.comparingDouble(Employee::getSalary).reversed()) sorts by descending salary.

You may wonder how the sort method sorts a list. Typically, when you look at a sorting algorithm in a book on algorithms, it is presented for arrays and uses random element access. However, random access in a linked list is in­efficient. You can actually sort linked lists efficiently by using a form of merge sort. However, the implementation in the Java programming language does not do that. It simply dumps all elements into an array, sorts the array, and then copies the sorted sequence back into the list.

The sort algorithm used in the collections library is a bit slower than Quick­Sort, the traditional choice for a general-purpose sorting algorithm. However, it has one major advantage: It is stable, that is, it doesn’t switch equal elements. Why do you care about the order of equal elements? Here is a common sce­nario. Suppose you have an employee list that you already sorted by name. Now you sort by salary. What happens to employees with equal salary? With a stable sort, the ordering by name is preserved. In other words, the outcome is a list that is sorted first by salary, then by name.

Collections need not implement all of their “optional” methods, so all methods that receive collection parameters must describe when it is safe to pass a collection to an algorithm. For example, you clearly cannot pass an unmodifiableList list to the sort algorithm. What kind of list can you pass? According to the documentation, the list must be modifiable but need not be resizable.

The terms are defined as follows:

  • A list is modifiable if it supports the set method.
  • A list is resizable if it supports the add and remove operations.

The Collections class has an algorithm shuffle that does the opposite of sorting—it randomly permutes the order of the elements in a list. For example:

ArrayList<Card> cards = . . .;


If you supply a list that does not implement the RandomAccess interface, the shuffle method copies the elements into an array, shuffles the array, and copies the shuffled elements back into the list.

The program in Listing 9.7 fills an array list with 49 Integer objects containing the numbers 1 through 49. It then randomly shuffles the list and selects the first six values from the shuffled list. Finally, it sorts the selected values and prints them.

3. Binary Search

To find an object in an array, you normally visit all elements until you find a match. However, if the array is sorted, you can look at the middle element and check whether it is larger than the element that you are trying to find. If so, keep looking in the first half of the array; otherwise, look in the second half. That cuts the problem in half, and you keep going in the same way. For example, if the array has 1024 elements, you will locate the match (or confirm that there is none) after 10 steps, whereas a linear search would have taken you an average of 512 steps if the element is present, and 1024 steps to confirm that it is not.

The binarySearch of the Collections class implements this algorithm. Note that the collection must already be sorted, or the algorithm will return the wrong an­swer. To find an element, supply the collection (which must implement the List interface—more on that in the note below) and the element to be located. If the collection is not sorted by the compareTo element of the Comparable interface, supply a comparator object as well.

i = Collections.binarySearch(c, element);

i = Collections.binarySearch(c, element, comparator);

A non-negative return value from the binarySearch method denotes the index of the matching object. That is, c.get(i) is equal to element under the comparison order. If the value is negative, then there is no matching element. However, you can use the return value to compute the location where you should insert element into the collection to keep it sorted. The insertion location is

insertionPoint = -i – 1;

It isn’t simply -i because then the value of 0 would be ambiguous. In other words, the operation

if (i < 0)

c.add(-i – 1, element);

adds the element in the correct place.

To be worthwhile, binary search requires random access. If you have to iterate one by one through half of a linked list to find the middle element, you have lost all advantage of the binary search. Therefore, the binarySearch algorithm reverts to a linear search if you give it a linked list.

4. Simple Algorithms

The Collections class contains several simple but useful algorithms. Among them is the example from the beginning of this section-finding the maximum value of a collection. Others include copying elements from one list to another, filling a container with a constant value, and reversing a list.

Why supply such simple algorithms in the standard library? Surely most programmers could easily implement them with simple loops. We like the algorithms because they make life easier for the programmer reading the code. When you read a loop that was implemented by someone else, you have to decipher the original programmer’s intentions. For example, look at this loop:

for (int i = 0; i < words.size(); i++)

if (words.get(i).equals(“C++”)) words.set(i, “Java”);

Now compare the loop with the call

Collections.replaceAU(words, “C++”, “Java”);

When you see the method call, you know right away what the code does.

The API notes at the end of this section describe the simple algorithms in the Collections class.

The default methods Collection.removelf and List.replaceAll that are just a bit more complex. You provide a lambda expression to test or transform elements. For example, here we remove all short words and change the remaining ones to lowercase:

words.removeIf(w -> w.tength() <= 3);


5. Bulk Operations

There are several operations that copy or remove elements “in bulk.” The call


removes all elements from coll1 that are present in coll2. Conversely,


removes all elements from coll1 that are not present in coll2. Here is a typical application.

Suppose you want to find the intersection of two sets—the elements that two sets have in common. First, make a new set to hold the result.

var result = new HashSet<String>(firstSet);

Here, we use the fact that every collection has a constructor whose parameter is another collection that holds the initialization values.

Now, use the retainAtt method:


It retains all elements that occur in both sets. You have formed the intersection without programming a loop.

You can carry this idea further and apply a bulk operation to a view. For ex­ample, suppose you have a map that maps employee IDs to employee objects and you have a set of the IDs of all employees that are to be terminated.

Map<String, Emptoyee> staffMap = . . .;

Set<String> terminatedIDs = . . .;

Simply form the key set and remove all IDs of terminated employees.


Since the key set is a view into the map, the keys and associated employee names are automatically removed from the map.

By using a subrange view, you can restrict bulk operations to sublists and subsets. For example, suppose you want to add the first ten elements of a list to another container. Form a sublist to pick out the first ten:

relocated.addAU(staff.subList(0, 10));

The subrange can also be a target of a mutating operation.

staff.subList(0, 10).clear();

6. Converting between Collections and Arrays

Large portions of the Java platform API were designed before the collections framework was created. As a result, you will occasionally need to translate between traditional arrays and the more modern collections.

If you have an array that you need to turn into a collection, the List.of wrapper serves this purpose. For example:

String[] vatues = . . .;

var staff = new HashSet<>(List.of(values));

Obtaining an array from a collection is a bit trickier. Of course, you can use the toArray method:

Object[] values = staff.toArray();

But the result is an array of objects. Even if you know that your collection contained objects of a specific type, you cannot use a cast:

String[] values = (String!)) staff.toArray(); // ERROR

The array returned by the toArray method was created as an Object[] array, and you cannot change its type. Instead, use a variant of the toArray method and give it an array of length 0 of the type that you’d like. The returned array is then created as the same array type:

String[] values = staff.toArray(new String[0]);

If you like, you can construct the array to have the correct size:

staff.toArray(new String[staff.size()]);

In this case, no new array is created.

7. Writing Your Own Algorithms

If you write your own algorithm (or, in fact, any method that has a collection as a parameter), you should work with interfaces, not concrete implementations, whenever possible. For example, suppose you want to process items. Of course, you can implement a method like this:

public void processItems(ArrayList<Item> items)


for (Item item : items)

do something with item


However, you now constrained the caller of your method—the caller must supply the items in an ArrayList. If the items happen to be in another collection, they first need to be repackaged. It is much better to accept a more general collection.

You should ask yourself this: What is the most general collection interface that can do the job? Do you care about the order? Then you should accept a List. But if the order doesn’t matter, you can accept collections of any kind:

public void processItems(Collection<Item> items)


for (Item item : items)

do something with item


Now, anyone can call this method with an ArrayList or a LinkedList, or even with an array wrapped with the List.of wrapper.

Conversely, if your method returns multiple elements, you don’t want to constrain yourself against future improvements. For example, consider

public ArrayList<Item> lookupItems(. . .)


var result = new ArrayList<Item>();

return result;


This method promises to return an ArrayList, even though the caller almost certainly doesn’t care what kind of lists it is. If instead you return a List, you can at any time add a branch that returns an empty or singleton list by calling List.of.

Source: Horstmann Cay S. (2019), Core Java. Volume I – Fundamentals, Pearson; 11th edition.

Leave a Reply

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