Java学习笔记之集合Collection&Map

存储对象可以考虑两种方式:①数组,②集合

数组存储对象的特点:Student[] stu = new Student[20]; stu[0] = new Student();......这样存储有一定的弊端

 

1、一旦创建,其长度不可变。

2、真实的数组存放的对象的个数是不可知的。

 

集合:

 

Collection接口

          |--------------List接口:存储有序的,可以重复的元素

                                 |----------ArrayList(List的主要实现类)

                                 |-----------LinkedList(对集合频繁的插入和删除时使用)

                                 |-----------Vector(线程安全的,比较古老的实现类,效率较低)

          |---------------Set接口:存储无序的,不可重复的元素。set中常用的方法都是Collection定义的。

                                |-----------HashSet(Set的主要实现类):1.无序的≠随机的,真正的无序性是指元素在底存储的位置是无序的。

                                                                                                    2.不可重复性:当向set中添加进相同的元素的时候,后面的这个不能添加进去

                                                                                                    3.

                                                                                                    说明:要求添加进set中的元素所在的类要重写equals()方法和hashCode()方法,进而保证元素的不可重复性。

                                                                                                   set中元素是如何存储的?使用哈希算法

                                                                                                当向set中添加元素时,首先调用此对象所在类的hashCode()方法,然后计算此对象的hash值,此hash值决定了                                                                                                  此对象在set中的存储位置,若此位置之前没有对象存储,则此对象直接存到该位置,若此位置已有对象存储,再                                                                                                  通过equals方法比较这两个对象是否相同,若相同那么后一个对象就不能添加进来

                                                                                                 要求:equals()方法要与hashCode()方法一致。

                                |-----------LinkedHashSet:使用链表维护了一个添加进集合中的顺序。导致当我们遍历LinkedHashSet集合元素时,是按照添加进去的顺序遍历的。

                                                                                LinkedHashSet插入性能略低于HashSet,但在迭代访问set里的全部元素时有很好的性能。

                                |-----------TreeSet:1.向TreeSet中添加元素必须是同一个类的。

                                                                   2.可以按照添加进集合中的元素的指定的顺序遍历。像String,包装类等默认按照从小到大的顺序遍历。

                                                                   3. 当向TreeSet中添加自定义类对象时,有两种排序方法:①自然排序②定制排序

                                                                   4.自然排序:要求自定义类实现Java.lang.Comparable接口并重写其compareTo(Object obj)抽象方法,在此方法中指明按照自定                                                                         义类的哪个属性进行排序。

                                                                   5.向TreeSet中添加元素时,首先按照compareTo()方法进行比较,一旦返回0,虽然仅是两个对象的此属性值相同,但是程序会认                                                                       为这两个对象是相同的,进而后一个对象就不能添加进来。

                                                                       >compareTo()与hashCode()以及equals()三者保持一致。

                                                                   6.定制排序:compare()与hashCode()以及equals()三者保持一致。

