java package util_Java_util_package(转)

Java_util_package(转)

发布时间:2020-08-12 03:04:30

来源:ITPUB博客

阅读:89

作者:ba

Java_util_package(转)[@more@]Collection

RetainAll :保留两个Collection的交集。注意,如果该Collection是由Arrays.asList转换而来,那么这个方法会失败。因为转换来的List接口不支持这个方法

Samples:

public static void collectionTest()

{

Collection c1 = new ArrayList();

Collection c2 = new ArrayList();

c1.add("aaa");

c1.add("bbb");

c1.add("ccc");

c2.add("ddd");

c2.add("ccc");

c2.add("eee");

boolean isRetainSucceed = false;

isRetainSucceed = c2.retainAll(c1);

System.out.println("isRetainSucceed = " + isRetainSucceed);

System.out.println("********** print collection c2 values ");

for (Iterator iter = c2.iterator(); iter.hasNext();)

{

String s = (String) iter.next();

System.out.println("s = " + s);

}

}

Enumeration

太简单,参考文档

Comparator

未使用过

EventListener

空接口

Iterator

和Enumeration 的不同点:

1. 允许遍历Collection时删除对象

2. 方法名字可读性更好

List

实现的四个类:AbstractList, ArrayList, LinkedList, Vector

List 特点:

1. 允许重复元素,允许null元素

2. 推荐用Iterator遍历,而不是用索引

addAll : 加入Collection

containsAll :是否包含Collection

retainAll : 保留和Collection的交集

subList : 返回指定索引区间的子List

ListIterator :

1. 提供元素的双向遍历,而不是单向

2. 遍历时可改变存储的元素

3. 可动态插入元素,插入的元素在当前操作元素的上一个位置

Samples:

public static void ListTest(){

System.out.println("**********ListTest begin:");

List list = new ArrayList();

list.add("aaa");

list.add("bbb");

list.add("ccc");

ListIterator iter = list.listIterator();

System.out.println("**************** Iterating List forward :");

while(iter.hasNext()){

String s = (String)iter.next();

System.out.println("**********element = " + s);

}

System.out.println("**************** Iterating List backward :");

while(iter.hasPrevious()){

String s = (String)iter.previous();

System.out.println("**********element = " + s);

}

System.out.println("**************** Add element into List :");

while(iter.hasNext()){

int i = iter.nextIndex();

if (i==2)

iter.add("ddd");

String s = (String)iter.next();

System.out.println("**********element = " + s);

}

System.out.println("**************** Iterating List backward after add element:");

while(iter.hasPrevious()){

String s = (String)iter.previous();

System.out.println("**********element = " + s);

}

System.out.println("**********ListTest end:");

}

Map

KeySet :

返回Set对象,然后可以遍历这个Set。其中的每个元素都是Map.Entry对象

Map.Entry.setValue :

在遍历Entry对象时,可以改变该Key对应的Value值

Samples:

public static void mapEntrySetTest(){

System.out.println("**********mapEntrySetTest begin:");

Map map = new HashMap();

map.put("first","aaa");

map.put("second","bbb");

map.put("third","ccc");

map.put("fourth","ddd");

Set set = map.entrySet();

Map.Entry entry = null;

System.out.println("********** print values in map :");

for(Iterator iter = set.iterator();iter.hasNext();){

entry = (Map.Entry)iter.next();

System.out.println("Key is :" + entry.getKey() + " and Value is :" + entry.getValue());

entry.setValue((String)entry.getValue() + "_setValueTest");

}

System.out.println("********** After set value ,iterating values in map :");

for(Iterator iter = set.iterator();iter.hasNext();){

entry = (Map.Entry)iter.next();

System.out.println("Key is :" + entry.getKey() + " and Value is :" + entry.getValue());

}

System.out.println("**********mapEntrySetTest end:");

}

Observable and Observer

暂未使用

RandomAccess

空接口。实现这个接口的List实现品,表示他们支持高速的随机访问元素。如果实现这个接口,理论上

for (int i=0, n=list.size(); i < n; i++)

list.get(i);

比下面代码要快:

for (Iterator i=list.iterator(); i.hasNext(); )

i.next();

Set :

没什么特别的,和Collection差不多

作者Blog:http://blog.csdn.net/WalkingWithJava/

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值