Java集合算法大全

集合框架定义了可以被应用到集合和映射的几种算法。

这些算法被定义为集合类中的静态方法。有几个方法可以抛出ClassCastException,当它试图比较不兼容的类型时,或者一个UnsupportedOperationException,当它试图修改一个不可修改的集合发生。

在集合框架的算法定义的方法总结如下表:

SN 方法及描述
1 static int binarySearch(List list, Object value, Comparator c)
Searches for value in list ordered according to c. Returns the position of value in list, or -1 if value is not found
2 static int binarySearch(List list, Object value)
Searches for value in list. The list must be sorted. Returns the position of value in list, or -1 if value is not found.
3 static void copy(List list1, List list2)
Copies the elements of list2 to list1.
4 static Enumeration enumeration(Collection c)
Returns an enumeration over c.
5 static void fill(List list, Object obj)
Assigns obj to each element of list.
6 static int indexOfSubList(List list, List subList)
Searches list for the first occurrence of subList. Returns the index of the first match, or .1 if no match is found.
7 static int lastIndexOfSubList(List list, List subList)
Searches list for the last occurrence of subList. Returns the index of the last match, or .1 if no match is found.
8 static ArrayList list(Enumeration enum)
Returns an ArrayList that contains the elements of enum.
9 static Object max(Collection c, Comparator comp)
Returns the maximum element in c as determined by comp.
10 static Object max(Collection c)
Returns the maximum element in c as determined by natural ordering. The collection need not be sorted.
11 static Object min(Collection c, Comparator comp)
Returns the minimum element in c as determined by comp. The collection need not be sorted.
12 static Object min(Collection c)
Returns the minimum element in c as determined by natural ordering.
13 static List nCopies(int num, Object obj)
Returns num copies of obj contained in an immutable list. num must be greater than or equal to zero.
14 static boolean replaceAll(List list, Object old, Object new)
Replaces all occurrences of old with new in list. Returns true if at least one replacement occurred. Returns false, otherwise.
15 static void reverse(List list)
Reverses the sequence in list.
16 static Comparator reverseOrder( )
Returns a reverse comparator
17 static void rotate(List list, int n)
Rotates list by n places to the right. To rotate left, use a negative value for n.
18 static void shuffle(List list, Random r)
Shuffles (i.e., randomizes) the elements in list by using r as a source of random numbers.
19 static void shuffle(List list)
Shuffles (i.e., randomizes) the elements in list.
20 static Set singleton(Object obj)
Returns obj as an immutable set. This is an easy way to convert a single object into a set.
21 static List singletonList(Object obj)
Returns obj as an immutable list. This is an easy way to convert a single object into a list.
22 static Map singletonMap(Object k, Object v)
Returns the key/value pair k/v as an immutable map. This is an easy way to convert a single key/value pair into a map.
23 static void sort(List list, Comparator comp)
Sorts the elements of list as determined by comp.
24 static void sort(List list)
Sorts the elements of list as determined by their natural ordering.
25 static void swap(List list, int idx1, int idx2)
Exchanges the elements in list at the indices specified by idx1 and idx2.
26 static Collection synchronizedCollection(Collection c)
Returns a thread-safe collection backed by c.
27 static List synchronizedList(List list)
Returns a thread-safe list backed by list.
28 static Map synchronizedMap(Map m)
Returns a thread-safe map backed by m.
29 static Set synchronizedSet(Set s)
Returns a thread-safe set backed by s.
30 static SortedMap synchronizedSortedMap(SortedMap sm)
Returns a thread-safe sorted set backed by sm.
31 static SortedSet synchronizedSortedSet(SortedSet ss)
Returns a thread-safe set backed by ss.
32 static Collection unmodifiableCollection(Collection c)
Returns an unmodifiable collection backed by c.
33 static List unmodifiableList(List list)
Returns an unmodifiable list backed by list.
34 static Map unmodifiableMap(Map m)
Returns an unmodifiable map backed by m.
35 static Set unmodifiableSet(Set s)
Returns an unmodifiable set backed by s.
36 static SortedMap unmodifiableSortedMap(SortedMap sm)
Returns an unmodifiable sorted map backed by sm.
37 static SortedSet unmodifiableSortedSet(SortedSet ss)
Returns an unmodifiable sorted set backed by ss.

例子:

以下是示例,其中演示各种算法。

List sorted in reverse: 20 8 -8 -20
List shuffled: 20 -20 8 -8
Minimum: -20
Maximum: 20
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值