Java cast hashmap to collection

Convert HashMap to ArrayList in java

HashMap and ArrayList both are the most used data structure in java and both have different Hierarchy.

Here are the ways to convert HashMap to ArrayList in java.

Using Java 8’s Stream API

Java 8: Convert HashMap’s keys to ArrayList

We can use HashMap’s keyset ( ) method to get a set of keys and use Java 8‘s Stream API to convert it to ArrayList.

Читайте также:  Render html inside html

Convert HashMap’s values to ArrayList

We can use HashMap’s values() method to get a collection of values and use Java 8’s Stream API to convert it to ArrayList.

Convert HashMap’s Entry objects to ArrayList

We can use HashMap’s entrySet() method to get set of Entry object and use Java 8’s Stream API to convert it to ArrayList.

Let’s see the complete example.

customerIds: [1001, 1002, 1003, 1004] Customer Names: [Arman, Javin, Mat, Joe] Customer ID and Names: [1001=Arman, 1002=Javin, 1003=Mat, 1004=Joe]

Using ArrayList’s constructor

Convert HashMap’s keys to ArrayList

Get keySet ( ) from HashMap and then convert it to ArrayList using its constructor.

Convert HashMap’s values to ArrayList

Get values ( ) from HashMap and then convert it to ArrayList using its constructor.

Convert HashMap’s Entry objects to ArrayList

Get entrySet ( ) from HashMap and then convert it to ArrayList using its constructor.

Java HashMap tutorial

Was this post helpful?

You may also like:

Update Value of Key in HashMap in Java

Create Array of Linked Lists in Java

Return ArrayList in Java

Create List with One Element in Java

How to Add Multiple Values for Single Key In HashMap in Java

[Fixed] java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

Create ArrayList of Objects in Java

How to remove element from Arraylist in java while iterating

Share this

Author

Update Value of Key in HashMap in Java

Table of ContentsUsing the put() Method of HashMap Collection in JavaUsing the compute() Method of HashMap Collection in JavaUsing the merge() Method of the HashMap Collection in JavaUsing the computeIfPresent() Method of The HashMap Collection in JavaUsing the replace() Method of The HashMap Collection in JavaUsing the TObjectIntHashMap Class of Gnu.Trove Package in JavaUsing the […]

Create Array of Linked Lists in Java

Table of ContentsIntroductionLinked List in JavaApplication of Array of Linked ListsCreate Array of Linked Lists in JavaUsing Object[] array of Linked Lists in JavaUsing the Linked List array in JavaUsing the ArrayList of Linked Lists in JavaUsing the Apache Commons Collections Package Introduction In this article, we will look at how to Create an Array […]

Return ArrayList in Java

Table of ContentsReturn ArrayList in Java From a Static MethodReturn ArrayList in Java From a Non-static MethodConclusion This article discusses cases of returning an ArrayList in Java from a method. An ArrayList in Java is a collection of elements of the same data type under a single variable name. In different cases, you can return […]

Create List with One Element in Java

Table of ContentsUsing Collections.singletonList()Using Array.asList() method [ Immuatable list]Using new ArrayList with Array.asList() method [ Mutable list] In this post, we will see how to create List with One Element in java.. Using Collections.singletonList() This is best way to create List with single element if you need an immutable List. [crayon-64be5303b51b9802326126/] Output [crayon-64be5303b51c4923782818/] If you […]

How to Add Multiple Values for Single Key In HashMap in Java

Table of ContentsHashMapCan HashMap Store Multiple Values AutomaticallyWays to Add Multiple Values for Single Key In HashMap in JavaUsing the Standard LibraryUsing Apache Commons LibraryUsing Google Guava LibraryUsing TreeSet as ValuesUsing a Wrapper ClassUsing Java TuplesUsing compute() Function in JDK 8Conclusion This article discusses the HashMap in Java and how to add multiple values for […]

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

[Fixed] java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

Table of ContentsReason for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayListFixes for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayListUse ArrayList’s constructorAssign Arrays.asList() to List reference rather than ArrayList In this post, we will see how to fix java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList. ClassCastException is runtime exception which indicate that code has tried to […]

Источник

How to Convert HashMap to ArrayList in Java?

HashMap is a part of Java’s collection since Java 1.2. It provides the basic implementation of the Map interface of Java. It stores the data in (Key, Value) pairs. Unlike Hashmap, an ArrayList is used to provides us with dynamic arrays in Java. It is an alternative to an array that provides a lot more flexibility in terms of storage. In this article, we will see how to convert a Hashmap to an ArrayList.
Since every element in the hashmap contains a key/value pair, it is not possible to store this key/value pair in a single ArrayList. Therefore, in order to convert a hashmap into an ArrayList, we need to maintain two separate ArrayList’s where one ArrayList is used to store the Keys and the other to store the values corresponding to those keys. The following are the various methods used to convert a hashmap to the ArrayList.

Method 1:

One way to convert is to use the constructor of the ArrayList. In order to do this, we can use the keySet() method present in the HashMap. This method returns the set containing all the keys of the hashmap. This set can be passed into the ArrayList while initialization in order to obtain an ArrayList containing all the keys. After obtaining the keys, we can use the values() method present in the hashmap to obtain a collection of all the values present in the hashmap. We can use this collection to initialize another array that contains all the values for the keys in the hashmap.

Источник

How to convert HashMap to ArrayList in Java?

Hashmap to Arraylist in Java - Crunchify

Here is a simple example on how to convert HashMap to ArrayList in Java.

Java Example:

package com.crunchify; /** * @author Crunchify.com */ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; public class CrunchifyHashmapToArrayList < public static void main(String. args) < HashMapcompanyDetails = new HashMap(); // create hashmap with keys and values (CompanyName, #Employees) companyDetails.put("eBay", 4444); companyDetails.put("Paypal", 5555); companyDetails.put("IBM", 6666); companyDetails.put("Google", 7777); companyDetails.put("Yahoo", 8888); System.out.println("==> Size of companyDetails Map: " + companyDetails.size()); Iterator it = companyDetails.entrySet().iterator(); while (it.hasNext()) < Map.Entry pairs = (Map.Entry) it.next(); System.out.println(pairs.getKey() + " = " + pairs.getValue()); >// Converting HashMap keys into ArrayList List keyList = new ArrayList(companyDetails.keySet()); System.out.println("\n==> Size of Key list: " + keyList.size()); for (String temp : keyList) < System.out.println(temp); >// Converting HashMap Values into ArrayList List valueList = new ArrayList(companyDetails.values()); System.out.println("\n==> Size of Value list: " + valueList.size()); for (Integer temp : valueList) < System.out.println(temp); >List entryList = new ArrayList(companyDetails.entrySet()); System.out.println("\n==> Size of Entry list: " + entryList.size()); for (Entry temp : entryList) < System.out.println(temp); >> >
==> Size of companyDetails Map: 5 IBM = 6666 Yahoo = 8888 Google = 7777 Paypal = 5555 eBay = 4444 ==> Size of Key list: 5 IBM Yahoo Google Paypal eBay ==> Size of Value list: 5 6666 8888 7777 5555 4444 ==> Size of Entry list: 5 IBM=6666 Yahoo=8888 Google=7777 Paypal=5555 eBay=4444

If you liked this article, then please share it on social media. Have a question or suggestion? Please leave a comment to start the discussion.

Источник

Оцените статью