更新 Java 把 Map 的值(Value)转换为 Array, List 或 Set https://www.ossez.com/t/java-map-value-array-list-set/14388

This commit is contained in:
YuCheng Hu 2023-04-21 16:35:42 -04:00
parent f482b249aa
commit b7e57d1240
14 changed files with 27 additions and 26 deletions

View File

@ -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)

View File

@ -1,4 +1,4 @@
package com.baeldung.convertToMap;
package com.ossez.convertToMap;
public class Book {
private String name;

View File

@ -1,4 +1,4 @@
package com.baeldung.convertToMap;
package com.ossez.convertToMap;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

View File

@ -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.

View File

@ -1,4 +1,4 @@
package com.baeldung.convertlisttomap;
package com.ossez.convertlisttomap;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.MapUtils;

View File

@ -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;

View File

@ -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<Integer, Animal> 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<Integer, Animal> 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)

View File

@ -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;

View File

@ -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<Integer> 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<Integer> 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<Integer> 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<Integer> targetSet = new HashSet<Integer>(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<Integer> targetSet = new HashSet<Integer>();
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<Integer> 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<Integer> targetSet = new HashSet<>(6);
CollectionUtils.addAll(targetSet, sourceArray);
}