toMap函数 java.lang.IllegalStateException: Duplicate key 123 (attempted merging values 123 and 124)

Java8的stream流中的toMap函数

 @Test
    public void test() {
        User user = new User();
        user.setName("123");
        user.setPassword("123");
        User user1 = new User();
        user1.setName("123");
        user1.setPassword("124");
        User user2 = new User();
        user2.setName("124");
        user2.setPassword("125");
        User user3 = new User();
        user3.setName("125");
        user3.setPassword("125");
        List<User> users = new ArrayList<>();
        users.add(user);
        users.add(user1);
        users.add(user2);
        users.add(user3);
        Map<String, List<User>> userMap = users.stream().collect(Collectors.toMap(User::getName, p -> {
            List<User> list = new ArrayList<>();
            list.add(user);
            return list;
        }, (List<User> value1, List<User> value2) -> {
            value1.addAll(value2);
            return value1;
        }));
        System.out.println(userMap.toString());
    }
}
"C:\Program Files\Java\jdk-11.0.5\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:E:\sofe\Idea\IntelliJ IDEA 2019.2.4\lib\idea_rt.jar=63858:E:\sofe\Idea\IntelliJ IDEA 2019.2.4\bin" -Dfile.encoding=UTF-8 -classpath "E:\sofe\Idea\IntelliJ IDEA 2019.2.4\lib\idea_rt.jar;E:\sofe\Idea\IntelliJ IDEA 2019.2.4\plugins\junit\lib\junit-rt.jar;E:\sofe\Idea\IntelliJ IDEA 2019.2.4\plugins\junit\lib\junit5-rt.jar;E:\sofe\maven\maven-repository-20191031\org\junit\platform\junit-platform-launcher\1.5.2\junit-platform-launcher-1.5.2.jar;E:\sofe\maven\maven-repository-20191031\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;E:\sofe\maven\maven-repository-20191031\org\junit\platform\junit-platform-engine\1.5.2\junit-platform-engine-1.5.2.jar;E:\sofe\maven\maven-repository-20191031\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;E:\sofe\maven\maven-repository-20191031\org\junit\platform\junit-platform-commons\1.5.2\junit-platform-commons-1.5.2.jar;E:\demo\target\test-classes;E:\demo\target\classes;E:\sofe\maven\maven-repository-20191031\org\springframework\boot\spring-boot-starter-security\2.2.2.RELEASE\spring-boot-starter-security-2.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\boot\spring-boot-starter\2.2.2.RELEASE\spring-boot-starter-2.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\boot\spring-boot\2.2.2.RELEASE\spring-boot-2.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\boot\spring-boot-autoconfigure\2.2.2.RELEASE\spring-boot-autoconfigure-2.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\boot\spring-boot-starter-logging\2.2.2.RELEASE\spring-boot-starter-logging-2.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;E:\sofe\maven\maven-repository-20191031\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;E:\sofe\maven\maven-repository-20191031\org\apache\logging\log4j\log4j-to-slf4j\2.12.1\log4j-to-slf4j-2.12.1.jar;E:\sofe\maven\maven-repository-20191031\org\apache\logging\log4j\log4j-api\2.12.1\log4j-api-2.12.1.jar;E:\sofe\maven\maven-repository-20191031\org\slf4j\jul-to-slf4j\1.7.29\jul-to-slf4j-1.7.29.jar;E:\sofe\maven\maven-repository-20191031\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;E:\sofe\maven\maven-repository-20191031\org\yaml\snakeyaml\1.25\snakeyaml-1.25.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\spring-aop\5.2.2.RELEASE\spring-aop-5.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\spring-beans\5.2.2.RELEASE\spring-beans-5.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\security\spring-security-config\5.2.1.RELEASE\spring-security-config-5.2.1.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\spring-context\5.2.2.RELEASE\spring-context-5.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\security\spring-security-web\5.2.1.RELEASE\spring-security-web-5.2.1.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\spring-expression\5.2.2.RELEASE\spring-expression-5.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\boot\spring-boot-starter-web\2.2.2.RELEASE\spring-boot-starter-web-2.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\boot\spring-boot-starter-json\2.2.2.RELEASE\spring-boot-starter-json-2.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.10.1\jackson-datatype-jdk8-2.10.1.jar;E:\sofe\maven\maven-repository-20191031\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.10.1\jackson-datatype-jsr310-2.10.1.jar;E:\sofe\maven\maven-repository-20191031\com\fasterxml\jackson\module\jackson-module-parameter-names\2.10.1\jackson-module-parameter-names-2.10.1.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\boot\spring-boot-starter-tomcat\2.2.2.RELEASE\spring-boot-starter-tomcat-2.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\apache\tomcat\embed\tomcat-embed-core\9.0.29\tomcat-embed-core-9.0.29.jar;E:\sofe\maven\maven-repository-20191031\org\apache\tomcat\embed\tomcat-embed-el\9.0.29\tomcat-embed-el-9.0.29.jar;E:\sofe\maven\maven-repository-20191031\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.29\tomcat-embed-websocket-9.0.29.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\boot\spring-boot-starter-validation\2.2.2.RELEASE\spring-boot-starter-validation-2.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\jakarta\validation\jakarta.validation-api\2.0.1\jakarta.validation-api-2.0.1.jar;E:\sofe\maven\maven-repository-20191031\org\hibernate\validator\hibernate-validator\6.0.18.Final\hibernate-validator-6.0.18.Final.jar;E:\sofe\maven\maven-repository-20191031\org\jboss\logging\jboss-logging\3.4.1.Final\jboss-logging-3.4.1.Final.jar;E:\sofe\maven\maven-repository-20191031\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\spring-web\5.2.2.RELEASE\spring-web-5.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\spring-webmvc\5.2.2.RELEASE\spring-webmvc-5.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\mybatis\spring\boot\mybatis-spring-boot-starter\2.1.1\mybatis-spring-boot-starter-2.1.1.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\boot\spring-boot-starter-jdbc\2.2.2.RELEASE\spring-boot-starter-jdbc-2.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\com\zaxxer\HikariCP\3.4.1\HikariCP-3.4.1.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\spring-jdbc\5.2.2.RELEASE\spring-jdbc-5.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\2.1.1\mybatis-spring-boot-autoconfigure-2.1.1.jar;E:\sofe\maven\maven-repository-20191031\org\mybatis\mybatis\3.5.3\mybatis-3.5.3.jar;E:\sofe\maven\maven-repository-20191031\org\mybatis\mybatis-spring\2.0.3\mybatis-spring-2.0.3.jar;E:\sofe\maven\maven-repository-20191031\org\projectlombok\lombok\1.18.10\lombok-1.18.10.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\boot\spring-boot-starter-test\2.2.2.RELEASE\spring-boot-starter-test-2.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\boot\spring-boot-test\2.2.2.RELEASE\spring-boot-test-2.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\boot\spring-boot-test-autoconfigure\2.2.2.RELEASE\spring-boot-test-autoconfigure-2.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\com\jayway\jsonpath\json-path\2.4.0\json-path-2.4.0.jar;E:\sofe\maven\maven-repository-20191031\net\minidev\json-smart\2.3\json-smart-2.3.jar;E:\sofe\maven\maven-repository-20191031\net\minidev\accessors-smart\1.2\accessors-smart-1.2.jar;E:\sofe\maven\maven-repository-20191031\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;E:\sofe\maven\maven-repository-20191031\org\slf4j\slf4j-api\1.7.29\slf4j-api-1.7.29.jar;E:\sofe\maven\maven-repository-20191031\jakarta\xml\bind\jakarta.xml.bind-api\2.3.2\jakarta.xml.bind-api-2.3.2.jar;E:\sofe\maven\maven-repository-20191031\jakarta\activation\jakarta.activation-api\1.2.1\jakarta.activation-api-1.2.1.jar;E:\sofe\maven\maven-repository-20191031\org\junit\jupiter\junit-jupiter\5.5.2\junit-jupiter-5.5.2.jar;E:\sofe\maven\maven-repository-20191031\org\junit\jupiter\junit-jupiter-api\5.5.2\junit-jupiter-api-5.5.2.jar;E:\sofe\maven\maven-repository-20191031\org\junit\jupiter\junit-jupiter-params\5.5.2\junit-jupiter-params-5.5.2.jar;E:\sofe\maven\maven-repository-20191031\org\junit\jupiter\junit-jupiter-engine\5.5.2\junit-jupiter-engine-5.5.2.jar;E:\sofe\maven\maven-repository-20191031\org\mockito\mockito-junit-jupiter\3.1.0\mockito-junit-jupiter-3.1.0.jar;E:\sofe\maven\maven-repository-20191031\org\assertj\assertj-core\3.13.2\assertj-core-3.13.2.jar;E:\sofe\maven\maven-repository-20191031\org\hamcrest\hamcrest\2.1\hamcrest-2.1.jar;E:\sofe\maven\maven-repository-20191031\org\mockito\mockito-core\3.1.0\mockito-core-3.1.0.jar;E:\sofe\maven\maven-repository-20191031\net\bytebuddy\byte-buddy\1.10.4\byte-buddy-1.10.4.jar;E:\sofe\maven\maven-repository-20191031\net\bytebuddy\byte-buddy-agent\1.10.4\byte-buddy-agent-1.10.4.jar;E:\sofe\maven\maven-repository-20191031\org\objenesis\objenesis\2.6\objenesis-2.6.jar;E:\sofe\maven\maven-repository-20191031\org\skyscreamer\jsonassert\1.5.0\jsonassert-1.5.0.jar;E:\sofe\maven\maven-repository-20191031\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\spring-core\5.2.2.RELEASE\spring-core-5.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\spring-jcl\5.2.2.RELEASE\spring-jcl-5.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\spring-test\5.2.2.RELEASE\spring-test-5.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\xmlunit\xmlunit-core\2.6.3\xmlunit-core-2.6.3.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\security\spring-security-test\5.2.1.RELEASE\spring-security-test-5.2.1.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\security\spring-security-core\5.2.1.RELEASE\spring-security-core-5.2.1.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\mysql\mysql-connector-java\6.0.5\mysql-connector-java-6.0.5.jar;E:\sofe\maven\maven-repository-20191031\com\alibaba\druid\1.1.12\druid-1.1.12.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\boot\spring-boot-starter-data-redis\2.2.2.RELEASE\spring-boot-starter-data-redis-2.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\data\spring-data-redis\2.2.3.RELEASE\spring-data-redis-2.2.3.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\data\spring-data-keyvalue\2.2.3.RELEASE\spring-data-keyvalue-2.2.3.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\data\spring-data-commons\2.2.3.RELEASE\spring-data-commons-2.2.3.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\spring-tx\5.2.2.RELEASE\spring-tx-5.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\spring-oxm\5.2.2.RELEASE\spring-oxm-5.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\springframework\spring-context-support\5.2.2.RELEASE\spring-context-support-5.2.2.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\io\lettuce\lettuce-core\5.2.1.RELEASE\lettuce-core-5.2.1.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\io\netty\netty-common\4.1.43.Final\netty-common-4.1.43.Final.jar;E:\sofe\maven\maven-repository-20191031\io\netty\netty-handler\4.1.43.Final\netty-handler-4.1.43.Final.jar;E:\sofe\maven\maven-repository-20191031\io\netty\netty-buffer\4.1.43.Final\netty-buffer-4.1.43.Final.jar;E:\sofe\maven\maven-repository-20191031\io\netty\netty-codec\4.1.43.Final\netty-codec-4.1.43.Final.jar;E:\sofe\maven\maven-repository-20191031\io\netty\netty-transport\4.1.43.Final\netty-transport-4.1.43.Final.jar;E:\sofe\maven\maven-repository-20191031\io\netty\netty-resolver\4.1.43.Final\netty-resolver-4.1.43.Final.jar;E:\sofe\maven\maven-repository-20191031\io\projectreactor\reactor-core\3.3.1.RELEASE\reactor-core-3.3.1.RELEASE.jar;E:\sofe\maven\maven-repository-20191031\org\reactivestreams\reactive-streams\1.0.3\reactive-streams-1.0.3.jar;E:\sofe\maven\maven-repository-20191031\cn\hutool\hutool-all\4.3.1\hutool-all-4.3.1.jar;E:\sofe\maven\maven-repository-20191031\io\jsonwebtoken\jjwt\0.7.0\jjwt-0.7.0.jar;E:\sofe\maven\maven-repository-20191031\com\fasterxml\jackson\core\jackson-databind\2.10.1\jackson-databind-2.10.1.jar;E:\sofe\maven\maven-repository-20191031\com\fasterxml\jackson\core\jackson-annotations\2.10.1\jackson-annotations-2.10.1.jar;E:\sofe\maven\maven-repository-20191031\com\fasterxml\jackson\core\jackson-core\2.10.1\jackson-core-2.10.1.jar;E:\sofe\maven\maven-repository-20191031\org\apache\commons\commons-pool2\2.7.0\commons-pool2-2.7.0.jar;E:\sofe\maven\maven-repository-20191031\javax\xml\bind\jaxb-api\2.3.0\jaxb-api-2.3.0.jar;E:\sofe\maven\maven-repository-20191031\com\sun\xml\bind\jaxb-impl\2.3.0\jaxb-impl-2.3.0.jar;E:\sofe\maven\maven-repository-20191031\com\sun\xml\bind\jaxb-core\2.3.0\jaxb-core-2.3.0.jar;E:\sofe\maven\maven-repository-20191031\javax\activation\activation\1.1.1\activation-1.1.1.jar;E:\sofe\maven\maven-repository-20191031\org\apache\poi\poi-ooxml\4.1.0\poi-ooxml-4.1.0.jar;E:\sofe\maven\maven-repository-20191031\org\apache\poi\poi\4.1.0\poi-4.1.0.jar;E:\sofe\maven\maven-repository-20191031\commons-codec\commons-codec\1.13\commons-codec-1.13.jar;E:\sofe\maven\maven-repository-20191031\org\apache\commons\commons-collections4\4.3\commons-collections4-4.3.jar;E:\sofe\maven\maven-repository-20191031\org\apache\commons\commons-math3\3.6.1\commons-math3-3.6.1.jar;E:\sofe\maven\maven-repository-20191031\org\apache\poi\poi-ooxml-schemas\4.1.0\poi-ooxml-schemas-4.1.0.jar;E:\sofe\maven\maven-repository-20191031\org\apache\xmlbeans\xmlbeans\3.1.0\xmlbeans-3.1.0.jar;E:\sofe\maven\maven-repository-20191031\org\apache\commons\commons-compress\1.18\commons-compress-1.18.jar;E:\sofe\maven\maven-repository-20191031\com\github\virtuald\curvesapi\1.06\curvesapi-1.06.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5 com.example.demo.mapper.UserMapperTest,test
10:34:11.101 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
10:34:11.116 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
10:34:11.148 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.example.demo.mapper.UserMapperTest] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
10:34:11.167 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.example.demo.mapper.UserMapperTest], using SpringBootContextLoader
10:34:11.174 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.example.demo.mapper.UserMapperTest]: class path resource [com/example/demo/mapper/UserMapperTest-context.xml] does not exist
10:34:11.175 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.example.demo.mapper.UserMapperTest]: class path resource [com/example/demo/mapper/UserMapperTestContext.groovy] does not exist
10:34:11.175 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.example.demo.mapper.UserMapperTest]: no resource found for suffixes {-context.xml, Context.groovy}.
10:34:11.176 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.example.demo.mapper.UserMapperTest]: UserMapperTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
10:34:11.235 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.example.demo.mapper.UserMapperTest]
10:34:11.345 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [E:\demo\target\classes\com\example\demo\SimpleApplication.class]
10:34:11.346 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.example.demo.SimpleApplication for test class com.example.demo.mapper.UserMapperTest
10:34:11.487 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.example.demo.mapper.UserMapperTest]: using defaults.
10:34:11.487 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener, org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener, org.springframework.security.test.context.support.ReactorContextTestExecutionListener]
10:34:11.512 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@5d8bafa9, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@576f63f6, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@2dca0d64, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@f80945f, org.springframework.test.context.support.DirtiesContextTestExecutionListener@ff684e1, org.springframework.test.context.transaction.TransactionalTestExecutionListener@23aa363a, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@5ef6ae06, org.springframework.test.context.event.EventPublishingTestExecutionListener@55dfebeb, org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener@6e35bc3d, org.springframework.security.test.context.support.ReactorContextTestExecutionListener@1cdc4c27, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@77b14724, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@5d9b7a8a, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@1e8ce150, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@604f2bd2, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@1d3ac898]
10:34:11.517 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@1ecee32c testClass = UserMapperTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@4372b9b6 testClass = UserMapperTest, locations = '{}', classes = '{class com.example.demo.SimpleApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6a8658ff, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@55de24cc, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@821330f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@14dd7b39], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].
10:34:11.560 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=-1}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.2.RELEASE)

