题目:
List list = Arrays.asList(new String[]{“1,张三,50”, “2,李四,40”, “3,王五,60”, “4,小红,20”, “5,辣鸡,10”})。
里面数据分别是对应id,名字,分数,不需要连接数据库。
要求:按分数升序,输出名字和分数
public class weitwoApplication {
public static void main(String[] args) {
//对这个list操作,不需要连接数据库
List<String> list = Arrays.asList(new String[]{"1,张三,50", "2,李四,40", "3,王五,60", "4,小红,20", "5,辣鸡,10"});
}
}
如:
辣鸡10
小红20
李四40
张三50
王五60
答案:
public class weitwoApplication {
public static void main(String[] args) {
List<String> list = Arrays.asList(new String[]{"1,张三,50", "2,李四,40", "3,王五,60", "4,小红,20", "5,辣鸡,10"});
List<student> result = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
String[] re = list.get(i).split(",");
student s = new student();
s.setName(re[1]);
s.setScore(re[2]);
result.add(s);
}
result.stream().sorted(Comparator.comparing(student::getScore)).forEach(student -> {
System.out.print(student.getName());
System.out.print(student.getScore());
System.out.println();
});
}
}
class student {
public int id;
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 String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public String name;
public String score;
}
嘻嘻:你还有更好的办法解决吗?