Java Collection 接口描述(一)

物件容器(container)

 

Collection接口分装了不同类型的容器,可以看上图所示。

物件容器可以帮你持有对象。在Java中分作两大类: CollectionMap。前者可持有各自独立的物件,后者持有承兑的key-value物件。

注意所有的核心collection接口是通用的,如何申明一个Collection接口:

Iterator<LinkedList> iterator=collections.iterator();

Collection类包括了List类与Set类,List以放置物件至容器中的顺序来排列物件,Set类不接受重复的物件,并有自己的一套排序规则。

ArrayList:

动态数组,Array的复杂版本,好处在于动态的增加和减少元素;实现了ICollectionIList接口;灵活的设置数组的大小。

ArrayList list1=new ArrayList();

            for(int i=0;i<10;i++){

              list1.add(i);

            }

            list1.remove(5);

            list1.add(10);

            for(int i=0;i<list1.size();i++){

              System.out.println(list1.get(i));

            }

Output:

0

1

2

3

4

6

7

8

9

10

 

LinkedList:

HashSet:

SortedSet:

LinkedHashSet:

TreeSet:

EnumSet

Map类:在将物件存入Map类时,需要配合一把key,要取回物件时就是根据这把keyMap中的key是唯一的,Map拥有自己的排序机制。

HashMap

TreeMap

EnumMap

LinkedHashMap:

 

算法

1. Sorting:对容器内的元素进行排序

List<String> list = Arrays.asList(args);

            java.util.Collections.sort(list);

            System.out.println(list);

Argument: I like hangzhou forever

Output: [Hangzhou, I, forever, like]

2. Iterator循环

HashSet collections = new HashSet();

            String str1="red";

            String str2="blue";

            String str3="yellow";

            String str4="White";

            collections.add(str1);

            collections.add(str2);

            collections.add(str3);

            collections.add(str4);

            Iterator iterator=collections.iterator();

            while(iterator.hasNext()){

              System.out.println(iterator.next());

            }

Output: White

red

blue

yellow

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值