From b7e57d12400e5d6012ae7dd26786904cc0e45caa Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Fri, 21 Apr 2023 16:35:42 -0400 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20Java=20=E6=8A=8A=20Map=20?= =?UTF-8?q?=E7=9A=84=E5=80=BC=EF=BC=88Value=EF=BC=89=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E4=B8=BA=20Array,=20List=20=E6=88=96=20Set=20https://www.ossez?= =?UTF-8?q?.com/t/java-map-value-array-list-set/14388?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core-java-collections-conversions/README.md | 8 ++++---- .../{baeldung => ossez}/convertToMap/Book.java | 2 +- .../convertToMap/ConvertToMap.java | 2 +- .../convertcollectiontoarraylist/Foo.java | 2 +- .../convertlisttomap/Animal.java | 2 +- .../ConvertListToMapService.java | 2 +- .../convertToMap/ConvertToMapUnitTest.java | 2 +- .../CollectionToArrayListUnitTest.java | 2 +- .../ConvertIteratorToListServiceUnitTest.java | 2 +- .../ConvertListToMapServiceUnitTest.java | 2 +- ...ListWithDuplicatedIdToMapServiceUnitTest.java | 7 ++++--- .../IterableToCollectionUnitTest.java | 2 +- .../JavaCollectionConversionUnitTest.java | 16 ++++++++-------- .../java/lists/ListToStringUnitTest.java | 2 +- 14 files changed, 27 insertions(+), 26 deletions(-) rename core-java-modules/core-java-collections-conversions/src/main/java/com/{baeldung => ossez}/convertToMap/Book.java (96%) rename core-java-modules/core-java-collections-conversions/src/main/java/com/{baeldung => ossez}/convertToMap/ConvertToMap.java (97%) rename core-java-modules/core-java-collections-conversions/src/main/java/com/{baeldung => ossez}/convertcollectiontoarraylist/Foo.java (94%) rename core-java-modules/core-java-collections-conversions/src/main/java/com/{baeldung => ossez}/convertlisttomap/Animal.java (91%) rename core-java-modules/core-java-collections-conversions/src/main/java/com/{baeldung => ossez}/convertlisttomap/ConvertListToMapService.java (96%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/convertToMap/ConvertToMapUnitTest.java (97%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java (98%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java (98%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/convertlisttomap/ConvertListToMapServiceUnitTest.java (97%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java (89%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/java/collections/IterableToCollectionUnitTest.java (98%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/java/collections/JavaCollectionConversionUnitTest.java (90%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/java/lists/ListToStringUnitTest.java (96%) diff --git a/core-java-modules/core-java-collections-conversions/README.md b/core-java-modules/core-java-collections-conversions/README.md index 5dd1848ce0..c4050c49dd 100644 --- a/core-java-modules/core-java-collections-conversions/README.md +++ b/core-java-modules/core-java-collections-conversions/README.md @@ -1,11 +1,11 @@ -## Java Collections Cookbooks and Examples +## Java 集合(Collections)相关文章和实例 -This module contains articles about conversions among Collection types and arrays in Java. +本模块中的内容包含有 Java 集合(Collections)转换相关的方法。 -### Relevant Articles: +### 相关文章 - [Converting between an Array and a List in Java](https://www.baeldung.com/convert-array-to-list-and-list-to-array) - [Converting Between an Array and a Set in Java](https://www.baeldung.com/convert-array-to-set-and-set-to-array) -- [Convert a Map to an Array, List or Set in Java](https://www.baeldung.com/convert-map-values-to-array-list-set) +- [Java 把 Map 的值(Value)转换为 Array, List 或 Set](https://www.ossez.com/t/java-map-value-array-list-set/14388) - [Converting a List to String in Java](https://www.baeldung.com/java-list-to-string) - [How to Convert List to Map in Java](https://www.baeldung.com/java-list-to-map) - [Converting a Collection to ArrayList in Java](https://www.baeldung.com/java-convert-collection-arraylist) diff --git a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/Book.java b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertToMap/Book.java similarity index 96% rename from core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/Book.java rename to core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertToMap/Book.java index 847e0bd8cd..2a4e969ae6 100644 --- a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/Book.java +++ b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertToMap/Book.java @@ -1,4 +1,4 @@ -package com.baeldung.convertToMap; +package com.ossez.convertToMap; public class Book { private String name; diff --git a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertToMap/ConvertToMap.java similarity index 97% rename from core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java rename to core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertToMap/ConvertToMap.java index 317cdca529..61393a0ac5 100644 --- a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java +++ b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertToMap/ConvertToMap.java @@ -1,4 +1,4 @@ -package com.baeldung.convertToMap; +package com.ossez.convertToMap; import java.util.*; import java.util.concurrent.ConcurrentHashMap; diff --git a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertcollectiontoarraylist/Foo.java b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertcollectiontoarraylist/Foo.java similarity index 94% rename from core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertcollectiontoarraylist/Foo.java rename to core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertcollectiontoarraylist/Foo.java index 5c9464182e..9a54f000bf 100644 --- a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertcollectiontoarraylist/Foo.java +++ b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertcollectiontoarraylist/Foo.java @@ -1,4 +1,4 @@ -package com.baeldung.convertcollectiontoarraylist; +package com.ossez.convertcollectiontoarraylist; /** * This POJO is the element type of our collection. It has a deepCopy() method. diff --git a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/Animal.java b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertlisttomap/Animal.java similarity index 91% rename from core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/Animal.java rename to core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertlisttomap/Animal.java index b8eddf71a5..c8d9fdfcaf 100644 --- a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/Animal.java +++ b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertlisttomap/Animal.java @@ -1,4 +1,4 @@ -package com.baeldung.convertlisttomap; +package com.ossez.convertlisttomap; public class Animal { private int id; diff --git a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertlisttomap/ConvertListToMapService.java similarity index 96% rename from core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java rename to core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertlisttomap/ConvertListToMapService.java index 57579e948f..d162183f56 100644 --- a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java +++ b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertlisttomap/ConvertListToMapService.java @@ -1,4 +1,4 @@ -package com.baeldung.convertlisttomap; +package com.ossez.convertlisttomap; import com.google.common.collect.Maps; import org.apache.commons.collections4.MapUtils; diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertToMap/ConvertToMapUnitTest.java similarity index 97% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertToMap/ConvertToMapUnitTest.java index d6eab461d7..745a801698 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertToMap/ConvertToMapUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.convertToMap; +package com.ossez.convertToMap; import java.util.ArrayList; import java.util.List; diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java similarity index 98% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java index b8134de08a..be470671a7 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.convertcollectiontoarraylist; +package com.ossez.convertcollectiontoarraylist; import java.util.ArrayList; import java.util.Collection; diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java similarity index 98% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java index 7d94f88d21..97ba2e4326 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.convertiteratortolist; +package com.ossez.convertiteratortolist; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.collection.IsCollectionWithSize.hasSize; diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertlisttomap/ConvertListToMapServiceUnitTest.java similarity index 97% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertlisttomap/ConvertListToMapServiceUnitTest.java index 4d783f4525..e538cfc9b9 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertlisttomap/ConvertListToMapServiceUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.convertlisttomap; +package com.ossez.convertlisttomap; import org.junit.Before; import org.junit.Test; diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java similarity index 89% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java index 5e6828a3d5..c8f61814fb 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java @@ -1,5 +1,6 @@ -package com.baeldung.convertlisttomap; +package com.ossez.convertlisttomap; +import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; @@ -40,7 +41,7 @@ public class ConvertListWithDuplicatedIdToMapServiceUnitTest { Map map = convertListService.convertListBeforeJava8(duplicatedIdList); assertThat(map.values(), hasSize(4)); - assertThat(map.values(), hasItem(duplicatedIdList.get(4))); + assertThat(map.values(), Matchers.hasItem(duplicatedIdList.get(4))); } @Test @@ -49,7 +50,7 @@ public class ConvertListWithDuplicatedIdToMapServiceUnitTest { Map map = convertListService.convertListWithApacheCommons(duplicatedIdList); assertThat(map.values(), hasSize(4)); - assertThat(map.values(), hasItem(duplicatedIdList.get(4))); + assertThat(map.values(), Matchers.hasItem(duplicatedIdList.get(4))); } @Test(expected = IllegalStateException.class) diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/IterableToCollectionUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/collections/IterableToCollectionUnitTest.java similarity index 98% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/IterableToCollectionUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/collections/IterableToCollectionUnitTest.java index 0283191b74..22293a7c77 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/IterableToCollectionUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/collections/IterableToCollectionUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.java.collections; +package com.ossez.java.collections; import static org.hamcrest.Matchers.contains; import static org.junit.Assert.assertThat; diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/collections/JavaCollectionConversionUnitTest.java similarity index 90% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/collections/JavaCollectionConversionUnitTest.java index 7947d1b6c7..9ab666a765 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/collections/JavaCollectionConversionUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.java.collections; +package com.ossez.java.collections; import java.util.ArrayList; import java.util.Arrays; @@ -25,7 +25,7 @@ public class JavaCollectionConversionUnitTest { @Test public final void givenUsingCoreJava_whenArrayConvertedToList_thenCorrect() { - final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Integer[] sourceArray = {0, 1, 2, 3, 4, 5}; final List targetList = Arrays.asList(sourceArray); } @@ -37,7 +37,7 @@ public class JavaCollectionConversionUnitTest { @Test public final void givenUsingGuava_whenArrayConvertedToList_thenCorrect() { - final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Integer[] sourceArray = {0, 1, 2, 3, 4, 5}; final List targetList = Lists.newArrayList(sourceArray); } @@ -49,7 +49,7 @@ public class JavaCollectionConversionUnitTest { @Test public final void givenUsingCommonsCollections_whenArrayConvertedToList_thenCorrect() { - final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Integer[] sourceArray = {0, 1, 2, 3, 4, 5}; final List targetList = new ArrayList<>(6); CollectionUtils.addAll(targetList, sourceArray); } @@ -58,13 +58,13 @@ public class JavaCollectionConversionUnitTest { @Test public final void givenUsingCoreJavaV1_whenArrayConvertedToSet_thenCorrect() { - final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Integer[] sourceArray = {0, 1, 2, 3, 4, 5}; final Set targetSet = new HashSet(Arrays.asList(sourceArray)); } @Test public final void givenUsingCoreJavaV2_whenArrayConvertedToSet_thenCorrect() { - final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Integer[] sourceArray = {0, 1, 2, 3, 4, 5}; final Set targetSet = new HashSet(); Collections.addAll(targetSet, sourceArray); } @@ -77,7 +77,7 @@ public class JavaCollectionConversionUnitTest { @Test public final void givenUsingGuava_whenArrayConvertedToSet_thenCorrect() { - final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Integer[] sourceArray = {0, 1, 2, 3, 4, 5}; final Set targetSet = Sets.newHashSet(sourceArray); } @@ -89,7 +89,7 @@ public class JavaCollectionConversionUnitTest { @Test public final void givenUsingCommonsCollections_whenArrayConvertedToSet_thenCorrect() { - final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Integer[] sourceArray = {0, 1, 2, 3, 4, 5}; final Set targetSet = new HashSet<>(6); CollectionUtils.addAll(targetSet, sourceArray); } diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/lists/ListToStringUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/lists/ListToStringUnitTest.java similarity index 96% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/lists/ListToStringUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/lists/ListToStringUnitTest.java index 0fdc096d14..b05002b9f4 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/lists/ListToStringUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/lists/ListToStringUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.java.lists; +package com.ossez.java.lists; import java.util.Arrays; import java.util.List;