CollectionsapiIcon (in the API reference documentation)class. All take the form of static methods whose first argument is the
collection on which the operation is to be performed.
The great majority of the algorithms provided by the Java platform operate on
ListapiIcon (in the API reference documentation)objects, but a couple of them (min and max) operate
on arbitrary
CollectionapiIcon (in the API reference documentation)objects. The algorithms are described below.
sort algorithm reorders a List so that its elements are ascending
order according to some ordering relation. Two forms of the operation are
provided. The simple form just takes a List and sorts it
according to its elements' natural ordering. If you're unfamiliar with
the concept of natural ordering, now would be a good time to read the interfaces/order Object Ordering section
The sort operation uses a slightly optimized merge sort
algorithm. If you don't know what this means but you do care, see any basic
textbook on algorithms. The important things to know about this algorithm are
that it is:
n log(n) time, and runs
substantially faster on nearly sorted lists. Empirical studies
showed it to be as fast as a highly optimized quicksort. Quicksort is
generally regarded to be faster than merge sort, but isn't stable, and
doesn't guarantee n log(n) performance.
a trivial little programsourceIcon (in a .java source file)
that prints out its arguments in lexicographic (alphabetical) order.
import java.util.*;
public class Sort {
public static void main(String args[]) {
List l = Arrays.asList(args);
Collections.sort(l);
System.out.println(l);
}
}
Let's run the program:
% java Sort i walk the line
[i, line, the, walk]
The program was included only to show you that I have nothing up my sleeve:
The algorithms really are as easy to use as they appear to be. I won't insult
your intelligence by including any more silly examples.
The second form of
Recall that the permutation groups are stored as values in a // Make a List of all permutation groups above size threshold
List winners = new ArrayList();
for (Iterator i = m.values().iterator(); i.hasNext(); ) {
List l = (List) i.next();
if (l.size()= minGroupSize)
winners.add(l);
}
// Sort permutation groups according to size
Collections.sort(winners, new Comparator() {
public int compare(Object o1, Object o2) {
return ((List)o2).size() - ((List)o1).size();
}
});
// Print permutation groups
for (Iterator i=winners.iterator(); i.hasNext(); ) {
List l = (List) i.next();
System.out.println(l.size() + : + l);
}
Running
interfaces/example-1dot2/dictionary.txtsame dictionary
in interfaces/mapthe Bhopal newssort takes a
ComparatorapiIcon (in the API reference documentation)in addition to a List and sorts the elements with the
Comparator. Remember the permutation group example at the end of
the interfaces/mapMap section? It printed
out the permutation groups in no particular order. Suppose you wanted to
print them out in reverse order of size, largest permutation group first.
The following example below shows you how to achieve this with the help of the
second form of the sort method.
Map,
in the form of List objects. The revised printing code
iterates through the Map's values-view, putting
every List that passes the minimum-size test into a
List of Lists. Then, the code sorts this
List using a Comparator that expects
List objects, and implements reverse-size ordering.
Finally, the code iterates through the now-sorted List,
printing its elements (the permutation groups). This code replaces the
printing code at the end of Perm's main method:
Map section,
with the same minimum permutation group size (eight)
produces the following output:
% java Perm2 dictionary.txt 8
12: [apers, apres, asper, pares, parse, pears, prase, presa, rapes,
reaps, spare, spear]
11: [alerts, alters, artels, estral, laster, ratels, salter, slater,
staler, stelar, talers]
10: [least, setal, slate, stale, steal, stela, taels, tales, teals,
tesla]
9: [estrin, inerts, insert, inters, niters, nitres, sinter, triens,
trines]
9: [capers, crapes, escarp, pacers, parsec, recaps, scrape, secpar,
spacer]
9: [anestri, antsier, nastier, ratines, retains, retinas, retsina,
stainer, stearin]
9: [palest, palets, pastel, petals, plates, pleats, septal, staple,
tepals]
8: [carets, cartes, caster, caters, crates, reacts, recast, traces]
8: [ates, east, eats, etas, sate, seat, seta, teas]
8: [arles, earls, lares, laser, lears, rales, reals, seral]
8: [lapse, leaps, pales, peals, pleas, salep, sepal, spale]
8: [aspers, parses, passer, prases, repass, spares, sparse, spears]
8: [earings, erasing, gainers, reagins, regains, reginas, searing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100