前言:在刚学习的时候,切记不能粘贴复制,更不能眼高手低,再简单的代码,都要自己独立动手写。
第一步:目录结构
第二步:代码区
Student.java:(一个学生的实体类)
1 package com.mon11.day13.collection.po;
2 /**
3 * 类说明 :实体类
4 * @author 作者 : Administrator
5 * @version 创建时间:2017年11月13日
6 */
7 public class Student {
8
9 private String name;
10 private int age;
11
12
13 public String getName() {
14 return name;
15 }
16
17
18 public void setName(String name) {
19 this.name = name;
20 }
21
22
23 public int getAge() {
24 return age;
25 }
26
27
28 public void setAge(int age) {
29 this.age = age;
30 }
31
32
33
34 public Student() {
35 super();
36 // TODO Auto-generated constructor stub
37 }
38
39
40
41 public Student(String name, int age) {
42 super();
43 this.name = name;
44 this.age = age;
45 }
46
47
48 @Override
49 public String toString() {
50 return "Student [name=" + name + ", age=" + age + "]";
51 }
52
53 }
View Code
TestJunit.java:(这个就是测试玩一玩)
1 package com.mon11.day13.collection;
2
3 import static org.junit.Assert.*;
4
5 import org.junit.After;
6 import org.junit.Before;
7 import org.junit.Test;
8
9 /**
10 * 类说明 :
11 * @author 作者 : Administrator
12 * @version 创建时间:2017年11月13日
13 */
14 public class TestJunit {
15
16 @Before
17 public void init(){
18 System.out.println("不管你们是谁,我最先执行!");
19 }
20
21 @After
22 public void destory(){
23 System.out.println("我是终结者------------!");
24 System.out.println("---------------OO**_**OO-----------------------");
25 }
26
27 @Test
28 public void test1() {
29 System.out.println("我是test1,我来运行了!");
30 }
31 @Test
32 public void test2() {
33 System.out.println("我是test2,我来运行了!");
34 }
35
36 }
View Code
LinkedListDemo.java:(全是测试集合的添加,存储,读取)
1 package com.mon11.day13.collection;
2
3 import static org.junit.Assert.*;
4
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.HashMap;
8 import java.util.Iterator;
9 import java.util.LinkedList;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13
14 import org.junit.Before;
15 import org.junit.Test;
16
17 import com.mon11.day13.collection.po.Student;
18
19 /**
20 * 类说明 :关于集合
21 * @author 作者 : Administrator
22 * @version 创建时间:2017年11月13日
23 */
24 @SuppressWarnings({ "rawtypes", "unchecked" })
25 public class LinkedListDemo {
26
27
28 //对象数组
29 private Student[] stus=new Student[5];
30
31 @Before
32 public void init(){
33 Student stu1=new Student("陈1",1);
34 Student stu2=new Student("陈2",2);
35 Student stu3=new Student("陈3",3);
36 Student stu4=new Student("陈4",4);
37 Student stu5=new Student("陈5",5);
38
39 stus[0]=stu1;
40 stus[1]=stu2;
41 stus[2]=stu3;
42 stus[3]=stu4;
43 stus[4]=stu5;
44 }
45
46
47 //1.测试ArrayList
48 @Test
49 public void test1() {
50 List list=new ArrayList();
51
52 list.add("ww1");
53 list.add("ww2");
54 list.add("ww3");
55 list.add("ww4");
56 list.add("ww5");
57
58 list.set(1, "张三");
59
60
61 //迭代器遍历遍历输出
62 Iterator it=list.iterator();
63 while(it.hasNext()){
64 System.out.println(it.next());
65 }
66 }
67
68 //2.for循环输出
69 @Test
70 public void test2() {
71 List list=new ArrayList();
72
73 list.add("ww1");
74 list.add("ww2");
75 list.add("ww3");
76 list.add("ww4");
77 list.add("ww5");
78
79 //迭代器遍历遍历输出
80 for(int i=0;i<list.size();i++){
81 System.out.println(list.get(i));
82 }
83 }
84
85 //3.这个是根据数组的长度
86 @Test
87 public void test3() {
88
89 List list=new ArrayList();
90 for(int i=0;i<stus.length;i++){
91 list.add(stus[i]);
92 }
93
94 System.out.println(list);
95 }
96
97 //4.遍历输出LinkedList
98 @Test
99 public void test4() {
100 //List list=new LinkedList();
101 LinkedList list=new LinkedList();//特有的,多
102 for(int i=0;i<stus.length;i++){
103 list.add(stus[i]);
104 }
105 list.addFirst(new Student("王五",12));
106 list.addLast(new Student("王五",12));
107 list.pop();//删除出栈
108 list.push("sdsada");
109 System.out.println("---------------------------");
110 list.push(new Student("王五",12));
111 for(Object obj:list){
112 if(obj instanceof Student){
113 System.out.println(((Student) obj).getName());
114 }
115 }
116 }
117
118 //5.遍历输出LinkedList
119 @Test
120 public void test5() {
121
122 LinkedList list=new LinkedList();
123 for(int i=0;i<stus.length;i++){
124 list.add(stus[i]);
125 }
126
127 /*for(Object obj:list){
128 System.out.println(obj);
129 }
130 */
131 Iterator it=list.iterator();
132 while(it.hasNext()){
133 Object obj=it.next();
134 Student s=(Student) obj;
135 System.out.println(s.getName());
136 }
137 }
138
139 //6.关于hashcode
140 @Test
141 public void test6(){
142 String str="this";
143 String str1="this ";
144 System.out.println(str.hashCode());
145 System.out.println(str1.hashCode());
146 }
147
148
149 //7.hashMap
150 @Test
151 public void test7(){
152 Map map=new HashMap();
153 for(int i=0;i<stus.length;i++){
154 map.put(stus[i].getName(), stus[i]);
155 }
156
157 //遍历输出
158 Set set=map.keySet();//这个是干嘛的,有点不理解
159 for(Object obj: set){
160 System.out.println(obj+"------->"+map.get(obj));
161 }
162 }
163
164 //8.hashMap遍历输出通过key
165 @Test
166 public void test8(){
167 Map map=new HashMap();
168 for(int i=0;i<stus.length;i++){
169 map.put(stus[i].getName(), stus[i]);
170 }
171
172 //遍历输出
173 Set set=map.keySet();
174 Iterator it=set.iterator();
175 while(it.hasNext()){
176 String key=(String) it.next();
177
178 System.out.println(key+"---->"+map.get(key));
179 }
180 }
181
182 //9.hashMap遍历输出通过values
183 @Test
184 public void test9(){
185 Map map=new HashMap();
186 for(int i=0;i<stus.length;i++){
187 map.put(stus[i].getName(), stus[i]);
188 }
189
190 Collection ss=map.values();
191 Iterator it=ss.iterator();
192 while(it.hasNext()){
193 System.out.println(it.next());
194 }
195 }
196
197 //10.hashMap遍历输出通过values
198 @Test
199 public void test10(){
200 Map map=new HashMap();
201 for(int i=0;i<stus.length;i++){
202 map.put(stus[i].getName(), stus[i]);
203 }
204
205 Collection ss=map.values();
206 Iterator it=ss.iterator();
207 while(it.hasNext()){
208 System.out.println(it.next());
209 }
210 }
211
212 //11.键值对Entry遍历
213 @Test
214 public void test11(){
215 Map<String,Student> map=new HashMap<>();
216 for(int i=0;i<stus.length;i++){
217 map.put(stus[i].getName(),stus[i]);
218 }
219
220 //遍历
221 for(Map.Entry<String, Student> entry:map.entrySet()){
222 System.out.println(entry.getKey()+"--------->"+entry.getValue());
223 }
224 }
225 }
View Code