master
面包骑士 2024-08-08 09:16:54 +08:00
commit bd6c81f3f8
5 changed files with 165 additions and 0 deletions

35
.gitignore vendored 100644
View File

@ -0,0 +1,35 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

24
pom.xml 100644
View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bw</groupId>
<artifactId>day19</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,22 @@
package com.bw.domain;
import lombok.*;
/**
* @Author:
* @Name: StuAvg
* @Description:
* @CreatedDate: 2024/8/8 8:41
* @FilePath: com.bw.domain
*/
@Data
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class StuAvg {
private String name;
private Double score;
private Integer num;
}

View File

@ -0,0 +1,23 @@
package com.bw.domain;
import lombok.*;
/**
* @Author:
* @Name: Student
* @Description:
* @CreatedDate: 2024/8/8 8:37
* @FilePath: com.bw.domain
*/
@Data
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private Integer id;
private String name;
private String subjects;
private Double score;
}

View File

@ -0,0 +1,61 @@
package com.bw;
/**
* @Author:
* @Name: day19
* @Description: 19
* @CreatedDate: 2024/8/8 8:36
* @FilePath: com.bw
*/
import com.bw.domain.StuAvg;
import com.bw.domain.Student;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @Author:
* @Name: day19
* @Description: 19
* @CreatedDate: 2024/8/8 8:36
* @FilePath: com.bw
*/
public class day19 {
public static void main(String[] args) {
// 学生成绩
List<Student> students = new ArrayList<>();
students.add(new Student(1, "张三","语文", 90.0));
students.add(new Student(2, "张三","英语", 55.0));
students.add(new Student(3, "张三","数学", 99.0));
students.add(new Student(4, "李四","语文", 20.0));
students.add(new Student(5, "李四","英语", 12.0));
students.add(new Student(6, "李四","数学", 43.0));
// 使用stream流获取学生成绩
HashMap<String,Double> hmap = new HashMap<>();
students.stream().sorted().map(stu -> {
int n = 0;
for (String key : hmap.keySet()) {
if (stu.getName().equals(key)){
n = 1;
}
}
if (n == 0){
hmap.put(stu.getName(),stu.getScore());
}else{
hmap.put(stu.getName(),hmap.get(stu.getName())+stu.getScore());
}
return stu;
});
// 输出学生成绩
hmap.forEach((k,v) -> {
System.out.println(k + " " + v/3);
});
}
}