Merge pull request 'Java 从一个 List 中删除所有重复的元素: https://www.ossez.com/t/java-list/13942' (#17) from code_bank into main

Reviewed-on: https://src.ossez.com/cwiki-us-docs/java-tutorials/pulls/17
This commit is contained in:
honeymoose 2022-05-01 11:08:06 -04:00
commit d4356f36f0
4 changed files with 109 additions and 24 deletions

View File

@ -4,9 +4,10 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="080575e3-7e62-4649-ae70-06fb6130a866" name="Changes" comment="init submit all for code branch">
<list default="true" id="080575e3-7e62-4649-ae70-06fb6130a866" name="Changes" comment="添加文章链接: https://www.ossez.com/t/java-list-null/13940">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/core-java-modules/core-java-collections-list/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/core-java-modules/core-java-collections-list/README.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/core-java-modules/core-java-collections-list/src/test/java/com/ossez/collections/JavaCollectionCleanupUnitTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/core-java-modules/core-java-collections-list/src/test/java/com/ossez/collections/JavaCollectionCleanupUnitTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/core-java-modules/core-java-collections-list/src/test/java/com/ossez/java8/Java8CollectionCleanupUnitTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/core-java-modules/core-java-collections-list/src/test/java/com/ossez/java8/Java8CollectionCleanupUnitTest.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -62,8 +63,11 @@
<recent name="D:\WorkDir\Repository\GitHub\cwiki-us-docs\java-tutorials\core-java-modules\core-java-collections-list\src\test\resources" />
<recent name="D:\WorkDir\Repository\GitHub\cwiki-us-docs\java-tutorials\core-java-modules\core-java-collections-list\src\test" />
</key>
<key name="CopyClassDialog.RECENTS_KEY">
<recent name="com.ossez.collections" />
</key>
</component>
<component name="RunManager" selected="JUnit.JavaCollectionCleanupUnitTest.givenListContainsNulls_whenRemovingNullsWithGuavaV1_thenCorrect">
<component name="RunManager" selected="JUnit.JavaCollectionCleanupUnitTest.givenListContainsDuplicates_whenRemovingDuplicatesWithPlainJava_thenCorrect">
<configuration default="true" type="DjangoTestsConfigurationType">
<module name="junit-5-basics" />
<option name="INTERPRETER_OPTIONS" value="" />
@ -103,6 +107,22 @@
<framework value="gotest" />
<method v="2" />
</configuration>
<configuration name="JavaCollectionCleanupUnitTest.givenListContainsDuplicates_whenRemovingDuplicatesWithPlainJava_thenCorrect" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="core-java-collections-list" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.ossez.collections.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="com.ossez.collections" />
<option name="MAIN_CLASS_NAME" value="com.ossez.collections.JavaCollectionCleanupUnitTest" />
<option name="METHOD_NAME" value="givenListContainsDuplicates_whenRemovingDuplicatesWithPlainJava_thenCorrect" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="JavaCollectionCleanupUnitTest.givenListContainsNulls_whenRemovingNullsWithGuavaV1_thenCorrect" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="core-java-collections-list" />
<extension name="coverage">
@ -167,22 +187,6 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="RandomListElementUnitTest.givenList_whenSeriesLengthChosen_shouldReturnRandomSeries" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="core-java-collections-list" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.ossez.list.random.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="com.ossez.list.random" />
<option name="MAIN_CLASS_NAME" value="com.ossez.list.random.RandomListElementUnitTest" />
<option name="METHOD_NAME" value="givenList_whenSeriesLengthChosen_shouldReturnRandomSeries" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration default="true" type="JetRunConfigurationType">
<module name="junit-5-basics" />
<method v="2">
@ -292,11 +296,11 @@
</configuration>
<recent_temporary>
<list>
<item itemvalue="JUnit.JavaCollectionCleanupUnitTest.givenListContainsDuplicates_whenRemovingDuplicatesWithPlainJava_thenCorrect" />
<item itemvalue="JUnit.JavaCollectionCleanupUnitTest.givenListContainsNulls_whenRemovingNullsWithGuavaV1_thenCorrect" />
<item itemvalue="JUnit.JavaCollectionCleanupUnitTest.givenListContainsNulls_whenRemovingNullsWithPlainJavaAlternative_thenCorrect" />
<item itemvalue="JUnit.JavaCollectionCleanupUnitTest.givenListContainsNulls_whenRemovingNullsWithPlainJava_thenCorrect" />
<item itemvalue="JUnit.RandomListElementUnitTest.givenList_whenRandomIndexChosen_shouldReturnARandomElementUsingMathRandom" />
<item itemvalue="JUnit.RandomListElementUnitTest.givenList_whenSeriesLengthChosen_shouldReturnRandomSeries" />
</list>
</recent_temporary>
</component>
@ -425,7 +429,14 @@
<option name="project" value="LOCAL" />
<updated>1651361573106</updated>
</task>
<option name="localTasksCounter" value="17" />
<task id="LOCAL-00017" summary="添加文章链接: https://www.ossez.com/t/java-list-null/13940">
<created>1651415236464</created>
<option name="number" value="00017" />
<option name="presentableId" value="LOCAL-00017" />
<option name="project" value="LOCAL" />
<updated>1651415236464</updated>
</task>
<option name="localTasksCounter" value="18" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -459,7 +470,8 @@
<MESSAGE value="格式化代码并修改错误" />
<MESSAGE value="提交所有代码,为将默认分支名从 master 切换到 main 做准备" />
<MESSAGE value="init submit all for code branch" />
<option name="LAST_COMMIT_MESSAGE" value="init submit all for code branch" />
<MESSAGE value="添加文章链接: https://www.ossez.com/t/java-list-null/13940" />
<option name="LAST_COMMIT_MESSAGE" value="添加文章链接: https://www.ossez.com/t/java-list-null/13940" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>

View File

@ -0,0 +1,64 @@
package com.ossez.collections;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.PredicateUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import java.util.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
/**
* Test for list Clean up
*
* <p><a href="https://www.ossez.com/t/java-list/13942">https://www.ossez.com/t/java-list/13942</a></p>
*
* @author YuCheng
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class JavaCollectionCleanupDuplicatesTest {
// TESTS - REMOVE DUPLICATES
@Test
public void givenListContainsDuplicates_whenRemovingDuplicatesWithPlainJava_thenCorrect() {
final List<Integer> listWithDuplicates = Lists.newArrayList(5, 0, 3, 1, 2, 3, 0, 0);
final List<Integer> listWithoutDuplicates = new ArrayList<>(new HashSet<>(listWithDuplicates));
assertThat(listWithoutDuplicates, hasSize(5));
assertThat(listWithoutDuplicates, containsInAnyOrder(5, 0, 3, 1, 2));
}
@Test
public void givenListContainsDuplicates_whenRemovingDuplicatesPreservingOrderWithPlainJava_thenCorrect() {
final List<Integer> listWithDuplicates = Lists.newArrayList(5, 0, 3, 1, 2, 3, 0, 0);
final List<Integer> listWithoutDuplicates = new ArrayList<>(new LinkedHashSet<>(listWithDuplicates));
assertThat(listWithoutDuplicates, hasSize(5));
assertThat(listWithoutDuplicates, containsInRelativeOrder(5, 0, 3, 1, 2));
}
@Test
public void givenListContainsDuplicates_whenRemovingDuplicatesWithGuava_thenCorrect() {
final List<Integer> listWithDuplicates = Lists.newArrayList(5, 0, 3, 1, 2, 3, 0, 0);
final List<Integer> listWithoutDuplicates = Lists.newArrayList(Sets.newHashSet(listWithDuplicates));
assertThat(listWithoutDuplicates, hasSize(5));
assertThat(listWithoutDuplicates, containsInAnyOrder(5, 0, 3, 1, 2));
}
@Test
public void givenListContainsDuplicates_whenRemovingDuplicatesPreservingOrderWithGuava_thenCorrect() {
final List<Integer> listWithDuplicates = Lists.newArrayList(5, 0, 3, 1, 2, 3, 0, 0);
final List<Integer> listWithoutDuplicates = Lists.newArrayList(Sets.newLinkedHashSet(listWithDuplicates));
assertThat(listWithoutDuplicates, hasSize(5));
assertThat(listWithoutDuplicates, containsInRelativeOrder(5, 0, 3, 1, 2));
}
}

View File

@ -71,7 +71,7 @@ public class JavaCollectionCleanupUnitTest {
assertThat(list, hasSize(3));
}
// tests - remove duplicates
// TESTS - REMOVE DUPLICATES
@Test
public void givenListContainsDuplicates_whenRemovingDuplicatesWithPlainJava_thenCorrect() {

View File

@ -2,15 +2,24 @@ package com.ossez.java8;
import com.google.common.collect.Lists;
import org.junit.Test;
import org.junit.jupiter.api.TestInstance;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
/**
* Test for list Clean up
*
* <p><a href="https://www.ossez.com/t/java-list/13942">https://www.ossez.com/t/java-list/13942</a></p>
*
* @author YuCheng
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class Java8CollectionCleanupUnitTest {
// tests -