256 Kilobytes

Forum in Web Scraping, Data Analysis | By kodelogsuser25

Published

43 views, 0 RAMs, and 0 comments

Profile Photo - Defaultkodelogsuser25

Hello readers, welcome to my post :)

In this post, we shall discuss how we can convert the given hash map into an arraylist. Both array list and hashmap are altogether different collections and the way and techniques of both of the collections are entirely different. The hash map works on the principle for key-value pairs whereas an arraylist does not work on the key-value principle, rather it is a kind of storage medium which tends to store data in the form of indices. Then how we can actually go about converting the hash map contents into an arraylist. We would see the various techniques involved in the conversion process. With that having said, let's get into the post.

Arraylist vs Hashmap?

As we have already discussed, An arraylist can be explained as a data structure that tends to store data elements in a list, which has a tendency to increase its size dynamically. When an element needs to be traversed and manipulated, it can be achieved in the arraylist through means of the index, memory location where the element is actually stored. However, the hashmap is a totally different entity that tends to store the elements in key-value pairs. The actual values would be stored in a particular memory location, which would be, in turn, represented by unique values called keys in the hashmap. Both key and value together can be considered as an entry. The hash map would have entries within it.

How can we convert?

We can actually create an arraylist using either the values or the keys from the map. Else, there is also another option to create an arraylist using the entries after extracting them from the map. We would see how these can be implemented in java. Broadly, the techniques can be represented as such like below -

  • Converting keys of hashmap into arraylist
  • Converting Values of hashmap into arraylist
  • Converting entries of hashmap into arraylist

We would see how we can achieve these conversions in detail in the forthcoming sections

The below code snippet depicts the conversion of the hashmap keys, values and the entries into its corresponding arraylist in a kind of consolidated way.

`public static void main(String[] args)

{ 
    //Creating a HashMap 
    
    HashMap<String, String> blogMap = new HashMap<String, String>(); 
     
    //Adding elements
     
    blogMap.put("Hashmap", "Java"); 
     
    blogMap.put(“Tuple", "python"); 

    blogMap.put(“clustered index", "sql"); 

    
     
    //Getting the keys
     
    Set<String> blogKeys = blogMap.keySet(); 
     
  
     
    ArrayList<String> keyList = new ArrayList<String>(blogKeys); 
     

     
    for (String key : keyList) 
    { 
        System.out.println(key); 
    }
     
   
     
    //Getting values 
     
    Collection<String> blogValues = blogMap.values(); 
         
    ArrayList<String> blogValues = new ArrayList<String>(blogValues); 
  
    for (String value : blogValues) 
    { 
        System.out.println(value); 
    } 
    
    //Getting entries from hash map
     
    Set<Entry<String, String>> entrySet = blogMap.entrySet(); 
     
     
    ArrayList<Entry<String, String>> listOfEntry = new ArrayList<Entry<String,String>>(entrySet); 
    
    for (Entry<String, String> entry : listOfEntry) 
    { 
        System.out.println(entry.getKey()+" : "+entry.getValue()); 
    } 
} `

In the above code, a hashmap is created and the key-value pairs are added to the hashmap. We tend to make use of the keySet, values and the entrySet methods for retrieving the keys, values and the entries from the map. Every time, after we fetch the values, we tend to construct and instantiate an array list that actually takes in the respective entities as input parameters. Thus we tend to create 3 different lists from the same hashmap.

Thus we have come to the end of this post. Hope you found this informative. Please do share in case of any comments

Thanks for reading :)

Download more RAM. 🐏 ⨉ 0Posted by kodelogsuser25 1 month ago 🕓 Posted at 23 May, 2022 05:26 AM PDT

Post a New Comment

Do you like having a good time?

Register an Account

You can also login to an existing account or reset your password. All use of this site is subject to the terms of service and privacy policy.

Read Quality Articles

Read some quality articles. If you can manage to not get banned for like five minutes, you can even post your own articles.

View Articles →

Argue with People on the Internet

Use your account to explain why people are wrong on the Internet forum.

View Forum →

Vandalize the Wiki

Or don't. I'm not your dad.

View Wiki →

Ask and/or Answer Questions

If someone asks a terrible question, post a LMGTFY link.

View Answers →

Make Some Money

Hire freelancers and/or advertise your goods and/or services. Hire people directly. We're not a middleman or your dad. Manage your own business transactions.

Register an Account

Forum— Read More

Find more related content below!

Read More: