更新 Java 把 Map 的值(Value)转换为 Array, List 或 Set https://www.ossez.com/t/java-map-value-array-list-set/14388
This commit is contained in:
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.convertToMap;
|
||||
package com.ossez.convertToMap;
|
||||
|
||||
public class Book {
|
||||
private String name;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.convertToMap;
|
||||
package com.ossez.convertToMap;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
+1
-1
@@ -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.
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.convertlisttomap;
|
||||
package com.ossez.convertlisttomap;
|
||||
|
||||
public class Animal {
|
||||
private int id;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.convertlisttomap;
|
||||
package com.ossez.convertlisttomap;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.convertToMap;
|
||||
package com.ossez.convertToMap;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.convertcollectiontoarraylist;
|
||||
package com.ossez.convertcollectiontoarraylist;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
+1
-1
@@ -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;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.convertlisttomap;
|
||||
package com.ossez.convertlisttomap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
+4
-3
@@ -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)
|
||||
+1
-1
@@ -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;
|
||||
+8
-8
@@ -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);
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java.lists;
|
||||
package com.ossez.java.lists;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
Reference in New Issue
Block a user