Core Java: Collections Framework and Algorithms

• Print
This chapter is from the book

Algorithms

Generic collection interfaces have a great advantage—you only need to implement 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 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 = iter.next();
while (iter.hasNext())
{
T next = iter.next();
if (largest.compareTo(next) < 0)
largest = next;
}
```

These loops are tedious to write, and they are 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 iteration 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 = iter.next();
while (iter.hasNext())
{
T next = iter.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 of which operates 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.

Sorting and Shuffling

Computer old-timers will sometimes reminisce about how they had to use punched cards and how they actually had to 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.

```List<String> staff = new LinkedList<String>();
// fill collection . . .;
Collections.sort(staff);
```

This method assumes that the list elements implement the Comparable interface. If you want to sort the list in some other way, you can pass a Comparator object as a second parameter. (We discussed comparators on page 105.) Here is how you can sort a list of items.

```Comparator<Item> itemComparator = new
Comparator<Item>()
{
public int compare(Item a, Item b)
{
return a.partNumber - b.partNumber;
}
});
Collections.sort(items, itemComparator);
```

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

```Collections.sort(staff, Collections.reverseOrder())
```

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

```Collections.sort(items, Collections.reverseOrder(itemComparator))
```

reverses the ordering of the itemComparator.

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 list can be inefficient. You can actually sort lists efficiently by using a form of merge sort (see, for example, Algorithms in C++ by Robert Sedgewick [Addison-Wesley 1998, pp. 366–369]). However, the implementation in the Java programming language does not do that. It simply dumps all elements into an array, sorts the array by using a different variant of merge sort, and then copies the sorted sequence back into the list.

The merge 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 scenario. 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.

Because collections need not implement all of their “optional” methods, 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. You supply the list to be shuffled and a random number generator. For example,

```ArrayList<Card> cards = . . .;
Collections.shuffle(cards);
```

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

The program in Example 2-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 6 values from the shuffled list. Finally, it sorts the selected values and prints them.

Example 2-7. ShuffleTest.java

``` 1. import java.util.*;
2.
3. /**
4.    This program demonstrates the random shuffle and sort algorithms.
5. */
6. public class ShuffleTest
7. {
8.    public static void main(String[] args)
9.    {
10.       List<Integer> numbers = new ArrayList<Integer>();
11.       for (int i = 1; i <= 49; i++)
13.       Collections.shuffle(numbers);
14.       List<Integer> winningCombination = numbers.subList(0, 6);
15.       Collections.sort(winningCombination);
16.       System.out.println(winningCombination);
17.    }
18. }
```
```
java.util.Collections 1.2
```
• static <T extends Comparable<? super T>> void sort(List<T> elements)
• static <T> void sort(List<T> elements, Comparator<? super T> c)

sort the elements in the list, using a stable sort algorithm. The algorithm is guaranteed to run in O(n log n) time, where n is the length of the list.

• static void shuffle(List<?> elements)
• static void shuffle(List<?> elements, Random r)

randomly shuffle the elements in the list. This algorithm runs in O(n a(n)) time, where n is the length of the list and a(n) is the average time to access an element.

• static <T> Comparator<T> reverseOrder()

returns a comparator that sorts elements in the reverse order of the one given by the compareTo method of the Comparable interface.

• static <T> Comparator<T> reverseOrder(Comparator<T> comp)

returns a comparator that sorts elements in the reverse order of the one given by comp.

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, then you can look at the middle element and check whether it is larger than the element that you are trying to find. If so, you keep looking in the first half of the array; otherwise, you look in the second half. That cuts the problem in half. 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 answer. 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, then you must supply a comparator object as well.

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

A return value of ≥ 0 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)
```

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.

JDK 1.3 had no separate interface for an ordered collection with efficient random access, and the binarySearch method employed a very crude device, checking whether the list parameter extended the AbstractSequentialList class. This has been fixed in JDK 1.4. Now the binarySearch method checks whether the list parameter implements the RandomAccess interface. If it does, then the method carries out a binary search. Otherwise, it uses a linear search.

```
java.util.Collections 1.2
```
• static <T extends Comparable<? super T>> int binarySearch(List<T> elements, T key)
• static <T> int binarySearch(List<T> elements, T key, Comparator<? super T> c)

