10 changed files with 142 additions and 0 deletions
@ -0,0 +1,8 @@ |
|||||
|
# Default ignored files |
||||
|
/shelf/ |
||||
|
/workspace.xml |
||||
|
# Datasource local storage ignored files |
||||
|
/../../../../:\java project\mergedemo\.idea/dataSources/ |
||||
|
/dataSources.local.xml |
||||
|
# Editor-based HTTP Client requests |
||||
|
/httpRequests/ |
@ -0,0 +1,14 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="CompilerConfiguration"> |
||||
|
<annotationProcessing> |
||||
|
<profile default="true" name="Default" enabled="true" /> |
||||
|
<profile name="Maven default annotation processors profile" enabled="true"> |
||||
|
<sourceOutputDir name="target/generated-sources/annotations" /> |
||||
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" /> |
||||
|
<outputRelativeToContentRoot value="true" /> |
||||
|
<module name="mergedemo" /> |
||||
|
</profile> |
||||
|
</annotationProcessing> |
||||
|
</component> |
||||
|
</project> |
@ -0,0 +1,20 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="RemoteRepositoriesConfiguration"> |
||||
|
<remote-repository> |
||||
|
<option name="id" value="central" /> |
||||
|
<option name="name" value="Central Repository" /> |
||||
|
<option name="url" value="https://repo.maven.apache.org/maven2" /> |
||||
|
</remote-repository> |
||||
|
<remote-repository> |
||||
|
<option name="id" value="central" /> |
||||
|
<option name="name" value="Maven Central repository" /> |
||||
|
<option name="url" value="https://repo1.maven.org/maven2" /> |
||||
|
</remote-repository> |
||||
|
<remote-repository> |
||||
|
<option name="id" value="jboss.community" /> |
||||
|
<option name="name" value="JBoss Community repository" /> |
||||
|
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" /> |
||||
|
</remote-repository> |
||||
|
</component> |
||||
|
</project> |
@ -0,0 +1,14 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" /> |
||||
|
<component name="MavenProjectsManager"> |
||||
|
<option name="originalFiles"> |
||||
|
<list> |
||||
|
<option value="$PROJECT_DIR$/pom.xml" /> |
||||
|
</list> |
||||
|
</option> |
||||
|
</component> |
||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK"> |
||||
|
<output url="file://$PROJECT_DIR$/out" /> |
||||
|
</component> |
||||
|
</project> |
@ -0,0 +1,2 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<module type="JAVA_MODULE" version="4" /> |
@ -0,0 +1,20 @@ |
|||||
|
<?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.woniu</groupId> |
||||
|
<artifactId>mergedemo</artifactId> |
||||
|
<version>1.0-SNAPSHOT</version> |
||||
|
|
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>org.projectlombok</groupId> |
||||
|
<artifactId>lombok</artifactId> |
||||
|
<version>1.18.24</version> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
|
||||
|
|
||||
|
</project> |
@ -0,0 +1,52 @@ |
|||||
|
package com.woniu.mergedemo; |
||||
|
|
||||
|
|
||||
|
import java.util.*; |
||||
|
|
||||
|
/** |
||||
|
* Java 8 中 Map的一个骚操作!好用到爆!! |
||||
|
* |
||||
|
* 假设我们有这么一段业务逻辑, |
||||
|
* 我有一个学生成绩对象的列表, |
||||
|
* 对象包含学生姓名、科目、科目分数三个属性, |
||||
|
* 要求求得每个学生的总成绩。 |
||||
|
*/ |
||||
|
public class MainTest { |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
List<StudentScore> studentScoreList = Arrays.asList( |
||||
|
new StudentScore("张三", "语文", 70), |
||||
|
new StudentScore("张三", "数学", 70), |
||||
|
new StudentScore("张三", "英语", 65), |
||||
|
new StudentScore("李四", "语文", 68), |
||||
|
new StudentScore("李四", "数学", 70), |
||||
|
new StudentScore("李四", "英语", 90), |
||||
|
new StudentScore("王五", "语文", 80), |
||||
|
new StudentScore("王五", "数学", 85), |
||||
|
new StudentScore("王五", "英语", 70) |
||||
|
); |
||||
|
|
||||
|
Map<String, Integer> studentScoreMap = new HashMap<>(); |
||||
|
studentScoreList.forEach(studentScore -> { |
||||
|
if (studentScoreMap.containsKey(studentScore.getStuName())) { |
||||
|
studentScoreMap.put(studentScore.getStuName(), studentScoreMap.get(studentScore.getStuName()) + studentScore.getScore()); |
||||
|
} else { |
||||
|
studentScoreMap.put(studentScore.getStuName(), studentScore.getScore()); |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
|
||||
|
Map<String, Integer> studentScoreMap2 = new HashMap<>(); |
||||
|
|
||||
|
studentScoreList.forEach(studentScore -> studentScoreMap2.merge( |
||||
|
studentScore.getStuName(), |
||||
|
studentScore.getScore(), |
||||
|
(oldValue, newValue) -> oldValue + newValue)); |
||||
|
//merge() 方法会先判断指定的 key 是否存在,如果不存在,则添加键值对到 hashMap 中。
|
||||
|
|
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,12 @@ |
|||||
|
package com.woniu.mergedemo; |
||||
|
|
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
@AllArgsConstructor |
||||
|
public class StudentScore { |
||||
|
private String stuName; |
||||
|
private String subject; |
||||
|
private int score; |
||||
|
} |
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue