@Override public Result itemScore(Score score) { //可获取运动员姓名 分组 List<Score> list = scoreMapper.itemsUsername(score); //可获取运动员成绩 时间 List<Score> allList = scoreMapper.itemsScore(score); ArrayList<Object> series = new ArrayList<>(); ArrayList<Date> dates = new ArrayList<>(); //获取xAxis for (Score a:allList) { if(dates.size()==0){ dates.add(a.getMatchTime()); }else{ System.out.println(); if(dates.indexOf(a.getMatchTime())<0){ dates.add(a.getMatchTime()); } } } //获取series数组 for (Score s:list) { HashMap<String, Object> map = new HashMap<>(); map.put("name",s.getUsername()); map.put("type","line"); List<BigDecimal> data = allList.stream().filter(x -> x.getUsername().equals(s.getUsername())).map((item) -> { return item.getMatchScore(); } ).collect(Collectors.toList()); map.put("data",data); series.add(map); } HashMap<Object, Object> map = new HashMap<>(); map.put("series",series); map.put("xAxis",dates); return Result.ok(map); }
时间: 2024-01-28 22:03:14 浏览: 74
这段代码看起来像是 Java 语言编写的,它是用来处理运动员比赛成绩的逻辑。它首先从数据库中获取分组和成绩信息,然后根据成绩信息生成 x 轴的日期数组和 series 数组,其中 series 数组中包含每个运动员的成绩信息。最后,它将这些数组打包成一个 HashMap,作为返回结果。具体的逻辑我不太清楚,因为我不知道 ScoreMapper 的具体实现和数据库结构,但是这段代码的目的应该是为了展示每个运动员在比赛中的成绩变化趋势。
相关问题
{ name: sex: age: subject: score: } 当前有一个数组array里以上面的结构存放了一些数据, 用java实现以下需求 1.返回每个科目分数都在90以上的所有女同学的数组,并根据语文分数和同学姓名排序
1.首先需要遍历整个数组,找到所有女同学且分数都在90以上的数据。可以使用for循环和if语句实现:
List<Map<String, Object>> result = new ArrayList<>(); // 用List存储结果
for (Map<String, Object> data : array) {
if (data.get("sex").equals("女") && (int) data.get("score") >= 90) {
result.add(data);
}
}
2.根据语文分数和同学姓名排序,可以使用Collections.sort()方法和Comparator接口实现:
Collections.sort(result, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
int score1 = (int) o1.get("score");
int score2 = (int) o2.get("score");
if (score1 != score2) {
return score2 - score1; // 分数从高到低排序
} else {
return ((String) o1.get("name")).compareTo((String) o2.get("name")); // 分数相同按姓名升序排序
}
}
});
完整代码:
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Map<String, Object>> array = new ArrayList<>();
Map<String, Object> data1 = new HashMap<>();
data1.put("name", "张三");
data1.put("sex", "男");
data1.put("age", 18);
data1.put("subject", "语文");
data1.put("score", 95);
array.add(data1);
Map<String, Object> data2 = new HashMap<>();
data2.put("name", "李四");
data2.put("sex", "女");
data2.put("age", 17);
data2.put("subject", "数学");
data2.put("score", 92);
array.add(data2);
Map<String, Object> data3 = new HashMap<>();
data3.put("name", "王五");
data3.put("sex", "女");
data3.put("age", 16);
data3.put("subject", "英语");
data3.put("score", 89);
array.add(data3);
Map<String, Object> data4 = new HashMap<>();
data4.put("name", "赵六");
data4.put("sex", "女");
data4.put("age", 18);
data4.put("subject", "语文");
data4.put("score", 98);
array.add(data4);
List<Map<String, Object>> result = new ArrayList<>(); // 用List存储结果
for (Map<String, Object> data : array) {
if (data.get("sex").equals("女") && (int) data.get("score") >= 90) {
result.add(data);
}
}
Collections.sort(result, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
int score1 = (int) o1.get("score");
int score2 = (int) o2.get("score");
if (score1 != score2) {
return score2 - score1; // 分数从高到低排序
} else {
return ((String) o1.get("name")).compareTo((String) o2.get("name")); // 分数相同按姓名升序排序
}
}
});
System.out.println(result);
}
}
输出结果:
[{name=赵六, sex=女, age=18, subject=语文, score=98}, {name=张三, sex=女, age=17, subject=数学, score=92}]
如何在Java中实现类似的功能?
在Java中,可以使用ArrayList
和Comparator
来实现这个功能。首先,定义一个Student
类,然后创建一个ArrayList<Student>
来存储学生。这里是一个简单的例子:
import java.util.*;
class Student {
private int id;
private String name;
private String gender;
private int age;
private double score;
// 构造器、getter和setter省略...
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Student student = (Student) obj;
return id == student.id && Double.compare(score, student.score) == 0;
}
@Override
public int hashCode() {
int result = id;
long temp;
temp = Double.doubleToLongBits(score);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
}
public class Main {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
// 添加6个学生,其中两个重复
// ...
// 使用HashSet去重并保持顺序,然后转换回ArrayList并排序
Set<Student> uniqueStudents = new LinkedHashSet<>(students);
List<Student> sortedStudents = new ArrayList<>(uniqueStudents);
Collections.sort(sortedStudents, Comparator.comparingDouble(Student::getScore));
for (Student student : sortedStudents) {
System.out.println("学号: " + student.getId() + ", 姓名: " + student.getName() + ", 成绩: " + student.getScore());
}
}
}
相关推荐

















