Java集合框架官方教程(6):算法
2016-07-29 00:00
459 查看
Lesson: Algorithms
The polymorphic algorithms described here are pieces of reusable functionality provided by the Java platform. All of them come from theCollectionsclass, and 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
Listinstances, but a few of them operate on arbitrary
Collectioninstances. This section briefly describes the following algorithms:
Sorting
Shuffling
Routine Data Manipulation
Searching
Composition
Finding Extreme Values
Sorting
Thesortalgorithm reorders a
Listso that its elements are in ascending order according to an ordering relationship. Two forms of the operation are provided. The simple form takes a
Listand sorts it according to its elements' natural ordering. If you're unfamiliar with the concept of natural ordering, read theObject Ordering section.
The
sortoperation uses a slightly optimized merge sort algorithm that is fast and stable:
Fast: It is guaranteed to run in
n log(n)time and runs substantially faster on nearly sorted lists. Empirical tests showed it to be as fast as a highly optimized quicksort. A quicksort is generally considered to be faster than a merge sort but isn't stable and doesn't guarantee
n log(n)performance.
Stable: It doesn't reorder equal elements. This is important if you sort the same list repeatedly on different attributes. If a user of a mail program sorts the inbox by mailing date and then sorts it by sender, the user naturally expects that the now-contiguous list of messages from a given sender will (still) be sorted by mailing date. This is guaranteed only if the second sort was stable.
The following
trivial programprints out its arguments in lexicographic (alphabetical) order.
import java.util.*; public class Sort { public static void main(String[] args) { List<String> list = Arrays.asList(args); Collections.sort(list); System.out.println(list); } }Let's run the program.
% java Sort i walk the lineThe following output is produced.
[i, line, the, walk]
The program was included only to show you that algorithms really are as easy to use as they appear to be.
The second form of
sorttakes a
Comparatorin addition to a
Listand sorts the elements with the
Comparator. Suppose you want to print out the anagram groups from our earlier example in reverse order of size — largest anagram group first. The example that follows shows you how to achieve this with the help of the second form of the
sortmethod.
Recall that the anagram groups are stored as values in a
Map, in the form of
Listinstances. The revised printing code iterates through the
Map's values view, putting every
Listthat passes the minimum-size test into a
Listof
Lists. Then the code sorts this
List, using a
Comparatorthat expects
Listinstances, and implements reverse size-ordering. Finally, the code iterates through the sorted
List, printing its elements (the anagram groups). The following code replaces the printing code at the end of the
mainmethod in the
Anagramsexample.
// Make aList of all anagram groups above size threshold. List<List<String>> winners = new ArrayList<List<String>>(); for (List<String> l : m.values()) if (l.size() >= minGroupSize) winners.add(l); // Sort anagram groups according to size Collections.sort(winners, newComparator<List<String>>() { public int compare(List<String> o1,List<String> o2) { return o2.size() - o1.size(); }}); // Print anagram groups. for (List<String> l : winners) System.out.println(l.size() + ": " + l);Running
the programon the
same dictionaryas in
The Map Interface section, with the same minimum anagram group size (eight), produces the following output.
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: [palest, palets, pastel, petals, plates, pleats, septal, staple, tepals] 9: [anestri, antsier, nastier, ratines, retains, retinas, retsina, stainer, stearin] 8: [lapse, leaps, pales, peals, pleas, salep, sepal, spale] 8: [aspers, parses, passer, prases, repass, spares, sparse, spears] 8: [enters, nester, renest, rentes, resent, tenser, ternes,??????treens] 8: [arles, earls, lares, laser, lears, rales, reals, seral] 8: [earings, erasing, gainers, reagins, regains, reginas, searing, seringa] 8: [peris, piers, pries, prise, ripes, speir, spier, spire] 8: [ates, east, eats, etas, sate, seat, seta, teas] 8: [carets, cartes, caster, caters, crates, reacts, recast,??????traces]
Shuffling
Theshufflealgorithm does the opposite of what
sortdoes, destroying any trace of order that may have been present in a
List. That is, this algorithm reorders the
Listbased on input from a source of randomness such that all possible permutations occur with equal likelihood, assuming a fair source of randomness. This algorithm is useful in implementing games of chance. For example, it could be used to shuffle a
Listof
Cardobjects representing a deck. Also, it's useful for generating test cases.
This operation has two forms: one takes a
Listand uses a default source of randomness, and the other requires the caller to provide aRandom object to use as a source of randomness. The code for this algorithm is used as an example in the
Listsection.
Routine Data Manipulation
TheCollectionsclass provides five algorithms for doing routine data manipulation on
Listobjects, all of which are pretty straightforward:
reverse— reverses the order of the elements in a
List.
fill— overwrites every element in a
Listwith the specified value. This operation is useful for reinitializing a
List.
copy— takes two arguments, a destination
Listand a source
List, and copies the elements of the source into the destination, overwriting its contents. The destination
Listmust be at least as long as the source. If it is longer, the remaining elements in the destination
Listare unaffected.
swap— swaps the elements at the specified positions in a
List.
addAll— adds all the specified elements to a
Collection. The elements to be added may be specified individually or as an array.
Searching
ThebinarySearchalgorithm searches for a specified element in a sorted
List. This algorithm has two forms. The first takes a
Listand an element to search for (the "search key"). This form assumes that the
Listis sorted in ascending order according to the natural ordering of its elements. The second form takes a
Comparatorin addition to the
Listand the search key, and assumes that the
Listis sorted into ascending order according to the specified
Comparator. The
sortalgorithm can be used to sort the
Listprior to calling
binarySearch.
The return value is the same for both forms. If the
Listcontains the search key, its index is returned. If not, the return value is
(-(insertion point) - 1), where the insertion point is the point at which the value would be inserted into the
List, or the index of the first element greater than the value or
list.size()if all elements in the
Listare less than the specified value. This admittedly ugly formula guarantees that the return value will be
>= 0if and only if the search key is found. It's basically a hack to combine a boolean
(found)and an integer
(index)into a single
intreturn value.
The following idiom, usable with both forms of the
binarySearchoperation, looks for the specified search key and inserts it at the appropriate position if it's not already present.
int pos =Collections.binarySearch(list, key); if (pos < 0) l.add(-pos-1, key);
Composition
The frequency and disjoint algorithms test some aspect of the composition of one or moreCollections:
frequency— counts the number of times the specified element occurs in the specified collection
disjoint— determines whether two
Collectionsare disjoint; that is, whether they contain no elements in common
Finding Extreme Values
Theminand the
maxalgorithms return, respectively, the minimum and maximum element contained in a specified
Collection. Both of these operations come in two forms. The simple form takes only a
Collectionand returns the minimum (or maximum) element according to the elements' natural ordering. The second form takes a
Comparatorin addition to the
Collectionand returns the minimum (or maximum) element according to the specified
Comparator.
Original:
http://docs.oracle.com/javase/tutorial/collections/algorithms/index.html
相关文章推荐
- 深入理解Java国际化
- Java集合框架官方教程(3):SortedSet/SortedMap接口
- 高吞吐低延迟Java应用的垃圾回收优化
- Java集合框架总结
- 深入理解Java注解(1):基础详解
- 成为Java GC专家(3):如何优化Java垃圾回收机制
- Java并发与多线程教程(3)
- 深入理解Java类加载器(1):Java类加载原理解析
- 浅析Java虚拟机结构与机制
- 深入理解Java注解(2):高级应用
- Java集合框架官方教程(5):集合类的同步包装器/不可变包装器
- Java集合框架官方教程(6):算法
- 深入理解Java国际化
- Java集合框架总结
- 深入理解Java注解(1):基础详解
- 成为Java GC专家(3):如何优化Java垃圾回收机制
- Java集合框架官方教程(5):集合类的同步包装器/不可变包装器
- 深入理解JavaBean(1):JavaBean的内省与BeanUtils库
- 深入理解JavaBean(1):JavaBean的内省与BeanUtils库
- Mockito:一个强大的用于Java开发的模拟测试框架