java初识集合(list,set,map)

java的集合有三类:list,set,map。list和set继承了collection接口。区别(list可以添加重复对象,且按照索引位置排序;set没有这两种特点)。

map是通过key操作里面的value,操作的是成对的对象。put放入对象,get取出对象。

另外:colletion没有随机访问的get()方法,因为collection还包括set,而set有自己的内部顺序。所以,要检查collection元素,必须使用iterator对象。

1、list中有ArrayList(类似数组形式进行存储) 和LinkedList(链表形式)

2、set具体实现有HashSet、TreeSet和LinkedHashset

 1 import java.util.*;
 2 
 3 public class basic {
 4     public static void main(String[] args){
 5         Collection<String> collection = new HashSet<String>();//HashSet向上转型,并使用泛型
 6         collection.add("aaa");
 7         collection.add("bbb");
 8         collection.add("ccc");
 9         Collections.addAll(collection,"E","ffff","aaa");//利用java.util包中的Collection的addALL方法,可以这样的
10                                                     //添加数据,但是用collection.add这样使用会报错
11         Iterator<String> it = collection.iterator();//使用iterator方法,该方法返回一个在容器各元素之间移动的Iterator
12         while(it.hasNext())
13         {
14             System.out.print(it.next()+" ");
15         }
16         System.out.print("\n"+collection);
17     }
18 }

 

posted @ 2016-01-30 16:48  白菜hxj  阅读(256)  评论(0编辑  收藏  举报