[java] view plain copy
 
 print?
  1. public class TestCollection {  
  2.     @Test  
  3.     public void test1() {  
  4.         Collection c1 = new ArrayList();  
  5.         // 1.size():返回集合中元素的个数  
  6.         System.out.println(c1.size());  
  7.         // 2.add(Object obj):向集合中添加一个元素  
  8.         c1.add(123);  
  9.         c1.add("AA");  
  10.         c1.add(new Date());  
  11.         c1.add("BB");  
  12.         System.out.println(c1.size());  
  13.         // 3.addAll(Collection coll): 将形参coll中包含的所有元素添加到当前集合中  
  14.         Collection c2 = Arrays.asList(1, 2, 3);  
  15.         c1.addAll(c2);  
  16.         System.out.println(c1.size());  
  17.         // 4.isEmpty():判断集合是否为空  
  18.         System.out.println(c1.isEmpty());  
  19.         // 查看集合元素  
  20.         System.out.println(c1);  
  21.         // 5.clear():清空集合中的元素  
  22.         c1.clear();  
  23.         System.out.println(c1.isEmpty());  
  24.     }  
  25.   
  26.     @Test  
  27.     public void test2() {  
  28.         Collection c1 = new ArrayList();  
  29.         c1.add(123);  
  30.         c1.add(new String("AA"));  
  31.         c1.add(new Date());  
  32.         c1.add("BB");  
  33.         c1.add(new Person("MM", 22));  
  34.         System.out.println(c1);  
  35.         // 6.contains(Object obj): 判断集合中是否包含指定的obj元素  
  36.         // 判断依据:根据元素所在的类的equals()方法判断  
  37.         // 明确:如果集合中存入的元素是自定义类型对象,那么自定义的类需要重写equals()方法。  
  38.         boolean b1 = c1.contains(123);  
  39.         System.out.println(b1);  
  40.         b1 = c1.contains("AA");  
  41.         System.out.println(b1);  
  42.         boolean b2 = c1.contains(new Person("MM", 22));  
  43.         System.out.println(b2);  
  44.         // 7.containsAll(Collection coll):判断当前集合中是否包含coll中所有的元素。  
  45.         Collection c2 = new ArrayList();  
  46.         c2.add(123);  
  47.         c2.add(new String("AA"));  
  48.         c2.add(456);  
  49.         boolean b3 = c1.containsAll(c2);  
  50.         System.out.println(b3);  
  51.         // 8.retainAll(Collection coll):求当前集合与coll的共有的元素,返回给当前集合。  
  52.         c1.retainAll(c2);  
  53.         System.out.println(c1);  
  54.         // 9.remove(Object obj):从当前集合中删除obj元素,若删除成功返回true,否则返回false。  
  55.         System.out.println(c1.remove("BB"));  
  56.         // 10.removeAll(Collection coll):从当前集合中删除包含在coll中的元素,若删除成功返回true,否则返回false。  
  57.         System.out.println(c1.removeAll(c2));  
  58.     }  
  59.   
  60.     @Test  
  61.     public void test3() {  
  62.         Collection c1 = new ArrayList();  
  63.         c1.add(123);  
  64.         c1.add(new String("AA"));  
  65.         c1.add(new Date());  
  66.         c1.add(new String("BB"));  
  67.         c1.add(new Person("GG", 22));  
  68.         Collection c2 = new ArrayList();  
  69.         c2.add(123);  
  70.         c2.add(new Person("GG", 22));  
  71.         System.out.println(c1.remove("AA"));  
  72.         System.out.println(c1.removeAll(c2));  
  73.         System.out.println(c1);  
  74.         // 11.equals(Object obj):判断集合中所有元素是否完全相等。  
  75.         // 12.hashCode(): 获得集合中元素的散列值。  
  76.         System.out.println(c1.hashCode());  
  77.         // 13.toArray():将集合转换为数组  
  78.         System.out.println(c1);  
  79.         Object[] obj = c1.toArray();  
  80.         for (int i = 0; i < obj.length; i++) {  
  81.             System.out.println(obj[i]);  
  82.         }  
  83.         System.out.println();  
  84.         // 14.iterator():返回一个Iterator接口实现类对象,用来遍历集合。  
  85.         Iterator it = c1.iterator();  
  86.         while (it.hasNext()) {  
  87.             System.out.println(it.next());  
  88.         }  
  89.     }  
  90. }  

 

 

 

Map接口:存储“键-值”结构的数据

          |-----------HashMap

          |-----------LinkedHashMap

          |-----------TreeMap

          |-----------HashTable(子类:Properties)

 

HashMap:Map的主要实现类。

 

1、key是用Set来存放的,不可重复。value是用Collection来存放的,可重复。一个key-value对,是一个Entry。所有的Entry是用Set存放的,也是不可重复的。

2、向HashMap中添加元素时会调用key所在类的equals()方法判断两个key是否相同,若相同,则只能添加进后添加的元素。 

如何遍历Map? Set keySet()、Collection values()、Set entrySet() 

 

LinkedHashMap:

使用链表维护添加进Map中的顺序。故遍历时,是按照添加的顺序遍历的。与LinkedHashSet类似

TreeMap:

按照添加进Map中的元素的key的指定属性进行排序。要求:key必须是同一个类的对象!

