Java-集合的总结

 Collection:接口

只有通过Collection接口中toString()进行重写,才能够System.out.print(coll)显示Collection集合中元素 

     |-----Set:元素无序,不可重复的集合

        |---hashSet(主要实现类) +LinkedHashSet(添加顺序遍历集合中元素)+TreeSet 

      Set 使用的方法基本上Collection接口下定义;  

      无序:元素底层存储的位置无序的,不重复性:添加相同元素无法添加进去

      HashSet(自定义类的重复性重写 hashCode和equals方法):原理:向Set集合中添加元素,首先hashCode方法计算hash值,决定此对象存取的位置

 在此位置有对象,在比较equals方法----注意:重写HashCode方法:对象属性值一样保证HashCode一样,对象属性值不一样保证HashCode不一样,不要相同hashcode值但是有不同的属性值。

    Treeset:(对加入元素进行排序处理)存储对象按照ComparaTo方法进行排序 

           1.TreeSet添加元素属于同一个类型,HashSet属于不同类型

           2.两种排序: 1.自然排序 自定义类实现的是 Comparable 接口 重写ComparaTo(按照某种属性排序,属性相同按照其他属性相同) +hashCode+equals方法

                              2.定制排序:1.创建一个Comparator接口的对象(采用匿名内部类方法)     2.在此对象中重写Compare的方法在此compare方法指明按照某个属性进行排序,3.将对象作为形参 传入TreeSet构造器中。

 要求重写 comparaTo 和Compare方法 与equals和HashCode方法表持一致---hashCode和equals用于对元素的储存


     |--------List:元素有序,可重复:相当于动态的数组

        |----ArrayList(主要的实现类) + LinkedList(适用于频繁的插入和删除)

   对于Collection的contains()方法判断依据 元素所在类的equals方法,如果元素使用自定义类的 重写自定义类中equals方法

Map 接口:具有映射关系 存放的键-值对

Key 使用set存储,不能重复;value用Collection存放,key-value 对构成entry,entry(Map.entrySet)使用Set存放

    |---HashMap:线程不安全存放null 值

    |---HashTable;线程安全的  

              |---Properties处理属性文件        ConcurrentHashMap:CAS的积极锁的操作

   |----TreeMap:按照可以所在类指定属性排序 Key必须要是同一个类型

Collections是集合的工作类,大部分static 的方法

 1 package com.testCollection;
 2 
 3 public class Empolyee implements Comparable{
 4 
 5     private String name ;
 6     private Integer age;
 7     private MyData birthday;
 8     public String getName() {
 9         return name;
10     }
11     public void setName(String name) {
12         this.name = name;
13     }
14     public Integer getAge() {
15         return age;
16     }
17     public void setAge(Integer age) {
18         this.age = age;
19     }
20     public MyData getBirthday() {
21         return birthday;
22     }
23     public void setBirthday(MyData birthday) {
24         this.birthday = birthday;
25     }
26     // 遍历的时候直接打印输出
27     @Override
28     public String toString() {
29         return "Empolyee [name=" + name + ", age=" + age + ", birthday=" + birthday + "]";
30     }
31     public Empolyee(String name, Integer age, MyData birthday) {
32         super();
33         this.name = name;
34         this.age = age;
35         this.birthday = birthday;
36     }
37     @Override
38     public int hashCode() {
39         final int prime = 31;
40         int result = 1;
41         result = prime * result + ((age == null) ? 0 : age.hashCode());
42         result = prime * result + ((birthday == null) ? 0 : birthday.hashCode());
43         result = prime * result + ((name == null) ? 0 : name.hashCode());
44         return result;
45     }
46     @Override
47     public boolean equals(Object obj) {
48         if (this == obj)
49             return true;
50         if (obj == null)
51             return false;
52         if (getClass() != obj.getClass())
53             return false;
54         Empolyee other = (Empolyee) obj;
55         if (age == null) {
56             if (other.age != null)
57                 return false;
58         } else if (!age.equals(other.age))
59             return false;
60         if (birthday == null) {
61             if (other.birthday != null)
62                 return false;
63         } else if (!birthday.equals(other.birthday))
64             return false;
65         if (name == null) {
66             if (other.name != null)
67                 return false;
68         } else if (!name.equals(other.name))
69             return false;
70         return true;
71     }
72     @Override
73     public int compareTo(Object o) {
74         if(o instanceof Empolyee)
75         {
76             Empolyee e1=(Empolyee) o;
77             // 本对象Name与输入名字进行标胶
78             int i=this.name.compareTo(e1.getName());
79              if(i==0)
80                  return this.age.compareTo(e1.getAge());
81              else
82                  return i;
83             }
84         return 0;
85     }
86     
87 }
 1 @Test
 2     public void test2()
 3     {
 4         // 创建一个实现Comparator类 的对象 自定义类防止混淆,创建新的类,有相同的属性
 5         Comparator com=new Comparator(){
 6 
 7             @Override
 8             public int compare(Object o1, Object o2) {
 9                 if(o1 instanceof Employee1 && o2 instanceof Employee1)
10                 {
11                     Employee1 e1=(Employee1) o1;
12                     Employee1 e2=(Employee1) o2;
13                     
14                     // 按照年月日比较
15                     MyData birth1=e1.getBirthday();
16                     MyData birth2=e2.getBirthday();
17                     
18                     boolean birthequals=false;
19                     if(birth1.getYear()!=birth2.getYear())
20                     {    return birth1.getYear()-(birth2.getYear());}
21                     else
22                     {
23                         if(birth1.getMonth()!=birth2.getMonth())
24                             return birth1.getMonth().compareTo(birth2.getMonth());
25                         else
26                         {
27                             if(birth1.getDay()!=birth2.getDay())
28                                 {return birth1.getDay().compareTo(birth2.getDay());}
29                             else
30                                 birthequals=true;
31                         }
32                     }
33                     // 按照名字进行排序
34                     
35                 }
36                 return 0;
37             }
38         };
39             // 自定义时候传入Comparator类的对象
40             TreeSet set=new TreeSet(com);
41             Employee1 e11=new Employee1("刘德华",55,new MyData(4,12,1966));
42             Employee1 e2=new Employee1("刘德",55,new MyData(4,12,1956));
43             
44             set.add(e11);
45             set.add(e2);
46             
47             for(Object str:set)
48                 System.out.println(str);
49             
50     }
原文地址:https://www.cnblogs.com/woainifanfan/p/6771343.html