求大神帮帮忙,用java的list map方法写
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* TODO类描述
*
* @author liuteng
* @version V1.0 2016-07-15
* @createDate 2016-07-15 10:27:29
* @modifyDate
*/
public class yuan3 {
/**
* 学生信息对象
*
* @return
*/
private static List<StudentInfo> initScoreList() {
List<StudentInfo> studentInfoList = new ArrayList<StudentInfo>();
studentInfoList.add(new StudentInfo("A", 1, 90));
studentInfoList.add(new StudentInfo("A", 2, 80));
studentInfoList.add(new StudentInfo("A", 3, 92));
studentInfoList.add(new StudentInfo("B", 1, 78));
studentInfoList.add(new StudentInfo("B", 2, 93));
studentInfoList.add(new StudentInfo("B", 3, 69));
studentInfoList.add(new StudentInfo("C", 1, 80));
studentInfoList.add(new StudentInfo("C", 2, 82));
studentInfoList.add(new StudentInfo("C", 3, 93));
return studentInfoList;
}
/**
* 学生对象
*
* @return
*/
private static List<Student> initNameList() {
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student(1, "sam"));
studentList.add(new Student(2, "mike"));
studentList.add(new Student(3, "tom"));
return studentList;
}
/**
* 处理过程
*
* @param nameList
* @param scoreList
* @return
*/
private static List process(List<Student> nameList, List<StudentInfo> scoreList) {
List result = new ArrayList();
Map<StudentInfo, Student> map = new HashMap<StudentInfo, Student>();
for (StudentInfo studentInfo : scoreList) {
List<Student> tmpStudentInfo = new ArrayList<Student>();
for (Student student : nameList) {
if (student.getId() - studentInfo.getId() == 0) {
tmpStudentInfo.add(student);
map.put(studentInfo, student);//关联两个对象
}
}
}
Collections.sort(scoreList);//排序
Map<String, String> stringMap = new HashMap<String, String>();
for (StudentInfo studentInfo : map.keySet()) {
stringMap.put(studentInfo.getCourse(), (stringMap.get(studentInfo.getCourse()) == null ? "" : stringMap.get(studentInfo.getCourse()) + ",") + map.get(studentInfo).getName());
}
for (String s : stringMap.keySet()) {
result.add(s + ",\"" + stringMap.get(s) + "\"");
}
return result;
}
public static void main(String[] args) {
List<StudentInfo> scoreList = initScoreList();
List<Student> nameList = initNameList();
List result = process(nameList, scoreList);
System.out.println(result);
}
}
/**
* 学生信息对象类
*/
class StudentInfo implements Comparable<StudentInfo> {
private String course;
private int id;
private int grade;
public StudentInfo(String course, int id, int grade) {
this.course = course;
this.id = id;
this.grade = grade;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
@Override
public int compareTo(StudentInfo o) {
return this.getGrade() - o.getGrade();
}
}
/**
* 学生对象类
*/
class Student {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
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;
}
}
0.0这么多?