2019-12-17 10:34:12.076  INFO 87840 --- [           main] com.example.demo.mapper.UserMapperTest   : Starting UserMapperTest on LAPTOP-5L33IHOU with PID 87840 (started by Lenovo in E:\demo)
2019-12-17 10:34:12.078 DEBUG 87840 --- [           main] com.example.demo.mapper.UserMapperTest   : Running with Spring Boot v2.2.2.RELEASE, Spring v5.2.2.RELEASE
2019-12-17 10:34:12.079  INFO 87840 --- [           main] com.example.demo.mapper.UserMapperTest   : No active profile set, falling back to default profiles: default
2019-12-17 10:34:13.309  INFO 87840 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2019-12-17 10:34:13.313  INFO 87840 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2019-12-17 10:34:13.361  INFO 87840 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 23ms. Found 0 Redis repository interfaces.
2019-12-17 10:34:14.041  INFO 87840 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-17 10:34:18.143  INFO 87840 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@40d96578, org.springframework.security.web.context.SecurityContextPersistenceFilter@19f02280, org.springframework.security.web.header.HeaderWriterFilter@28a6e171, org.springframework.web.filter.CorsFilter@c97721b, org.springframework.security.web.authentication.logout.LogoutFilter@5d9618f2, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@cc9ef8d, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@fbe70d8, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@283ab206, org.springframework.security.web.session.SessionManagementFilter@265c1a7c, org.springframework.security.web.access.ExceptionTranslationFilter@1472208d, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@359ceb13]
2019-12-17 10:34:18.217 ERROR 87840 --- [           main] o.a.catalina.core.AprLifecycleListener   : An incompatible version [1.2.8] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]
2019-12-17 10:34:18.527  INFO 87840 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-12-17 10:34:19.467  INFO 87840 --- [           main] com.example.demo.mapper.UserMapperTest   : Started UserMapperTest in 7.894 seconds (JVM running for 9.218)

