public class CollectionsDemo {
public static void main(String[] args) {
/*
* Collections演示。
* 1,用于操作集合的工具类。
* 2,提供了很多的静态方法。
*
* 比如对list集合排序,二分查找,位置置换。
* 对排序的元素进行顺序的逆转。reverseOrder
* 还可以获取集合的最大值和最小值。
* 最牛叉的是将非同步的集合转成同步的集合 synchronizedXXX
*
*/
sortDemo();
}
//1,演示Collections中的排序。
public static void sortDemo(){
List list = new ArrayList();
list.add("abc");
list.add("aaaaaaa");
list.add("hahah");
list.add("cba");
System.out.println(list);
//排序方法。
// Collections.sort(list,Collections.reverseOrder());
// Collections.sort(list,Collections.reverseOrder(new ComparatorByLength()));
// String max = Collections.max(list,new ComparatorByLength());
String max = myMax(list);
System.out.println("max="+max);
System.out.println(list);
}
//模拟一个max方法。获取集合中的最大值。
public static > T myMax(Collection extends T> coll){
Iterator extends T> it = coll.iterator();
//定义一个记录较大值的变量。
T max = it.next();
while(it.hasNext()){
T temp = it.next();
if(temp.compareTo(max)>0){
// if(comp.compare(temp, max)>0){
max = temp;
}
}
return max;
}
}
如果collection使用的时候是多线程访问的 用Collections的加锁方法 对Collection加锁
Modifier and Type | Field and Description |
---|---|
static List |
EMPTY_LIST
The empty list (immutable).
|
static Map |
EMPTY_MAP
The empty map (immutable).
|
static Set |
EMPTY_SET
The empty set (immutable).
|
Modifier and Type | Method and Description |
---|---|
static |
addAll(Collection super T> c, T... elements)
Adds all of the specified elements to the specified collection.
|
static |
asLifoQueue(Deque
Returns a view of a
Deque as a Last-in-first-out (Lifo)
Queue .
|
static |
binarySearch(List extends Comparable super T>> list, T key)
Searches the specified list for the specified object using the binary search algorithm.
|
static |
binarySearch(List extends T> list, T key, Comparator super T> c)
Searches the specified list for the specified object using the binary search algorithm.
|
static |
checkedCollection(Collection
Returns a dynamically typesafe view of the specified collection.
|
static |
checkedList(List
Returns a dynamically typesafe view of the specified list.
|
static |
checkedMap(Map
Returns a dynamically typesafe view of the specified map.
|
static |
checkedSet(Set
Returns a dynamically typesafe view of the specified set.
|
static |
checkedSortedMap(SortedMap
Returns a dynamically typesafe view of the specified sorted map.
|
static |
checkedSortedSet(SortedSet
Returns a dynamically typesafe view of the specified sorted set.
|
static |
copy(List super T> dest, List extends T> src)
Copies all of the elements from one list into another.
|
static boolean |
disjoint(Collection> c1, Collection> c2)
Returns
true if the two specified collections have no elements in common.
|
static |
emptyEnumeration()
Returns an enumeration that has no elements.
|
static |
emptyIterator()
Returns an iterator that has no elements.
|
static |
emptyList()
Returns the empty list (immutable).
|
static |
emptyListIterator()
Returns a list iterator that has no elements.
|
static |
emptyMap()
Returns the empty map (immutable).
|
static |
emptySet()
Returns the empty set (immutable).
|
static |
enumeration(Collection
Returns an enumeration over the specified collection.
|
static |
fill(List super T> list, T obj)
Replaces all of the elements of the specified list with the specified element.
|
static int |
frequency(Collection> c, Object o)
Returns the number of elements in the specified collection equal to the specified object.
|
static int |
indexOfSubList(List> source, List> target)
Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.
|
static int |
lastIndexOfSubList(List> source, List> target)
Returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.
|
static |
list(Enumeration
Returns an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration.
|
static |
max(Collection extends T> coll)
Returns the maximum element of the given collection, according to the
natural ordering of its elements.
|
static |
max(Collection extends T> coll, Comparator super T> comp)
Returns the maximum element of the given collection, according to the order induced by the specified comparator.
|
static |
min(Collection extends T> coll)
Returns the minimum element of the given collection, according to the
natural ordering of its elements.
|
static |
min(Collection extends T> coll, Comparator super T> comp)
Returns the minimum element of the given collection, according to the order induced by the specified comparator.
|
static |
nCopies(int n, T o)
Returns an immutable list consisting of
n copies of the specified object.
|
static |
newSetFromMap(Map
Returns a set backed by the specified map.
|
static |
replaceAll(List
Replaces all occurrences of one specified value in a list with another.
|
static void |
reverse(List> list)
Reverses the order of the elements in the specified list.
|
static |
reverseOrder()
Returns a comparator that imposes the reverse of the
natural ordering on a collection of objects that implement the
Comparable interface.
|
static |
reverseOrder(Comparator
Returns a comparator that imposes the reverse ordering of the specified comparator.
|
static void |
rotate(List> list, int distance)
Rotates the elements in the specified list by the specified distance.
|
static void |
shuffle(List> list)
Randomly permutes the specified list using a default source of randomness.
|
static void |
shuffle(List> list, Random rnd)
Randomly permute the specified list using the specified source of randomness.
|
static |
singleton(T o)
Returns an immutable set containing only the specified object.
|
static |
singletonList(T o)
Returns an immutable list containing only the specified object.
|
static |
singletonMap(K key, V value)
Returns an immutable map, mapping only the specified key to the specified value.
|
static |
sort(List
Sorts the specified list into ascending order, according to the natural ordering of its elements.
|
static |
sort(List
Sorts the specified list according to the order induced by the specified comparator.
|
static void |
swap(List> list, int i, int j)
Swaps the elements at the specified positions in the specified list.
|
static |
synchronizedCollection(Collection
Returns a synchronized (thread-safe) collection backed by the specified collection.
|
static |
synchronizedList(List
Returns a synchronized (thread-safe) list backed by the specified list.
|
static |
synchronizedMap(Map
Returns a synchronized (thread-safe) map backed by the specified map.
|
static |
synchronizedSet(Set
Returns a synchronized (thread-safe) set backed by the specified set.
|
static |
synchronizedSortedMap(SortedMap
Returns a synchronized (thread-safe) sorted map backed by the specified sorted map.
|
static |
synchronizedSortedSet(SortedSet
Returns a synchronized (thread-safe) sorted set backed by the specified sorted set.
|
static |
unmodifiableCollection(Collection extends T> c)
Returns an unmodifiable view of the specified collection.
|
static |
unmodifiableList(List extends T> list)
Returns an unmodifiable view of the specified list.
|
static |
unmodifiableMap(Map extends K,? extends V> m)
Returns an unmodifiable view of the specified map.
|
static |
unmodifiableSet(Set extends T> s)
Returns an unmodifiable view of the specified set.
|
static |
unmodifiableSortedMap(SortedMap
Returns an unmodifiable view of the specified sorted map.
|
static |
unmodifiableSortedSet(SortedSet
Returns an unmodifiable view of the specified sorted set.
|