集合框架的概述及Collection常用方法(一)

  1 import org.junit.Test;
  2 
  3 import java.util.*;
  4 
  5 /**
  6  * 一、集合框架的概述
  7  * 1.集合、数组都是对多个数据进行存储操作的结构,简称为java容器。
  8  *    说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储。
  9  * 2.数组在存储多个数据方面的
 10  *  2.1特点:
 11  *    >一旦初始化以后,长度就确定了。
 12  *    >数组一旦定义好,其元素的类型也就确定了。我们也就只能操作指定类型的数据了。比如 String[] arr; int[] arr;Object[] arr
 13  *  2.2缺点:
 14  *    >一旦初始化以后,长度就不可修改。
 15  *    >数组中提供的方法非常有限,对于添加、删除、插入数据等操作,非常不便,同时效率不高。
 16  *    >获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用。
 17  *    >数组存储数据的特点:有序、可重复。对于无序、不可重复的需求,不能满足。
 18  *二、集合框架
 19  *      /----Collection接口:单列集合,用来存储一个一个的对象
 20  *          /----list接口:存储有序的、可重复的数据。 ---> “动态”数组
 21  *              /----ArrayList、LinkedList、Vector
 22  *
 23  *          /----set接口:存储无序的、不可重复的数据。--->高中讲的 “集合”
 24  *              /----HashSet、LinkedHashSet、TreeSet
 25  *
 26  *      /----Map接口:双列集合,用来存储一对一对(key-value)的数据--->高中讲的 “函数”
 27  *          /----HashMap、LinkedHashMap、TreeMap、Hashtable、Properties
 28  *三、Collection接口中的方法的使用
 29  *
 30  *
 31  * @author fu jingchao
 32  * @creat 2021/12/7-19:17
 33  */
 34 public class CollectionTest {
 35     //向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals()方法
 36     @Test
 37     public void test1(){
 38         Collection coll = new ArrayList();
 39 
 40         //add(Object e):将元素e添加到集合coll中
 41         coll.add("AA");
 42         coll.add("BB");
 43         coll.add(123);//自动装箱
 44         coll.add(new Date());
 45         coll.add(new String("Tom"));
 46         coll.add(new Person("Jack",20));
 47 
 48         //size:获取添加的元素的个数
 49         System.out.println(coll.size());//4
 50 
 51         //addAll(Collection coll1):将coll1集合中的元素全部添加到当前的集合中
 52         Collection coll1 = new ArrayList();
 53         coll1.add(456);
 54         coll1.add("CC");
 55         coll.addAll(coll1);
 56         System.out.println(coll.size());
 57         System.out.println(coll);
 58 
 59         //isEmpty():判断当前集合是否为空
 60         System.out.println(coll.isEmpty());//false
 61 
 62         //clear():清空集合元素
 63 //        coll.clear();
 64 //        System.out.println(coll.isEmpty());//true
 65 
 66         //contains(Object obj):判断当前集合中是否包含obj  判断的是内容,不是地址
 67         //我们在判断时会调用obj对象所在类的equals()方法,自定义类需要重写equals()方法
 68         boolean contains = coll.contains(456);
 69         System.out.println(contains);//true
 70         System.out.println(coll.contains(new String("Tom")));//true
 71 
 72         //2.containsAll(Collection coll1):判断形参coll1对应的集合中的所有元素是否都存在于当前集合中
 73         boolean containsAll = coll.containsAll(coll1);
 74         System.out.println(containsAll);//true
 75 
 76     }
 77     @Test
 78     public void test2(){
 79         //3.remove(Object obj):从当前集合中移除obj元素
 80         Collection coll = new ArrayList();
 81         coll.add("AA");
 82         coll.add("BB");
 83         coll.add("CC");
 84         coll.add(123);//自动装箱
 85         coll.add(new Date());
 86         coll.add(new String("Tom"));
 87         coll.add(new Person("Jack",20));
 88         System.out.println(coll);
 89         coll.remove(123);
 90         System.out.println(coll);
 91 
 92         Collection coll1 = new ArrayList();
 93         coll1.add(456);
 94         coll1.add("CC");
 95         coll1.add(new Person("Jack",20));
 96         //4.removeAll(Collection coll1):从当前集合中移除coll1共有的元素。
 97         coll.removeAll(coll1);
 98         System.out.println(coll);//[AA, BB, Wed Dec 08 16:02:42 CST 2021, Tom]
 99     }
100     @Test
101     public void test3(){
102         Collection coll = new ArrayList();
103         coll.add("AA");
104         coll.add("BB");
105         coll.add("CC");
106         coll.add(123);//自动装箱
107         coll.add(new String("Tom"));
108         coll.add(789);
109         coll.add(new Person("Jack",20));
110         System.out.println(coll);//[AA, BB, CC, 123, Tom, 789, Person{name='Jack', age=20}]
111 
112 //        //5.retainAll(Collection coll1):获取当前集合和coll1的交集,并返回给当前集合(对当前集合做了修改)
113 //        Collection coll1 = Arrays.asList(123,456,789,333);
114 //        coll.retainAll(coll1);
115 //        System.out.println(coll);//[123, 789]
116 
117         //6.equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同。
118         Collection coll1 = new ArrayList();
119         coll1.add("AA");
120         coll1.add("BB");
121         coll1.add("CC");
122         coll1.add(123);//自动装箱
123         coll1.add(new String("Tom"));
124         coll1.add(789);
125         coll1.add(new Person("Jack",20));
126         System.out.println(coll1);//[AA, BB, CC, 123, Tom, 789, Person{name='Jack', age=20}]
127 
128         System.out.println(coll.equals(coll1));//true
129     }
130     @Test
131     public void test4(){
132         Collection coll = new ArrayList();
133         coll.add("AA");
134         coll.add("BB");
135         coll.add("CC");
136         coll.add(123);//自动装箱
137         coll.add(new String("Tom"));
138         coll.add(789);
139         coll.add(new Person("Jack",20));
140 
141         //7.hashCode():返回当前对象的哈希值
142         System.out.println(coll.hashCode());
143 
144         //8.集合---->数组 toArray()
145         Object[] array = coll.toArray();
146         for (int i = 0; i < array.length; i++) {
147             System.out.println(array[i]);
148         }
149         //拓展:数组--->集合:调用Arrays类的静态方法asList()
150         List<String> strings = Arrays.asList(new String[]{"AA", "BB"});
151         List<Integer> integers = Arrays.asList(new Integer[]{123, 456});
152         List array1 = Arrays.asList(new int[]{123, 456});
153         System.out.println(integers);//[123, 456]
154         System.out.println(array1);//[[I@2b05039f]
155 
156         //9.iterator():返回Iterator接口的实例,用于遍历集合元素。
157     }
158 }

 

 1 /**
 2  * @author fu jingchao
 3  * @creat 2021/12/8-14:53
 4  */
 5 public class Person {
 6     private String name;
 7     private int age;
 8 
 9     public Person() {
10     }
11 
12     public Person(String name, int age) {
13         this.name = name;
14         this.age = age;
15     }
16 
17     public String getName() {
18         return name;
19     }
20 
21     public void setName(String name) {
22         this.name = name;
23     }
24 
25     public int getAge() {
26         return age;
27     }
28 
29     public void setAge(int age) {
30         this.age = age;
31     }
32 
33     @Override
34     public String toString() {
35         return "Person{" +
36                 "name='" + name + '\'' +
37                 ", age=" + age +
38                 '}';
39     }
40 
41     @Override
42     public boolean equals(Object o) {
43         System.out.println("Person equals()....");
44         if (this == o) return true;
45         if (o == null || getClass() != o.getClass()) return false;
46 
47         Person person = (Person) o;
48 
49         if (age != person.age) return false;
50         return name != null ? name.equals(person.name) : person.name == null;
51     }
52 
53 //    @Override
54 //    public int hashCode() {
55 //        int result = name != null ? name.hashCode() : 0;
56 //        result = 31 * result + age;
57 //        return result;
58 //    }
59 }

 

posted @ 2021-12-12 17:00  橘猫的夏天  阅读(40)  评论(0编辑  收藏  举报