码迷,mamicode.com
首页 > 编程语言 > 详细

java集合学习一-Collection学习

时间:2020-02-09 21:57:39      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:tcl   pen   ons   close   private   图片   com   ide   vat   

 

集合类练习

技术图片
  1 package com.example.demo.collection;
  2 
  3 import org.junit.Test;
  4 
  5 import java.util.*;
  6 
  7 public class CollectionTest {
  8     @Test
  9     public void test1() {
 10         Collection coll = new ArrayList();
 11         coll.add("AA");
 12         coll.add("BB");
 13         coll.add(123); // 自动装箱
 14         coll.add(new Date());
 15         System.out.println(coll.size()); // 个数
 16 
 17         // addAll(Collecton coll1):将coll1集合添加到Collecton集合中
 18         Collection coll1 = new ArrayList();
 19         coll1.add(456);
 20         coll1.add("CC");
 21         coll.addAll(coll1);
 22         System.out.println(coll.size());
 23         System.out.println(coll);
 24         //isEmpty():判断是否为空
 25         System.out.println(coll1.isEmpty());
 26         // clean清空集合元素
 27         coll1.clear();
 28         System.out.println(coll1.isEmpty());
 29     }
 30 
 31 
 32     @Test
 33     public void test2() {
 34         Collection coll = new ArrayList();
 35         coll.add(123);
 36         coll.add(456);
 37         coll.add(new String("Tom"));
 38         coll.add(false);
 39         coll.add(new Person("jerry", 20));
 40 
 41         // contains(Object obj) 判断元素中是否包含obj
 42         boolean contains = coll.contains(123);
 43         System.out.println(contains);
 44         System.out.println(coll.contains(456));
 45         System.out.println(coll.contains(new String("Tom")));
 46         System.out.println(coll.contains(false));
 47         System.out.println(coll.contains(new Person("jerry", 20)));
 48         System.out.println("======");
 49         // 对象1是不是包含对象2的数据
 50         Collection coll1 = Arrays.asList(123, 456);
 51         boolean b = coll.containsAll(coll1);
 52         System.out.println(b);
 53     }
 54 
 55     @Test
 56     public void test3() {
 57         Collection coll = new ArrayList();
 58         coll.add(123);
 59         coll.add(456);
 60         System.out.println(coll);
 61         boolean a = coll.remove(123);
 62         System.out.println(coll);
 63         System.out.println("是否移除成功:" + a);
 64         System.out.println(coll.remove(123));
 65         System.out.println(coll);
 66     }
 67 
 68     @Test
 69     public void test4() {
 70         Collection coll = new ArrayList();
 71         coll.add(123);
 72         coll.add(456);
 73         coll.add(789);
 74         coll.add("101");
 75         Collection cool1 = Arrays.asList(123, "101");
 76         // 将b的元素在a里面全部移除
 77         System.out.println(coll.removeAll(cool1));
 78         System.out.println(coll);
 79     }
 80 
 81 
 82     @Test
 83     public void test5() {
 84         Collection coll = new ArrayList();
 85         coll.add(123);
 86         coll.add(456);
 87         coll.add(789);
 88         coll.add("101");
 89         Collection coll1 = new ArrayList();
 90         coll1.add(123);
 91         coll1.add(456);
 92         coll1.add(789);
 93         coll1.add("101");
 94         System.out.println(coll.equals(coll1));
 95     }
 96 
 97     // 因为是有序集合,顺序不一样,所以
 98     @Test
 99     public void test6() {
100         Collection coll = new ArrayList();
101         coll.add(123);
102         coll.add(456);
103         coll.add("101");
104         coll.add(789);
105         Collection coll1 = new ArrayList();
106         coll1.add(123);
107         coll1.add(456);
108         coll1.add(789);
109         coll1.add("101");
110         System.out.println(coll.equals(coll1));
111     }
112 
113     // 计算hash值
114     @Test
115     public void test7() {
116         Collection coll = new ArrayList();
117         coll.add(123);
118         coll.add(456);
119         coll.add("101");
120         coll.add(789);
121         System.out.println(coll.hashCode());
122         // 集合转换为数组
123         Object[] arr = coll.toArray();
124         for (Object o : arr) {
125             System.out.println(o);
126         }
127 
128         System.out.println("========");
129         // 数组转集合
130         List<String> list = Arrays.asList(new String[]{"aa","bb","cc","dd"});
131         System.out.println(list);
132         //反例
133         List<int[]> ints = Arrays.asList(new int[]{123, 456});
134         System.out.println(ints);
135         // 修改
136         List<Integer> arr2 = Arrays.asList(new Integer[]{123, 456, 789});
137         System.out.println(arr2);
138         System.out.println(arr2.size());
139 
140         List arr3 = Arrays.asList(new int[]{123, 456, 789});
141         System.out.println(arr3.size());
142     }
143 
144     @Test
145     public void test8() {
146         Collection coll = new ArrayList();
147         coll.add(123);
148         coll.add(456);
149         coll.add("101");
150         coll.add(789);
151         Iterator iterator = coll.iterator();
152         // 第一种方式
153         for (int i=0;i<coll.size();i++){
154             System.out.println(iterator.next());
155         }
156         System.out.println("===");
157         // 第二种方式
158         while (iterator.hasNext()){
159             System.out.println(iterator.next());
160         }
161     }
162 
163 }
CollectionTest
技术图片
 1 package com.example.demo.collection;
 2 
 3 import lombok.*;
 4 
 5 import java.util.Objects;
 6 
 7 //@Data
 8 //@AllArgsConstructor
 9 //@NoArgsConstructor
10 //@ToString
11 public class Person {
12     private String name;
13 
14     public void setName(String name) {
15         this.name = name;
16     }
17 
18     public void setAge(Integer age) {
19         this.age = age;
20     }
21 
22     public String getName() {
23         return name;
24     }
25 
26     public Integer getAge() {
27         return age;
28     }
29 
30     public Person() {
31     }
32 
33     public Person(String name, Integer age) {
34         this.name = name;
35         this.age = age;
36     }
37 
38     private Integer age;
39 
40     @Override
41     public boolean equals(Object o) {
42         if (this == o) return true;
43         if (o == null || getClass() != o.getClass()) return false;
44         Person person = (Person) o;
45         return Objects.equals(name, person.name) &&
46                 Objects.equals(age, person.age);
47     }
48 
49     @Override
50     public int hashCode() {
51         return Objects.hash(name, age);
52     }
53 
54     @Override
55     public String toString() {
56         return "Person{" +
57                 "name=‘" + name + ‘\‘‘ +
58                 ", age=" + age +
59                 ‘}‘;
60     }
61 }
Person

 

java集合学习一-Collection学习

标签:tcl   pen   ons   close   private   图片   com   ide   vat   

原文地址:https://www.cnblogs.com/lizhen1412/p/12288867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!