{123=[User(id=null, password=123, name=123, createTime=null, updateTime=null, role=null), User(id=null, password=123, name=123, createTime=null, updateTime=null, role=null)], 124=[User(id=null, password=123, name=123, createTime=null, updateTime=null, role=null)], 125=[User(id=null, password=123, name=123, createTime=null, updateTime=null, role=null)]}

2019-12-17 10:34:19.990  INFO 87840 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

Process finished with exit code 0

list 转化为map出现key重复:

 @Test
    public void testList() {
        User user = new User();
        user.setName("123");
        user.setPassword("123");
        User user1 = new User();
        user1.setName("123");
        user1.setPassword("124");
        User user2 = new User();
        user2.setName("124");
        user2.setPassword("125");
        User user3 = new User();
        user3.setName("125");
        user3.setPassword("125");

        List<User> users = new ArrayList<>();
        users.add(user);
        users.add(user1);
        users.add(user2);
        users.add(user3);
        Map<String, String> userMap = users.stream().collect(Collectors.toMap(User::getName, User::getPassword));
        System.out.println(userMap);
    }
java.lang.IllegalStateException: Duplicate key 123 (attempted merging values 123 and 124)

	at java.base/java.util.stream.Collectors.duplicateKeyException(Collectors.java:133)
	at java.base/java.util.stream.Collectors.lambda$uniqKeysMapAccumulator$1(Collectors.java:180)
	at java.base/java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
	at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1654)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
	at com.example.demo.mapper.UserMapperTest.testList(UserMapperTest.java:183)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
	at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:125)
	at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:132)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:124)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:74)
	at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:104)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:62)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:43)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:35)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:202)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:198)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
	at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
	at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

