java.util.Collections

java的java.util.Collections
类是一个非常有用的工具类。

空集合
Collections.EMPTY_LIST,Collections.emptyList()——返回只读 的空LIST 集合
Collections.EMPTY_MAP,Collections.emptyMap()——返回只读 的空MAP集合
Collections.EMPTY_SET,Collections.emptySet()返回只读 的空SET集合
所谓的空集合指的是没有元素在这些集合中,特别需要主要的是返回的集合都是只读的。以下代码会抛出UnsupportedOperationException
异常。
-------------------------------------------------------
public static void main(String[] args) ... {
         List children
= Collections.EMPTY_LIST;
         children.add(
new HashMap());
}


-------------------------------------------------------
Collections.emptyList(),Collections.emptySet(),Collections.emptyMap()由于支持泛型使用起来会更方便,例如
--------------------------------------------------------------
List<String> s = Collections.emptyList();
---------------------------------------------------------------

单元素集合
Collections中的单元素集合指的是集合只有一个元素而且集合只读。
Collections.singletonList——用来生成只读 的单一元素的List
Collections.singletonMap——用来生成只读 的单Key和Value组成的Map
Collections.singleton——用来生成只读 的单一元素的Set


只读集合
Collections提供了生成几种生成只读集合的方法unmodifiableCollection,unmodifiableList,unmodifiableMap,unmodifiableSet,
unmodifiableSortedMap,unmodifiableSortedSet。这些集合一旦初始化以后就不能修改,任何修改这些集合的方法都会抛出
UnsupportedOperationException异常。


Checked集合(Checked Collections)
Checked集合具有检查插入集合元素类型的特性,例如当我们设定checkedList中元素的类型是String的时候,如果插入起来类型的元素就会抛出
ClassCastExceptions异常,Java5中提供泛型的功能,泛型功能能够在代码编译阶段就约束集合中元素的类型,但有些时候声明的集合可能是raw集合,
编译阶段的类型约束就不起作用了,这个时候Checked集合就能起到约束集合中元素类型的作用。
Collections中提供了以下生成Checked集合的方法
checkedCollection,checkedList,checkedMap,checkedSet,checkedSortedMap,checkedSortedSet


同步集合(Synchronized Collections)
Collections的synchronizedXxxxx系列方法顾名思义会返回同步化集合类(SynchronizedMap,
SynchronizedList等等)。这些集合类内部实现都是通过一个mutex(互斥体)来实现对这些集合操作的同步化。



Enumeration接口
从JDK1.0开始Java就提供了Enumeration 接口。Collections中list和enumeration和Enumeration接口相关。
list(Enumeration<T> e) 方法用于有Enumeration接口中产生一个List
———————————————————————————————————————
public void demonstrateEnumerationToList()
{
log("===== Demonstrate Collections.list(Enumeration) =====", System.out);
final Enumeration properties = System.getProperties().propertyNames();
final List propertiesList = Collections.list(properties);
log(propertiesList.toString(), System.out);
}
————————————————————————————————————————
enumeration(Collection<T> c) 方法用于基于Collection返回Enumeration。
——————————————————————————————————————
public void demonstrateCollectionToEnumeration()
{
log("===== Demonstrate Collections.enumeration(Collection) =====", System.out);
final Enumeration books = Collections.enumeration(this.favoriteBooks);
while (books.hasMoreElements())
{
log(books.nextElement().toString(), System.out);
}
————————————————————————————————————————————
}



查找替换
fill——使用指定元素替换指定列表中的所有元素。
frequency——返回指定 collection 中等于指定对象的元素数。
indexOfSubList—— 返回指定源列表中第一次出现指定目标列表的起始位置,如果没有出现这样的列表,则返回 -1。
lastIndexOfSubList——返回指定源列表中最后一次出现指定目标列表的起始位置,如果没有出现这样的列表,则返回-1。
max—— 根据元素的自然顺序,返回给定 collection 的最大元素。
min——根据元素的自然顺序 返回给定 collection 的最小元素。
replaceAll——使用另一个值替换列表中出现的所有某一指定值。

集合排序
Collections还提供了集中对集合进行排序的方法。
reverse——对List中的元素倒序排列
shuffle——对List中的元素随即排列,这个方法让我想到了Apple的iPod Shuffle
sort——对List中的元素排序
swap——交换List中某两个指定下标位元素在集合中的位置。
rotate——循环移动。循环移动这个方法让人比较难以理解,下面的例子就会让你一下子就理解这个方法的含义。
——————————————————————————————————————————————————————————————————————
假设 list 包含 [t, a, n, k, s] 。在调用 Collections.rotate(list, 1) (或 Collections.rotate(list, -4) )之后,list 将包含 [s, t, a, n, k]
——————————————————————————————————————————————————————————————————————

其他方法
binarySearch——使用二进制搜索算法来搜索指定列表,以获得指定对象。
addAll——将所有指定元素添加到指定 collection 中。
copy——将所有元素从一个列表复制到另一个列表。
disjoint——如果两个指定 collection 中没有相同的元素,则返回 true。
nCopies——返回由指定对象的 n 个副本组成的不可变列表。

参考资料
http://java.sun.com/javase/6/docs/api/java/util/Collections.html
http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/util/Collections.html
java.util.Collections类包的学习
The Java Collections Class
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值