1. 程式人生 > >集合總結(全是代碼)----------java基礎學習

集合總結(全是代碼)----------java基礎學習

map() 讀取 簡單 util 技術分享 test 結構 dsa tag

前言:在剛學習的時候,切記不能粘貼復制,更不能眼高手低,再簡單的代碼,都要自己獨立動手寫。

第一步:目錄結構

技術分享

第二步:代碼區

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

集合總結(全是代碼)----------java基礎學習