Java中的集合

来源:互联网 发布:分布式更新数据 编辑:程序博客网 时间:2024/06/05 05:40

1.javaz集合主要由两个接口派生:Collection和Map。其Collection继承树如下:


Map的继承树为:


其中,粗线圈出的为接口,深黑色背景的为常用的实现类。

2.操作集合的工具类

import java.util.*;class CollectionToolTest {public static void main(String[] args) {ArrayList nums=new ArrayList();nums.add(2);nums.add(-3);nums.add(5);nums.add(0);//排序操作//依顺序进行输出System.out.println(nums);//将元素翻转Collections.reverse(nums);System.out.println(nums);//从小到大进行排序Collections.sort(nums);System.out.println(nums);//随机排序Collections.shuffle(nums);System.out.println(nums);//查找替换操作//返回最大、小值System.out.println(Collections.max(nums));System.out.println(Collections.min(nums));//将其中的0改为1Collections.replaceAll(nums,0,1);System.out.println(nums);//计算-3出现的次数System.out.println(Collections.frequency(nums,-3));//排序并使用二分查找来查找2的索引号Collections.sort(nums);System.out.println(Collections.binarySearch(nums,2));//同步控制Collection c=Collections.synchronizedCollection(new ArrayList());List list=Collections.synchronizedList(new ArrayList());Set s=Collections.synchronizedSet(new HashSet());//设置不可变的集合List unmodList=Collections.emptyList();//创建一个只有一个元素的不可变Set集合Set unmodSet=Collections.singleton("123");        //使用普通的Map对象创建其不可变的版本Map scores=new HashMap();scores.put("zhang1",123);scores.put("zhang2",234);Map unmodMap=Collections.unmodifiableMap(scores);}}

0 0
原创粉丝点击