功能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
将Map中每个Entry信息放入List对象
List集合转Map集合:Student类和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集合转List集合
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());
}
}
}