Week5

The Collections utility class

Before starting this last paragraph, let us first test our knowledge of what we have learned so far.

Exercise

Test your knowledge

In this quiz, you can test your knowledge on the subjects covered in this chapter.

What is the difference between a List and a Set object?

What is a Map?

Collections class

Java contains a number of classes that provide handy, static utility methods. Examples of this you may have see before are Math, using Math.pow() or Math.sin(), or Arrays, using Arrays.toString() to convert an array to a string you can print conveniently.

Collections is a similar class with useful utility methods that can be used to deal with all kinds of Collection objects.

Some useful methods for List types:

public static <T extends Comparable<? super T>> void sort(List<T> list)
public static <T> void sort(List<T> list, Comparator<? super T> c)
public static void reverse(List<?> list)
public static void shuffle(List<?> list, Random rnd)

The sort method has been discussed last week, and can be used to sort a List, either by relying on the natural order of an object, or specifying a custom order by providing a Comparator as a second argument. We can also reverse the order of the list, or randomize the order of the elements by using a Random object with the shuffle method, which randomizes the order like you would shuffle a deck of cards.

There are also a number of useful methods for very basic statistics:

public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp)
public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp)
public static int frequency(Collection<?> c, Object o)

For both min and max, if the type T implements the Comparable interface, the Comparator doesn't have to be passed.

We can find the minimum or maximum value in any Collection (so this works for both Set and List objects) according to an order specified by a given Comparator with the min and max methods. The frequency method can be used to determine how often a certain element appears in a Collection, which will work on a Set and a List, even though a typical Set will contain an element at most once.

If we want to return a version of a List, Set or Map that cannot be modified (and we don’t want to copy it), there are helper methods that wrap the List, Set or Map in a so-called proxy that will throw an Exception as soon as you call any mutator method on it, but which can still be used to read the data from the data structure.

public static <T> Set<T> unmodifiableSet(Set<? extends T> s)
public static <T> List<T> unmodifiableList(List<? extends T> list)
public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m)

With these methods, we have a very powerful and flexible toolkit to handle large collections of objects.

Exercise

Test your knowledge

In this quiz, you can test your knowledge on the subjects covered in this chapter.

What is the difference between the Collection and the Collections type?


Suppose you have a Set<String> documents and a List<Integer> numbers. You want to compute the first and last documents from documents according to their alphabetical order, and the smallest and greatest number in numbers. Can you do this without writing a loop yourself?


Now that you have seen the different collection types, determine for each of the following use cases which data structure (e.g. LinkedList, ArrayList, HashSet, TreeSet, HashMap or TreeMap) you would prefer to use.

  1. You have set of unique specimens, each with an unique numerical identifier. You want to be able to keep tracks of which specimens were involved in various experiments, but do not care about the order or frequency of occurrence. You also want to be able to access a subset from those specimens within a certain range of IDs.
  2. There is an interface EventType with different classes implementing this interface. You want to keep track how often each EventType occurs during a simulation. There is no natural order of EventType objects available.
  3. You need to analyze the history of Product objects that are viewed by a user of your store's website. The order of the products that were visited is important and you need to have quick access to products at various locations in the list.
  4. You want to keep track of Task objects that represent work you still need to perform in a first-in first-out order. After a task is completed, you want to remove it, and when a new task comes up you want to add it.
  5. You want to associate the names of different persons with those persons' phone numbers. Preferably, you want to have the persons' names in alphabetical order when you print them.
  6. You write an algorithm that wants to visit all Location objects in a certain Environment. While exploring, you want to keep track of Location objects you have visited at least once. No natural order of Location objects is available.
You have reached the end of this section!