Hashtable:古老的实现类,线程安全,不建议使用,其子类Propertirs常用来处理属性文件。键和值都为String类型的。

 

[java] view plain copy
 
 print?
  1. public class TestMap {  
  2.     @Test  
  3.     public void test1() {  
  4.         Map map = new HashMap();  
  5.         // 1.Object put(Object key, Object value):向map中添加一个元素(对象)  
  6.         // 2.int size():返回集合中元素的个数。  
  7.         map.put("AA", 123);  
  8.         map.put("BB", 456);  
  9.         map.put("BB", 45);  
  10.         map.put(123, "CC");  
  11.         map.put(null, null);  
  12.         map.put(new Person("AA", 20), 66);  
  13.         map.put(new Person("AA", 20), 67);  
  14.         System.out.println(map.size());  
  15.         System.out.println(map);  
  16.         map.remove(null);  
  17.         System.out.println(map);  
  18.         // 3.Object get(Object key):返回指定key的value值,若无此key则返回null.  
  19.         Object value = map.get(123);  
  20.         System.out.println(value);  
  21.         System.out.println(map.get(null));  
  22.         System.out.println(map.get(1234));  
  23.     }  
  24.   
  25.     @Test  
  26.     public void test2() {  
  27.         Map map = new HashMap();  
  28.         map.put("AA", 123);  
  29.         map.put("BB", 456);  
  30.         map.put(123, "CC");  
  31.         map.put(null, null);  
  32.         map.put(new Person("AA", 123), 22);  
  33.         //遍历Map的key值  
  34.         Set keySet = map.keySet();  
  35.         Iterator it = keySet.iterator();  
  36.         while (it.hasNext()) {  
  37.             System.out.println(it.next());  
  38.         }  
  39.         System.out.println("==============");  
  40.         // 遍历Map的value  
  41.         Collection coll = map.values();  
  42.         Iterator it1 = coll.iterator();  
  43.         while (it1.hasNext()) {  
  44.             System.out.println(it1.next());  
  45.         }  
  46.         System.out.println("+++++++++++++");  
  47.         // 遍历Map的键值对 entry  
  48.         Set entrySet = map.entrySet();  
  49.         for (Object obj : entrySet) {  
  50.             Map.Entry entry = (Map.Entry) obj;  
  51.             System.out.println(entry.getKey() + "------->" + entry.getValue());  
  52. //            System.out.println(entry);  
  53.         }  
  54.     }  
  55.   
  56.     @Test  
  57.     public void test3() {  
  58.         Map map = new LinkedHashMap();  
  59.         map.put("AA", 123);  
  60.         map.put("BB", 456);  
  61.         map.put(123, "CC");  
  62.         map.put(null, null);  
  63.         map.put(new Person("AA", 123), 22);  
  64.   
  65.         Set set = map.keySet();  
  66.         Iterator it = set.iterator();  
  67.         while (it.hasNext()) {  
  68.             System.out.println(it.next());  
  69.         }  
  70.     }  
  71.   
  72.     @Test  
  73.     public void testTreeMap1() {  
  74.         Map map = new TreeMap();  
  75.         map.put("AA", 123);  
  76.         map.put("BB", 456);  
  77.         map.put(123, "CC");  
  78.         map.put(null, null);  
  79.         map.put(new Person("AA", 123), 22);  
  80.   
  81.     }  
  82.   
  83.     @Test  
  84.     public void testProperties() {  
  85.         try {  
  86.             Properties props = new Properties();  
  87.             props.load(new FileInputStream(new File("jdbc.properties")));  
  88.             String user = props.getProperty("user");  
  89.             String password = props.getProperty("password");  
  90.             System.out.println("用户名:" + user + "\n" + "密码:" + password);  
  91.         } catch (FileNotFoundException e) {  
  92.             System.out.println(e.getMessage());  
  93.         } catch (IOException e) {  
  94.             e.printStackTrace();  
  95.         }  
  96.   
  97.     }  
  98. }  


加:操作集合的工具类:Collections

posted @ 2016-12-05 15:17  天涯海角路  阅读(79)  评论(0)    收藏  举报