search for a key in a sorted list, using a linear search if elements extends the AbstractSequentialList class, and a binary search in all other cases. The methods are guaranteed to run in O(a(n) log n) time, where n is the length of the list and a(n) is the average time to access an element. The methods return either the index of the key in the list, or a negative value i if the key is not present in the list. In that case, the key should be inserted at index –i1 for the list to stay sorted.

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. When you see a call to a method such as Collections.max, you know right away what the code does.

The following API notes describe the simple algorithms in the Collections class.

```
java.util.Collections 1.2
```
• static <T extends Comparable<? super T>> T min(Collection<T> elements)
• static <T extends Comparable<? super T>> T max(Collection<T> elements)
• static <T> min(Collection<T> elements, Comparator<? super T> c)
• static <T> max(Collection<T> elements, Comparator<? super T> c)

return the smallest or largest element in the collection. (The parameter bounds are simplified for clarity.)

• static <T> void copy(List<? super T> to, List<T> from)

copies all elements from a source list to the same positions in the target list. The target list must be at least as long as the source list.

• static <T> void fill(List<? super T> l, T value)

sets all positions of a list to the same value.

• static <T> boolean addAll(Collection<? super T> c, T... values) 5.0

adds all values to the given collection and returns true if the collection changed as a result.

• static <T> boolean replaceAll(List<T> l, T oldValue, T newValue) 1.4

replaces all elements equal to oldValue with newValue.

• static int indexOfSubList(List<?> l, List<?> s) 1.4
• static int lastIndexOfSubList(List<?> l, List<?> s) 1.4

return the index of the first or last sublist of l equalling s, or −1 if no sublist of l equals s. For example, if l is [s, t, a, r] and s is [t, a, r], then both methods return the index 1.

• static void swap(List<?> l, int i, int j) 1.4

swaps the elements at the given offsets.

• static void reverse(List<?> l)

reverses the order of the elements in a list. For example, reversing the list [t, a, r] yields the list [r, a, t]. This method runs in O(n) time, where n is the length of the list.

• static void rotate(List<?> l, int d) 1.4

rotates the elements in the list, moving the entry with index i to position

(i + d) % l.size(). For example, rotating the list [t, a, r] by 2 yields the list [a, r, t]. This method runs in O(n) time, where n is the length of the list.

• static int frequency(Collection<?> c, Object o) 5.0

returns the count of elements in c that equal the object o.

• boolean disjoint(Collection<?> c1, Collection<?> c2) 5.0

returns true if the collections have no elements in common.

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 fill a JMenu with a set of menu items. Traditionally, such a method might have been implemented like this:

```void fillMenu(JMenu menu, ArrayList<JMenuItem> items)
{
}
```

However, you now constrained the caller of your method—the caller must supply the choices in an ArrayList. If the choices happen to be in another container, 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? In this case, you just need to visit all elements, a capability of the basic Collection interface. Here is how you can rewrite the fillMenu method to accept collections of any kind.

```void fillMenu(JMenu menu, Collection<JMenuItem> items)
{

}
```

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

If it is such a good idea to use collection interfaces as method parameters, why doesn't the Java library follow this rule more often? For example, the JComboBox class has two constructors:

```JComboBox(Object[] items)
JComboBox(Vector<?> items)
```

The reason is simply timing. The Swing library was created before the collections library.

If you write a method that returns a collection, you may also want to return an interface instead of a class because you can then change your mind and reimplement the method later with a different collection.

For example, let's write a method getAllItems that returns all items of a menu.

```List<MenuItem> getAllItems(JMenu menu)
{
for (int i = 0; i < menu.getItemCount(); i++)
return items;
}
```

Later, you can decide that you don't want to copy the items but simply provide a view into them. You achieve this by returning an anonymous subclass of AbstractList.

```List<MenuItem> getAllItems(final JMenu menu)
{
return new
{
{
return item.getItem(i);
}
public int size()
{
return item.getItemCount();
}
};
}
```

Of course, this is an advanced technique. If you employ it, be careful to document exactly which “optional” operations are supported. In this case, you must advise the caller that the returned object is an unmodifiable list.