noaman_wgs

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

集合

1 集合用来存储多种类型数据的数量不等的对象,与数组一旦创建长度就须固定的限制不一样。
2 集合主要有
|-----Collection接口:
|-----List接口:存储有序可重复元素,主要实现类:ArrayList、LinkedList、Vector
|-----Set接口:存储无序不可重复元素,主要实现类:HashSet、LinkedHashSet、TreeSet
|-----Map接口:用来存储具有一一对应关系的数据,主要实现类:HashMap、LinkedHashMap、TreeMap、Hashtable(Properties)

Collection接口
1 Collection常用方法:

  1 package org.exe.集合;
  2 //01
  3 public class TestCollection1 {
  4 
  5     @Test
  6     public void test1(){
  7         //创建对象
  8         Collection col1=new ArrayList();
  9         //1 add(Object obj)添加集合对象
 10         System.out.println(col1.size());//未创建之前长度为0
 11         col1.add(123);
 12         col1.add("I LOVE YOU");
 13         col1.add(new Date());        
 14         //2 size()  返回集合长度
 15         System.out.println("col1集合长度为"+col1.size());//创建后长度为3        
 16         //3 addAll() 将集合1中所有元素添加到当前集合中
 17         Collection col2=new ArrayList();
 18         col2.addAll(col1);
 19         System.out.println("添加了col1集合后的col2集合长度应该是"+col2.size());        
 20         //4 isEmpty();
 21         System.out.println("col2中元素是否为空true/false"+"  "+col2.isEmpty());
 22         //5 clear();清空集合元素
 23         System.out.println("-------------------col2中元素开始清除中----------");
 24         col2.clear();
 25         System.out.println("col2中元素是否为空true/false"+"  "+col2.isEmpty());                
 26         System.out.println("------------------------------------------------");
 27         //打印集合名,可打印出集合中元素,说明ArrayList中syso重写了
 28         System.out.println("col1中元素为  "+col1);
 29         System.out.println("col2中元素为  "+col2);                
 30     }
 31     @Test
 32     public void test2(){
 33         //创建对象
 34                 Collection col3=new ArrayList();
 35                 col3.add(1126);
 36                 col3.add("今天是11月26日感恩节祝大家节日快乐");
 37                 col3.add(new Date());
 38                 col3.add("AAAA");                
 39                 System.out.println(col3);
 40                 //6 contains(Object obj):判断是否存在obj元素   返回boolean值
 41                 //判断依据:根据元素所在类的equals()方法进行判断(一般是判断地址值,Strin等都重写了)
 42                 //自定义类要重写equals()方法
 43                 System.out.println("是否存在1126  "+col3.contains(1126));
 44                 System.out.println("是否存在11  "+col3.contains(11));
 45                 System.out.println("是否存在AA  "+col3.contains("AA"));
 46                 System.out.println();
 47                             
 48                 //6.1 可添加自定义类
 49                 Person p=new Person("王根深",24);
 50                 col3.add(p);
 51                 //看col3中是否存在p
 52                 boolean b1=col3.contains(p);
 53                 System.out.println("col3中是否存在p  "+b1);//true
 54                 
 55                 //若改成匿名类
 56                 col3.add(new Person("王根深",24));
 57                 //看col3中是否存在匿名类
 58                 boolean b2=col3.contains(new Person("王根深",24));
 59                 System.out.println("col3中是否存在匿名类  "+b2);//false  当在Person重写HashCode后就变味true
 60                 
 61               //7 containAll(Collection e)  e是否为前面子集  返回boolean值
 62                 Collection col4=new ArrayList();
 63                 col4.add(1126);
 64                 col4.add("今天是11月26日感恩节祝大家节日快乐");
 65                 col4.add("AAAA");
 66                 boolean b3=col3.containsAll(col4);
 67                 System.out.println("col3中是否存在col4  "+b3);
 68                 
 69               //8 retainAll(Collection e)  返回  boolean值  求交集   范围大的将会只剩下交集部分
 70                 col3.retainAll(col4);
 71                 System.out.println(col3);                    
 72     }
 73     @Test
 74     public void test3(){
 75         Collection col5=new ArrayList();
 76         col5.add(1);
 77         col5.add(2);
 78         col5.add(3);
 79         col5.add(4);
 80         col5.add(55);
 81         
 82         Collection col6=new ArrayList();
 83         col6.add(1);
 84         col6.add(2);
 85         
 86         //9 remove(Object obj)   删除集合中元素,若删除成功,返回true
 87         col5.remove(33);
 88         col5.remove(55);
 89         System.out.println(col5);
 90         System.out.println();
 91         //10 removeAll(Collection e) 差集  返回boolean
 92         col5.removeAll(col6);
 93         System.out.println(col5);
 94         
 95         //11 equals(object obj)判断两个集合是否相等 
 96     }
 97     @Test
 98     public void test4(){
 99         Collection col1=new ArrayList();
100         col1.add(1);
101         col1.add(2);
102         
103         Collection col2=new ArrayList();
104         col2.add(1);
105         col2.add(2);
106         
107         Collection col3=new ArrayList();
108         col3.add(111);
109         col3.add(222);
110         
111         //11 equals(object obj)判断两个集合所有元素是否相等 
112         boolean b1=col1.equals(col2);
113         System.out.println("col1和col2是否相等true/false  "+b1);
114         b1=col1.equals(col3);
115         System.out.println("col1和col3是否相等true/false  "+b1);
116         
117         //12 hashcode()
118         System.out.println("col1.hashCode()是"+col1.hashCode());
119         System.out.println("col2.hashCode()是"+col2.hashCode());
120         System.out.println("col3.hashCode()是"+col3.hashCode());
121         //13 toArray():将集合转化为数组
122         Object[] obj=col1.toArray();
123         for(int i=0;i<obj.length;i++){
124             System.out.println(obj[i]);
125         }
126         //数组化成集合
127         Collection collection=Arrays.asList(1,2,3);
128         //14 iterator():返回一个Iterator接口实现类的对象
129         Iterator iterator=col1.iterator();        
130         System.out.println("--------------------------");
131        while(iterator.hasNext()){
132            System.out.println(iterator.next()); 
133          }        
134     }
135

2 迭代器

 1 public class TestIterator2 {
 2     //方式一:
 3     @Test
 4     public void test1(){
 5 
 6         String str[]=new String[]{"AA","BB","dd"};
 7         for(int i=0;i<str.length;i++){
 8             System.out.println(str[i]);
 9         }
10     }
11     //方式二:
12     @Test
13     public void test2(){
14         String str[]=new String[]{"AAAAA","BBBBBB","dDDDd"};
15         for (String s : str) {
16             System.out.println(s);
17         }
18     }
19     //方式三:
20         @Test
21         public void test3(){
22             Collection col5=new ArrayList();
23             col5.add(1);
24             col5.add(2);
25             col5.add(3);
26             col5.add(4);
27             col5.add(55);
28             Iterator i=col5.iterator();
29             while(i.hasNext()){
30                 System.out.println(i.next());
31             }
32             
33         }

 

一 List接口:存储有序可重复元素
1.1 ArrayList(List主要实现类)
1) ArrayList相对于Collection新增加的方法:
①增 void add(Object obj)
②删 Object remove(Object obj)或remove(int index)
③改 Object set(int index,Object obj)
④查 Object get(int index)
⑤插 void add(int index,Object obj)
1.2 LinkedList(对于频繁插入、删除操作)
1.3 Vector:古老的实现类,线程安全,不常用。

 

二 Set接口:存储无序不可重复元素
2.1 HashSet(Set主要实现类)
1)无序性:添加元素后遍历元素,会呈现无序性,值得是元素在底层存储的位置是无序的,由HahsCode决定。
2)不可重复性:要求添加元素所在的类必须要重写equals()和HashCode()方法。
2.2 LinkedHashSet
1)遍历集合中元素时是按添加顺序遍历的,这是因为内部使用一个链表维护了一个添加集合中元素的顺序。
2.3 TreeSet
1)向TreeSet中添加元素时要求元素必须是同一类的;
2)可以指定按添加的某一元素顺便遍历;
3)两种排序方法:
|-----自然排序: ①要求添加元素所在类要实现Comparable接口,并在该类中重写compareTo(Object obj)方法;
②在此方法中指明按照哪个属性进行排序。
!!!要求:compareTO()与HashCode()、equals()方法一致

