BAEL-6783: Converting HashMap to an ArrayList in Java (#14814)
This commit is contained in:
parent
8171a4604f
commit
6c4848958f
|
@ -0,0 +1,5 @@
|
|||
## Java Collections Cookbooks and Examples
|
||||
|
||||
This module contains articles about conversions among Collection types in Java.
|
||||
|
||||
### Relevant Articles:
|
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-collections-conversions-3</artifactId>
|
||||
<name>core-java-collections-conversions-3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-collections-conversions-3</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,66 @@
|
|||
package com.baeldung.hashmaptoarraylist;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Maps.EntryTransformer;
|
||||
|
||||
public class HashMapToArrayListConverterUtils {
|
||||
|
||||
static ArrayList<String> convertUsingConstructor(HashMap<Integer, String> hashMap) {
|
||||
if (hashMap == null) {
|
||||
return null;
|
||||
}
|
||||
return new ArrayList<String>(hashMap.values());
|
||||
}
|
||||
|
||||
static ArrayList<String> convertUsingAddAllMethod(HashMap<Integer, String> hashMap) {
|
||||
if (hashMap == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ArrayList<String> arrayList = new ArrayList<String>(hashMap.size());
|
||||
arrayList.addAll(hashMap.values());
|
||||
|
||||
return arrayList;
|
||||
}
|
||||
|
||||
static ArrayList<String> convertUsingStreamApi(HashMap<Integer, String> hashMap) {
|
||||
if (hashMap == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return hashMap.values()
|
||||
.stream()
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
}
|
||||
|
||||
static ArrayList<String> convertUsingForLoop(HashMap<Integer, String> hashMap) {
|
||||
if (hashMap == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ArrayList<String> arrayList = new ArrayList<String>(hashMap.size());
|
||||
for (Map.Entry<Integer, String> entry : hashMap.entrySet()) {
|
||||
arrayList.add(entry.getValue());
|
||||
}
|
||||
|
||||
return arrayList;
|
||||
}
|
||||
|
||||
static public ArrayList<String> convertUsingGuava(HashMap<Integer, String> hashMap) {
|
||||
if (hashMap == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
EntryTransformer<Integer, String, String> entryMapTransformer = (key, value) -> value;
|
||||
|
||||
return Lists.newArrayList(Maps.transformEntries(hashMap, entryMapTransformer)
|
||||
.values());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package com.baeldung.hashmaptoarraylist;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HashMapToArrayListConverterUtilsUnitTest {
|
||||
|
||||
private HashMap<Integer, String> hashMap;
|
||||
|
||||
@Before
|
||||
public void beforeEach() {
|
||||
hashMap = new HashMap<>();
|
||||
hashMap.put(1, "AAA");
|
||||
hashMap.put(2, "BBB");
|
||||
hashMap.put(3, "CCC");
|
||||
hashMap.put(4, "DDD");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAHashMap_whenConvertUsingConstructor_thenReturnArrayList() {
|
||||
ArrayList<String> myList = HashMapToArrayListConverterUtils.convertUsingConstructor(hashMap);
|
||||
|
||||
assertThat(hashMap.values(), containsInAnyOrder(myList.toArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAHashMap_whenConvertUsingAddAllMethod_thenReturnArrayList() {
|
||||
ArrayList<String> myList = HashMapToArrayListConverterUtils.convertUsingAddAllMethod(hashMap);
|
||||
|
||||
assertThat(hashMap.values(), containsInAnyOrder(myList.toArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAHashMap_whenConvertUsingForLoop_thenReturnArrayList() {
|
||||
ArrayList<String> myList = HashMapToArrayListConverterUtils.convertUsingForLoop(hashMap);
|
||||
|
||||
assertThat(hashMap.values(), containsInAnyOrder(myList.toArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAHashMap_whenConvertUsingStreamApi_thenReturnArrayList() {
|
||||
ArrayList<String> myList = HashMapToArrayListConverterUtils.convertUsingStreamApi(hashMap);
|
||||
|
||||
assertThat(hashMap.values(), containsInAnyOrder(myList.toArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAHashMap_whenConvertUsingGuava_thenReturnArrayList() {
|
||||
ArrayList<String> myList = HashMapToArrayListConverterUtils.convertUsingGuava(hashMap);
|
||||
|
||||
assertThat(hashMap.values(), containsInAnyOrder(myList.toArray()));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue