ListとMapの相互変換
5716 ワード
ListとMapデータの変換を実現します.具体的な要求は以下の通りである.
機能1:定義方法public void listToMap(){}リスト中のStudio要素をMapにカプセル化1)構造方法Student(int id,String name,int age,String sex)を用いて複数の学生情報を作成してList 2に参加)リストを巡り、各Student情報3を出力)リスト中のデータを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がMapの各Entry情報をリストオブジェクトに入れる
Listコレクション転送Mapコレクション:StudioクラスとTest_StudentクラスStudentクラス:
Test_Studentクラス
以上がListコレクション転送Mapコレクション
次は、Mapコレクション転送リストコレクションStudentEntryクラスです.
Test_StudentEntryクラス
機能1:定義方法public void listToMap(){}リスト中のStudio要素をMapにカプセル化1)構造方法Student(int id,String name,int age,String sex)を用いて複数の学生情報を作成してList 2に参加)リストを巡り、各Student情報3を出力)リスト中のデータを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がMapの各Entry情報をリストオブジェクトに入れる
Listコレクション転送Mapコレクション:StudioクラスとTest_StudentクラスStudentクラス:
package Day610.Practice.Demo_ListAndMap;
public class Student {
private int id;
private String name;
private int age;
private String sex;
public Student() {
}
public Student(int id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void mapToList(){}
}
Test_Studentクラス
package Day610.Practice.Demo_ListAndMap;
import java.util.*;
public class Test_Student {
public static void main(String[] args) {
listToMap();//
}
public static void listToMap(){
Student s1 = new Student(1001, "zs", 22, " ");
Student s2 = new Student(1002, "ls", 23, " ");
Student s3 = new Student(1003, "ww", 21, " ");
Student s4 = new Student(1004, "zl", 20, " ");
Student s5 = new Student(1005, "eh", 24, " ");
List list = new ArrayList<>();// List
Map map = new HashMap<>();// Map
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
// List
System.out.println("List ");
for (Student ss1 : list) {// for
System.out.println(ss1.getId()+","+ss1.getName()+","+ss1.getAge()+","+ss1.getSex());
// List , Map
map.put(ss1.getId(),ss1);
}
System.out.println("Map ");
Set> entries = map.entrySet();//Map entrySet K,V, Set
for (Map.Entry m:entries){// for
System.out.println(m.getKey()+","+m.getValue().getName()+","+m.getValue().getAge()+","+m.getValue().getSex());
}
}
}
以上がListコレクション転送Mapコレクション
次は、Mapコレクション転送リストコレクションStudentEntryクラスです.
package Day610.Practice.Demo_ListAndMap;
public class StudentEntry {
private int id;
private String name;
private int age;
private String sex;
public StudentEntry() {
}
public StudentEntry(int id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
Test_StudentEntryクラス
package Day610.Practice.Demo_ListAndMap;
import java.util.*;
public class Test_StudentEntry {
public static void main(String[] args) {
mapToList();
}
public static void mapToList(){
StudentEntry s1 = new StudentEntry(1001, "zs", 22, " ");
StudentEntry s2 = new StudentEntry(1002, "ls", 23, " ");
StudentEntry s3 = new StudentEntry(1003, "ww", 21, " ");
StudentEntry s4 = new StudentEntry(1004, "zl", 20, " ");
StudentEntry s5 = new StudentEntry(1005, "eh", 24, " ");
Map map = new HashMap<>();// Map
map.put(s1.getId(),s1);
map.put(s2.getId(),s2);
map.put(s3.getId(),s3);
map.put(s4.getId(),s4);
map.put(s5.getId(),s5);
List list = new ArrayList<>();// List
Set> entries = map.entrySet();//Map entrySet K,V, Set
System.out.println("Map ");
for (Map.Entry m:entries){// for
System.out.println(m.getKey()+","+m.getValue().getName()+","+m.getValue().getAge()+","+m.getValue().getSex());
// map , List
list.add(m.getValue());
}
System.out.println("List ");
for (StudentEntry s:list){// for
System.out.println(s.getId()+","+s.getName()+","+s.getAge()+","+s.getSex());
}
}
}