Map和List间对象元素的相互转换
大二上学期学习Java整整半年,不得不说有了C++的基础再来看面向对象的程序设计真的很容易上手,Java、Python都是如此,但也知道离真正的熟练还有很长的路要走。
直接上代码吧,自己的Coding习惯很奇怪,注释很少,但是可读性的确很高。。
/*
Coding by NebulaChien 2021 12 17
IDE Eclipse
"If you gonna do something,then do it all the way"
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class Solution {
Solution(){
}
public class Student{
String StuName;
String StuSex;
int StuAge;
String StuCode;
double StuScore;
public void Init() {
Scanner sc=new Scanner(System.in);
System.out.print("姓名:");
this.StuName=sc.next();
System.out.print("性别:");
this.StuSex=sc.next();
System.out.print("年龄:");
this.StuAge=sc.nextInt();
System.out.print("编号:");
this.StuCode=sc.next();
System.out.print("成绩:");
this.StuScore=sc.nextDouble();
}
Student(){
System.out.println("开始初始化信息");
Init();
System.out.println("信息初始化完毕");
}
Student(String name,String sex,int age,String code,double score){
this.StuName=name;
this.StuSex=sex;
this.StuAge=age;
this.StuCode=code;
this.StuScore=score;
}
public String toString() {
return this.StuName+" "+this.StuSex+" "+this.StuAge+" "+this.StuCode+" "+this.StuScore;
}
//获取Map的Key-Value算法
public String StudentKey() {
return this.StuCode;
}
public Object StudentValue() {
Student Temp=this;
return Temp;
}
}
public static Map ListToMap() {
System.out.println("独立执行List到Map转换");
List<Student> StudentList=new ArrayList<Student>();
Map<String,Student> StudentMap=new HashMap<String,Student>();
System.out.println("初始化对象元素");
Solution newS=new Solution();
Student newStu1=newS.new Student();
Student newStu2=newS.new Student();
Student newStu3=newS.new Student();
Student newStu4=newS.new Student();
Student newStu5=newS.new Student();
System.out.println("初始化成功");
System.out.println("开始将这部分元素逐个存入List");
StudentList.add(newStu1);
StudentList.add(newStu2);
StudentList.add(newStu3);
StudentList.add(newStu4);
StudentList.add(newStu5);
System.out.println("存入成功");
System.out.println("开始转入Map");
for(int Index=0;Index<StudentList.size();Index++) {
StudentMap.put(StudentList.get(Index).StuCode, StudentList.get(Index));
}
System.out.println("转入成功");
//用不了Iterator
return StudentMap;
}
public static List MapToList() {
System.out.println("独立执行Map到List转换");
Map<String,Student> StudentMap=new HashMap<String,Student>();
List<Student> StudentList=new ArrayList<Student>();
System.out.println("初始化对象元素");
Solution newS=new Solution();
Student newStu1=newS.new Student();
Student newStu2=newS.new Student();
Student newStu3=newS.new Student();
Student newStu4=newS.new Student();
Student newStu5=newS.new Student();
System.out.println("初始化成功");
System.out.println("开始将这部分元素逐个存入Map");
StudentMap.put(newStu1.StuCode, newStu1);
StudentMap.put(newStu2.StuCode, newStu2);
StudentMap.put(newStu3.StuCode, newStu3);
StudentMap.put(newStu4.StuCode, newStu4);
StudentMap.put(newStu5.StuCode, newStu5);
System.out.println("存入成功");
System.out.println("开始转入List");
Set<String> KeySet=StudentMap.keySet();
for(String StuCode:KeySet) {
StudentList.add(StudentMap.get(StuCode));
}
System.out.println("转入成功");
return StudentList;
}
public static void main(String args[]) {
//创建存放Student对象的List和Map
List<Object> StudentList=new ArrayList<Object>();
Map<String,Object> StudentMap=new HashMap<String,Object>();
Solution S=new Solution();
Student Stu1=S.new Student();
Student Stu2=S.new Student();
Student Stu3=S.new Student();
Student Stu4=S.new Student();
Student Stu5=S.new Student();
System.out.println("将Student对象存入StudentList");
StudentList.add(Stu1);
StudentList.add(Stu2);
StudentList.add(Stu3);
StudentList.add(Stu4);
StudentList.add(Stu5);
System.out.println("存入成功");
System.out.println("将Student对象存入StudentMap");
StudentMap.put(Stu1.StudentKey(),Stu1);
StudentMap.put(Stu2.StudentKey(),Stu2);
StudentMap.put(Stu3.StudentKey(),Stu3);
StudentMap.put(Stu4.StudentKey(),Stu4);
StudentMap.put(Stu5.StudentKey(),Stu5);
System.out.println("存入成功");
System.out.println("StudentList中存放的对象元素:");
for(Iterator<Object> ListIterator=StudentList.iterator();ListIterator.hasNext();) {
Object ListElement=(Student)ListIterator.next();
System.out.println(ListElement);
}
System.out.println("StudentList元素个数:"+StudentList.size());
System.out.println("StudentMap中存放的对象元素:");
Set<Entry<String,Object>> MapSet=StudentMap.entrySet();
for(Iterator MapIterator=MapSet.iterator();MapIterator.hasNext();) {
Entry TempElement=(Entry)MapIterator.next();
System.out.println("编号"+TempElement.getKey()+"对应的Student信息为: "+TempElement.getValue());
}
System.out.println("StudentMap中存放的元素个数:"+StudentMap.size());
System.out.println("以上部分为单独将Student元素放入List/Map中");
Map<String,Student> NewMap1=new HashMap<String,Student>();
NewMap1=ListToMap();
System.out.println("转换后得到的Map:");
Set<Entry<String,Object>> newMapSet=StudentMap.entrySet();
for(Iterator MapIterator=newMapSet.iterator();MapIterator.hasNext();) {
Entry TempElement=(Entry)MapIterator.next();
System.out.println("编号"+TempElement.getKey()+"对应的Student信息为: "+TempElement.getValue());
}System.out.println("转换后Map大小:"+NewMap1.size());
List<Student> NewList2=new ArrayList<Student>();
NewList2=MapToList();
System.out.println("转换后得到的List:");
for(Iterator newListIterator=NewList2.iterator();newListIterator.hasNext();) {
Student Temp=(Student)newListIterator.next();
System.out.println(Temp);
}
System.out.println("转换后Lis大小:"+NewList2.size());
}
}
运行结果:
可能以后还是得改改这个毛病:简单的问题复杂化,又想把一个复杂的问题简单的解释出来。以后搞硬件这个真的很要命。晚安,祝各位考四六级的同学们取得优异的成绩。