javaSE-集合工具类-Collections

本文详细介绍了Java中Collections工具类的功能和使用方法,包括对集合进行排序、查找、位置置换等操作,并展示了如何获取集合的最大值和最小值。此外,还介绍了如何通过Collections将非线程安全的集合转换为线程安全的集合。
public class CollectionsDemo {

	public static void main(String[] args) {

		
		/*
		 * Collections演示。
		 * 1,用于操作集合的工具类。
		 * 2,提供了很多的静态方法。 
		 * 
		 * 比如对list集合排序,二分查找,位置置换。
		 * 对排序的元素进行顺序的逆转。reverseOrder
		 * 还可以获取集合的最大值和最小值。
		 * 最牛叉的是将非同步的集合转成同步的集合 synchronizedXXX
		 * 
		 */
		sortDemo();
		
	}
	//1,演示Collections中的排序。
	public static void sortDemo(){
		
		List<String> list = new ArrayList<String>();
		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 extends Object & Comparable<? super T>> 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加锁

Field Summary

Fields 
Modifier and TypeField and Description
static ListEMPTY_LIST
The empty list (immutable).
static MapEMPTY_MAP
The empty map (immutable).
static SetEMPTY_SET
The empty set (immutable).

Method Summary

Methods 
Modifier and TypeMethod and Description
static <T> booleanaddAll(Collection<? super T> c, T... elements)
Adds all of the specified elements to the specified collection.
static <T> Queue<T>asLifoQueue(Deque<T> deque)
Returns a view of a Deque as a Last-in-first-out (Lifo) Queue.
static <T> intbinarySearch(List<? extends Comparable<? super T>> list, T key)
Searches the specified list for the specified object using the binary search algorithm.
static <T> intbinarySearch(List<? extends T> list, T key, Comparator<? super T> c)
Searches the specified list for the specified object using the binary search algorithm.
static <E> Collection<E>checkedCollection(Collection<E> c, Class<E> type)
Returns a dynamically typesafe view of the specified collection.
static <E> List<E>checkedList(List<E> list, Class<E> type)
Returns a dynamically typesafe view of the specified list.
static <K,V> Map<K,V>checkedMap(Map<K,V> m, Class<K> keyType, Class<V> valueType)
Returns a dynamically typesafe view of the specified map.
static <E> Set<E>checkedSet(Set<E> s, Class<E> type)
Returns a dynamically typesafe view of the specified set.
static <K,V> SortedMap<K,V>checkedSortedMap(SortedMap<K,V> m, Class<K> keyType, Class<V> valueType)
Returns a dynamically typesafe view of the specified sorted map.
static <E> SortedSet<E>checkedSortedSet(SortedSet<E> s, Class<E> type)
Returns a dynamically typesafe view of the specified sorted set.
static <T> voidcopy(List<? super T> dest, List<? extends T> src)
Copies all of the elements from one list into another.
static booleandisjoint(Collection<?> c1, Collection<?> c2)
Returns true if the two specified collections have no elements in common.
static <T> Enumeration<T>emptyEnumeration()
Returns an enumeration that has no elements.
static <T> Iterator<T>emptyIterator()
Returns an iterator that has no elements.
static <T> List<T>emptyList()
Returns the empty list (immutable).
static <T> ListIterator<T>emptyListIterator()
Returns a list iterator that has no elements.
static <K,V> Map<K,V>emptyMap()
Returns the empty map (immutable).
static <T> Set<T>emptySet()
Returns the empty set (immutable).
static <T> Enumeration<T>enumeration(Collection<T> c)
Returns an enumeration over the specified collection.
static <T> voidfill(List<? super T> list, T obj)
Replaces all of the elements of the specified list with the specified element.
static intfrequency(Collection<?> c, Object o)
Returns the number of elements in the specified collection equal to the specified object.
static intindexOfSubList(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 intlastIndexOfSubList(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 <T> ArrayList<T>list(Enumeration<T> e)
Returns an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration.
static <T extends Object & Comparable<? super T>> 
T
max(Collection<? extends T> coll)
Returns the maximum element of the given collection, according to the natural ordering of its elements.
static <T> Tmax(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 <T extends Object & Comparable<? super T>> 
T
min(Collection<? extends T> coll)
Returns the minimum element of the given collection, according to the natural ordering of its elements.
static <T> Tmin(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 <T> List<T>nCopies(int n, T o)
Returns an immutable list consisting of n copies of the specified object.
static <E> Set<E>newSetFromMap(Map<E,Boolean> map)
Returns a set backed by the specified map.
static <T> booleanreplaceAll(List<T> list, T oldVal, T newVal)
Replaces all occurrences of one specified value in a list with another.
static voidreverse(List<?> list)
Reverses the order of the elements in the specified list.
static <T> Comparator<T>reverseOrder()
Returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.
static <T> Comparator<T>reverseOrder(Comparator<T> cmp)
Returns a comparator that imposes the reverse ordering of the specified comparator.
static voidrotate(List<?> list, int distance)
Rotates the elements in the specified list by the specified distance.
static voidshuffle(List<?> list)
Randomly permutes the specified list using a default source of randomness.
static voidshuffle(List<?> list, Random rnd)
Randomly permute the specified list using the specified source of randomness.
static <T> Set<T>singleton(T o)
Returns an immutable set containing only the specified object.
static <T> List<T>singletonList(T o)
Returns an immutable list containing only the specified object.
static <K,V> Map<K,V>singletonMap(K key, V value)
Returns an immutable map, mapping only the specified key to the specified value.
static <T extends Comparable<? super T>> 
void
sort(List<T> list)
Sorts the specified list into ascending order, according to the natural ordering of its elements.
static <T> voidsort(List<T> list, Comparator<? super T> c)
Sorts the specified list according to the order induced by the specified comparator.
static voidswap(List<?> list, int i, int j)
Swaps the elements at the specified positions in the specified list.
static <T> Collection<T>synchronizedCollection(Collection<T> c)
Returns a synchronized (thread-safe) collection backed by the specified collection.
static <T> List<T>synchronizedList(List<T> list)
Returns a synchronized (thread-safe) list backed by the specified list.
static <K,V> Map<K,V>synchronizedMap(Map<K,V> m)
Returns a synchronized (thread-safe) map backed by the specified map.
static <T> Set<T>synchronizedSet(Set<T> s)
Returns a synchronized (thread-safe) set backed by the specified set.
static <K,V> SortedMap<K,V>synchronizedSortedMap(SortedMap<K,V> m)
Returns a synchronized (thread-safe) sorted map backed by the specified sorted map.
static <T> SortedSet<T>synchronizedSortedSet(SortedSet<T> s)
Returns a synchronized (thread-safe) sorted set backed by the specified sorted set.
static <T> Collection<T>unmodifiableCollection(Collection<? extends T> c)
Returns an unmodifiable view of the specified collection.
static <T> List<T>unmodifiableList(List<? extends T> list)
Returns an unmodifiable view of the specified list.
static <K,V> Map<K,V>unmodifiableMap(Map<? extends K,? extends V> m)
Returns an unmodifiable view of the specified map.
static <T> Set<T>unmodifiableSet(Set<? extends T> s)
Returns an unmodifiable view of the specified set.
static <K,V> SortedMap<K,V>unmodifiableSortedMap(SortedMap<K,? extends V> m)
Returns an unmodifiable view of the specified sorted map.
static <T> SortedSet<T>unmodifiableSortedSet(SortedSet<T> s)
Returns an unmodifiable view of the specified sorted set.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值