Sorting Collections
As we have seen, there are some classes in the collections framework that force the items within them to be sorted. Examples of that are TreeSet
and TreeMap
. The aspect to explore in this section is how to use existing sorting mechanisms for lists, but also for cases that have datasets with more than one value per data point.
The exercise we are doing throughout this chapter is a good example of a case where there are data points with more than one value. For each data point, we need to store the word for which we are calculating the frequency and the frequency itself. You might think that a good technique to sort that out is by storing the information in the form of maps. The unique words could be the keys, while the frequencies could be the values. This could be achieved by modifying the final part of the previous program to look like this:
Map map = new HashMap(); while (iterator.hasNext()) { // point to next element ...