From 6c4848958f18c84133b79301fe06ab896aee7b85 Mon Sep 17 00:00:00 2001 From: Azhwani <13301425+azhwani@users.noreply.github.com> Date: Sun, 24 Sep 2023 04:43:53 +0200 Subject: [PATCH] BAEL-6783: Converting HashMap to an ArrayList in Java (#14814) --- .../README.md | 5 ++ .../pom.xml | 26 ++++++++ .../HashMapToArrayListConverterUtils.java | 66 +++++++++++++++++++ ...hMapToArrayListConverterUtilsUnitTest.java | 60 +++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100644 core-java-modules/core-java-collections-conversions-3/README.md create mode 100644 core-java-modules/core-java-collections-conversions-3/pom.xml create mode 100644 core-java-modules/core-java-collections-conversions-3/src/main/java/com/baeldung/hashmaptoarraylist/HashMapToArrayListConverterUtils.java create mode 100644 core-java-modules/core-java-collections-conversions-3/src/test/java/com/baeldung/hashmaptoarraylist/HashMapToArrayListConverterUtilsUnitTest.java diff --git a/core-java-modules/core-java-collections-conversions-3/README.md b/core-java-modules/core-java-collections-conversions-3/README.md new file mode 100644 index 0000000000..f89cd199ff --- /dev/null +++ b/core-java-modules/core-java-collections-conversions-3/README.md @@ -0,0 +1,5 @@ +## Java Collections Cookbooks and Examples + +This module contains articles about conversions among Collection types in Java. + +### Relevant Articles: diff --git a/core-java-modules/core-java-collections-conversions-3/pom.xml b/core-java-modules/core-java-collections-conversions-3/pom.xml new file mode 100644 index 0000000000..4813d33713 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions-3/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + core-java-collections-conversions-3 + core-java-collections-conversions-3 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + core-java-collections-conversions-3 + + + src/main/resources + true + + + + + \ No newline at end of file diff --git a/core-java-modules/core-java-collections-conversions-3/src/main/java/com/baeldung/hashmaptoarraylist/HashMapToArrayListConverterUtils.java b/core-java-modules/core-java-collections-conversions-3/src/main/java/com/baeldung/hashmaptoarraylist/HashMapToArrayListConverterUtils.java new file mode 100644 index 0000000000..dd5cc1fe47 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions-3/src/main/java/com/baeldung/hashmaptoarraylist/HashMapToArrayListConverterUtils.java @@ -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 convertUsingConstructor(HashMap hashMap) { + if (hashMap == null) { + return null; + } + return new ArrayList(hashMap.values()); + } + + static ArrayList convertUsingAddAllMethod(HashMap hashMap) { + if (hashMap == null) { + return null; + } + + ArrayList arrayList = new ArrayList(hashMap.size()); + arrayList.addAll(hashMap.values()); + + return arrayList; + } + + static ArrayList convertUsingStreamApi(HashMap hashMap) { + if (hashMap == null) { + return null; + } + + return hashMap.values() + .stream() + .collect(Collectors.toCollection(ArrayList::new)); + } + + static ArrayList convertUsingForLoop(HashMap hashMap) { + if (hashMap == null) { + return null; + } + + ArrayList arrayList = new ArrayList(hashMap.size()); + for (Map.Entry entry : hashMap.entrySet()) { + arrayList.add(entry.getValue()); + } + + return arrayList; + } + + static public ArrayList convertUsingGuava(HashMap hashMap) { + if (hashMap == null) { + return null; + } + + EntryTransformer entryMapTransformer = (key, value) -> value; + + return Lists.newArrayList(Maps.transformEntries(hashMap, entryMapTransformer) + .values()); + } + +} diff --git a/core-java-modules/core-java-collections-conversions-3/src/test/java/com/baeldung/hashmaptoarraylist/HashMapToArrayListConverterUtilsUnitTest.java b/core-java-modules/core-java-collections-conversions-3/src/test/java/com/baeldung/hashmaptoarraylist/HashMapToArrayListConverterUtilsUnitTest.java new file mode 100644 index 0000000000..26a42e77c0 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions-3/src/test/java/com/baeldung/hashmaptoarraylist/HashMapToArrayListConverterUtilsUnitTest.java @@ -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 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 myList = HashMapToArrayListConverterUtils.convertUsingConstructor(hashMap); + + assertThat(hashMap.values(), containsInAnyOrder(myList.toArray())); + } + + @Test + public void givenAHashMap_whenConvertUsingAddAllMethod_thenReturnArrayList() { + ArrayList myList = HashMapToArrayListConverterUtils.convertUsingAddAllMethod(hashMap); + + assertThat(hashMap.values(), containsInAnyOrder(myList.toArray())); + } + + @Test + public void givenAHashMap_whenConvertUsingForLoop_thenReturnArrayList() { + ArrayList myList = HashMapToArrayListConverterUtils.convertUsingForLoop(hashMap); + + assertThat(hashMap.values(), containsInAnyOrder(myList.toArray())); + } + + @Test + public void givenAHashMap_whenConvertUsingStreamApi_thenReturnArrayList() { + ArrayList myList = HashMapToArrayListConverterUtils.convertUsingStreamApi(hashMap); + + assertThat(hashMap.values(), containsInAnyOrder(myList.toArray())); + } + + @Test + public void givenAHashMap_whenConvertUsingGuava_thenReturnArrayList() { + ArrayList myList = HashMapToArrayListConverterUtils.convertUsingGuava(hashMap); + + assertThat(hashMap.values(), containsInAnyOrder(myList.toArray())); + } + +}