|-----定制排序: ①在测试类中new一个Comparator(){
         ②在这里重写compareTo()方法;
       };
      ③将此对象作为形参传给TreeSet构造器

 

    

Map接口:具有函数关系的一一对应的关系
1 key是用Set来存放的,不可重复 Set s=map.keySet()
Values是用Collection存放的,可重复;Collection c=map.values();
Entry:一个Key-Values对,不可重复。

 

一 HashMap接口
1 向HashMap中添加元素时,会调用key所在类的equals()方法,判断key值是否相等,若相等,后添加的会将前者覆盖;
2 常用方法
①增 Object put(Object key,Object value)
②删 Object remove(Object key)
③改 Object set(int index,Object obj)
④查 Object get(Object obj)
3 遍历
①遍历key值

1 //1 遍历key(Set)
2         Set set=map.keySet();
3         for (Object object : set) {
4             System.out.println(object);
5         }

②遍历value值

1 //2 遍历value集(Collection)
2         Collection set2=map.values();
3 //        for (Object object : set) {
4 //            System.out.println(object);
5 //        }
6         Iterator i=set2.iterator();
7         while(i.hasNext()){
8             System.out.println(i.next());
9         }

③遍历entry值

 1 //3 遍历key-value
 2         /*Set set3=map.keySet();
 3         for (Object obj : set3) {
 4             System.out.println(obj+"-------------->"+map.get(obj));
 5         }*/
 6         Set set4=map.entrySet();
 7         for (Object obj : set4) {
 8             Map.Entry entry=(Map.Entry)obj;
 9             System.out.println(entry);
10         } 

 

 


二 LinkedHashMap
三 TreeMap:按照添加进Map中的元素的key指定属性进行排序
1 自然排序
2 定制排序

 


四 Hashtable(不能存null)
子类:Properties常用来处理属性文件,键和值都是String类型的。

1     public void testHashtable() throws FileNotFoundException, IOException{
2         Properties pros=new Properties();
3         pros.load(new FileInputStream(new File("wgs.properties")));
4         String user=pros.getProperty("user");
5         System.out.println(user);
6         String code=pros.getProperty("password");
7         System.out.println(code);
8         
9     }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2015-12-02 23:14  noaman_wgs  阅读(270)  评论(0编辑  收藏  举报