1. 程式人生 > >java集合(List集合與Map集合的數據轉換)

java集合(List集合與Map集合的數據轉換)

string stat 多個 遍歷 使用 iterator 類型 ron map.entry

List集合與Map集合的數據轉換  

  實現List和Map數據的轉換。

    具體要求如下:

    功能1:定義方法public void listToMap( ){ }將List中Student元素封裝到Map中

        1)使用構造方法Student(int id,String name,int age,String sex )創建多個學生信息並加入List

        2) 遍歷List,輸出每個Student信息

        3) 將List中數據放入Map,使用Student的id屬性作為key,使用Student對象信息作為value

        4) 遍歷Map,輸出每個Entry的key和value

    功能2:定義方法public void mapToList( ){ }將Map中Student映射信息封裝到List

        1)創建實體類StudentEntry,可以存儲Map中每個Entry的信息

        2) 使用構造方法Student(int id,String name,int age,String sex )創建多個學生信息,並使用Student的id屬性作為key,存入Map

        3)創建List對象,每個元素類型是StudentEntry

        4)將Map中每個Entry信息放入List對象

一,創建學生對象

 1 public class Student_3 {
 2 /**
 3  * 使用構造方法Student(int id,String name,int age,String sex )
 4  */
 5     private int id;
 6     private String name;
 7     private int age;
 8     private String sex;
 9 
10     public Student_3() {
11     }
12 
13     public Student_3(int id, String name, int age, String sex) {
14 this.id = id; 15 this.name = name; 16 this.age = age; 17 this.sex = sex; 18 } 19 20 public int getId() { 21 return id; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public int getAge() { 29 return age; 30 } 31 32 public String getSex() { 33 return sex; 34 } 35 36 public void setId(int id) { 37 this.id = id; 38 } 39 40 public void setName(String name) { 41 this.name = name; 42 } 43 44 public void setAge(int age) { 45 this.age = age; 46 } 47 48 public void setSex(String sex) { 49 this.sex = sex; 50 } 51 52 @Override 53 public String toString() { 54 return "["+id +" "+ name +" "+ age +" "+ sex+"]"; 55 } 56 57 58 }

  二,創建測試類

 1 import java.util.ArrayList;
 2 import java.util.HashMap;
 3 import java.util.Iterator;
 4 import java.util.List;
 5 import java.util.Map;
 6 import java.util.Map.Entry;
 7 import java.util.Set;
 8 
 9 
10 public class Test_3 {
11     //List集合轉Map集合
12     public void listToMap(Student_3 s1,Student_3 s2 ){ 
13         //1.創建List集合
14         List<Student_3> list = new ArrayList<>();
15         //2.創建Map集合
16         Map<Integer, Student_3> map = new HashMap<>();
17         //3.傳入學生對象
18         list.add(s1);
19         list.add(s2);
20         Iterator<Student_3> it = list.iterator();
21         //4.遍歷List集合
22         while(it.hasNext()){
23             Student_3 stu = it.next();
24             //5.將學生id作為Map集合的Key,學生對象作為Map集合的Vaul
25             map.put(stu.getId(), stu);
26         }
27         //6.遍歷Map集合
28         Set<Entry<Integer, Student_3>> entrySet = map.entrySet();
29         for (Entry<Integer, Student_3> en : entrySet) {
30             System.out.println(en);
31         }
32         
33     }
34     
35     //Map集合轉List集合
36     public void mapToList(Student_3 s3,Student_3 s4){
37         //1.創建Map集合
38         Map< Integer, Student_3> map = new HashMap<>();
39         //2.創建List集合
40         List<Student_3> list = new ArrayList<>();
41         //3.將學生id作為Map集合的Key,學生對象作為Map集合的Vaul
42         map.put(s3.getId(),s3);
43         map.put(s4.getId(),s4);
44         //4.遍歷Map集合
45         Set<Entry<Integer, Student_3>> entrySet = map.entrySet();
46         for (Entry<Integer, Student_3> e : entrySet) {
47             //5.將Map集合的值加入到Liat集合中
48             list.add(e.getValue());
49         }
50         //6.遍歷List集合
51         Iterator<Student_3> it = list.iterator();
52         while(it.hasNext()){
53             System.out.println(it.next());
54         }
55         
56     }
57     public static void main(String[] args) {
58         //創建四個學生對象
59         Student_3 s1 = new Student_3(101, "張三", 25, "男");
60         Student_3 s2 = new Student_3(102, "馬琴", 25, "女");
61         Student_3 s3 = new Student_3(103, "趙薇", 36, "女");
62         Student_3 s4 = new Student_3(104, "李小梅", 31, "女");
63         //創建Test_3的實例
64         Test_3 t = new Test_3();
65         System.out.println("List集合轉Map集合");
66         //調用List集合轉Map集合方法並傳入學生對象s1,s2
67         t.listToMap(s1,s2);
68         System.out.println("\n");
69         System.out.println("Map集合轉List集合");
70         //調用Map集合轉List集合方法並傳入學生對象s3,s4
71         t.mapToList(s3, s4);
72     }
73 }

java集合(List集合與Map集合的數據轉換)