解决:1.将后面的值覆盖前面的值

 Map<String, String> userMap = users.stream().collect(Collectors.toMap(User::getName, User::getPassword, (String name1, String name2) -> name2));
        System.out.println(userMap);

{123=124, 124=125, 125=125}

2019-12-17 15:15:18.432  INFO 98828 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

Process finished with exit code 0

2.将重复值拼接后返回

Map<String, String> userMap = users.stream().collect(Collectors.toMap(User::getName, User::getPassword, (String name1, String name2) -> name1 + ":" + name2));
        System.out.println(userMap);
{123=123:124, 124=125, 125=125}

2019-12-17 15:19:39.014  INFO 94772 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

Process finished with exit code 0

3.将重复的值返回成集合

Map<String, List<String>> userMap = users.stream().collect(Collectors.toMap(User::getName, perUser -> {
                    List<String> list = new ArrayList<>();
                    list.add(perUser.getPassword());
                    return list;
                },
                (List<String> password1, List<String> password2) -> {
                    password1.addAll(password2);
                    return password1;
                }
        ));
        System.out.println(userMap);

{123=[123, 124], 124=[125], 125=[125]}

2019-12-17 15:40:05.683  INFO 101176 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

Process finished with exit code 0

有兴趣学习的可以关注公众号阅读相关:码上行走

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值