Collection接口

  1 package day2_18;
  2 
  3 import org.junit.Test;
  4 
  5 import java.util.*;
  6 
  7 /**
  8  *
  9  * 一、集合框架的概数
 10  * 1.集合、数组都是对多个数据进行存储操作的结构,简称Java容器
 11  *  说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt .jpg .avi,数据库)
 12  *
 13 *  2.1数组在存储多个数据方面的特点
 14  *      >一旦初始化后,其长度就确定了
 15  *      >数组一旦定义好了,其元素的类型也就确定了。我们也就只能操作指定类型的数据了
 16  *      比如 String[] arr; int[] arr1; Object[] arr2
 17  *      >数组存储数据元素的特点:有序、不可重复
 18  *
 19 *  2.2数组在存储多个数据方面的缺点
 20  *      >一旦初始化后,其长度就不可修改
 21  *      >数组中提供的方法非常有限,对于添加、删除、插入数据等操作,非常不便,同时效率不高
 22  *      >获取数组实际元素的个数的需求,数组没有现成的属性或方法可用
 23  *      >数组存储数据的特点:有序、可重复,对于无序,不可重复的需求,数组不能满足
 24  *
 25  *
 26  *
 27 *  二、集合框架
 28  *   |----Collection接口,单列集合,存储一个一个的对象
 29  *      |----List接口:存储有序,可重复的数据  -->“动态”数组
 30  *          |----ArrayList、LinkedList、Vector
 31  *
 32  *      |----Set接口:存储无序,不可重复的数据  -->高中讲的“集合”
 33  *          |----HashSet、LinkedHashSet、TreeSet
 34  *
 35 *   |-----Map接口,双列集合,用来存储一对一对(key-value)的数据  -->高中函数:y= f(x)
 36  *          |----HashMap、LinkedHashMap、TreeMap、Hashtable、Properties
 37  *
 38  *
 39  * 三、Collection接口中的方法使用
 40  *    结论:
 41  *      向Collection接口的实现类对象中添加数据obj时,要求obj所在类要重写equals()
 42  *
 43  * @Author Tianhao
 44  * @create 2021-02-18-11:05
 45  */
 46 public class CollectionTest {
 47     @Test
 48     public void test1() {
 49         Collection coll = new ArrayList();
 50         //1.add(Object e):将元素e添加到集合中
 51         coll.add("AA");
 52         coll.add("BB");
 53         coll.add(123);//自动装箱
 54         coll.add(new Date());
 55 
 56         //2.size():获取添加的元素的个数
 57         System.out.println(coll.size());//4
 58 
 59         //3.addAll(Collection coll1):将coll1集合中的元素添加到当前集合中
 60         Collection coll1 = new ArrayList();
 61         coll1.add(456);
 62         coll1.add("CC");
 63         coll.addAll(coll1);
 64         System.out.println(coll.size());//6
 65         System.out.println(coll);//[AA, BB, 123, Thu Feb 18 12:02:58 CST 2021, 456, CC]
 66 
 67         //4.clear():清空集合元素
 68         coll.clear();
 69         System.out.println(coll);//[]
 70 
 71         //5.isEmpty():判断当前集合是否为空
 72         System.out.println(coll.isEmpty());//true
 73     }
 74 
 75     @Test
 76     public void test2() {
 77         Collection coll = new ArrayList();
 78         coll.add(123);
 79         Person p = new Person("张三", 23);
 80         coll.add(p);
 81         coll.add(456);
 82         coll.add(new String("Tom"));
 83         coll.add(false);
 84         coll.add(new Person("张三", 23));
 85         System.out.println(coll.size());
 86 
 87         //6.contains(Object obj):判断当前集合中是否包含obj
 88         //在判断时会调用obj对象所在类的equals(),与集合中的每个元素比较内容,而不是比较引用地址
 89         boolean contains = coll.contains(123);
 90         System.out.println(contains);//true
 91         System.out.println(coll.contains(new String("Tom")));//true
 92         System.out.println(coll.contains(p));//true
 93         //如果自定义Person类重写了equals(),则比较的是内容,就会返回true
 94         System.out.println(coll.contains(new Person("张三", 23)));
 95 
 96         System.out.println("********************");
 97         //7.containsAll(Colleciton coll1):判断形参coll1的所有所有元素是否都存在于当前集合中
 98         Collection coll1 = Arrays.asList(123,456);
 99         System.out.println(coll.containsAll(coll1));//true
100     }
101 
102     @Test
103     public void test3() {
104 
105         Collection coll = new ArrayList();
106         coll.add(123);
107         coll.add(456);
108         coll.add(false);
109         coll.add(new String("Tom"));
110         coll.add(new Person("张三", 23));
111 
112         //8.remove(Object obj):从当前集合中移除obj元素
113         //底层也会调用obj所在类的equals()
114         boolean remove = coll.remove(1234);
115         System.out.println(remove);
116         coll.remove(new Person("张三", 23));
117         System.out.println(coll);
118 
119         //9.removeAll(Collection coll1):从当前集合中移除coll1的所有元素
120         //差集:也就是在当前集合中把当前集合和参数集合的相同元素移除掉
121         Collection coll1 = Arrays.asList(123, 4567);
122         coll.removeAll(coll1);
123         System.out.println(coll);
124     }
125 
126     @Test
127     public void test4() {
128 
129         Collection coll = new ArrayList();
130         coll.add(123);
131         coll.add(456);
132         coll.add(false);
133         coll.add(new String("Tom"));
134         coll.add(new Person("张三", 23));
135 
136         Collection coll1 = Arrays.asList(123, 456);
137 
138         //10.retainAll(Collection coll1):交集:获取当前集合和参数集合coll1的交集,并赋值给当前集合
139 //        boolean b = coll.retainAll(coll1);
140 //        System.out.println(b);
141 //        System.out.println(coll);
142 
143         Collection coll2 = new ArrayList();
144         coll2.add(456);
145         coll2.add(123);
146 
147         coll2.add(false);
148         coll2.add(new String("Tom"));
149         coll2.add(new Person("张三", 23));
150 
151         //11.equals(Object obj):判断当前集合和参数集合的元素内容是否都相同(List对象则需要顺序也要相同)
152         boolean equals = coll.equals(coll2);
153         System.out.println(equals);
154     }
155 
156     @Test
157     public void test5() {
158 
159         Collection coll = new ArrayList();
160         coll.add(123);
161         coll.add(456);
162         coll.add(false);
163         coll.add(new String("Tom"));
164         coll.add(new Person("张三", 23));
165 
166         //12.hashCode()
167         System.out.println(coll.hashCode());//739633142
168 
169         //13.toArray():集合 --> 数组
170         Object[] arr = coll.toArray();
171         for (Object object : arr) {
172             System.out.println(object);
173         }
174         
175         //拓展:调用数组工具类Arrays.toList(T... a): 数组 -->集合
176         //注意参数列表是可变参数
177         List<String> list = Arrays.asList(new String[]{"aa", "bb", "cc"});
178         System.out.println(list);//[aa, bb, cc]
179         //包装类数组会当成多个元素
180         List<Integer> arr1 = Arrays.asList(new Integer[]{123, 456});
181         System.out.println(arr1.size());//2
182         System.out.println(arr1);//[123, 456]
183         //基本类型数组会当成一个元素
184         List<int[]> arr2 = Arrays.asList(new int[]{12, 34});
185         System.out.println(arr2.size());//1
186         System.out.println(arr2);//[[I@78e03bb5]
187 
188         //14.iterator():返回Iterator接口的实例,用于遍历集合元素
189 
190     }
191 
192 }

Person类

 1 package day2_18;
 2 
 3 import java.util.Objects;
 4 
 5 /**
 6  * @Author Tianhao
 7  * @create 2021-02-18-12:16
 8  */
 9 public class Person {
10     private String name;
11     private int age;
12 
13     public Person(String name, int age) {
14         this.name = name;
15         this.age = age;
16     }
17 
18     public String getName() {
19         return name;
20     }
21 
22     public void setName(String name) {
23         this.name = name;
24     }
25 
26     public int getAge() {
27         return age;
28     }
29 
30     public void setAge(int age) {
31         this.age = age;
32     }
33 
34     @Override
35     public boolean equals(Object o) {
36         System.out.println("day2_18.Person equals()...");
37         if (this == o) return true;
38         if (o == null || getClass() != o.getClass()) return false;
39         Person person = (Person) o;
40         return age == person.age &&
41                 Objects.equals(name, person.name);
42     }
43 
44     @Override
45     public String toString() {
46         return "day2_18.Person{" +
47                 "name='" + name + '\'' +
48                 ", age=" + age +
49                 '}';
50     }
51 }

 

posted @ 2021-02-20 18:43  dog_IT  阅读(69)  评论(0编辑  收藏  举报