Friday, 15 September 2023

How many ways can convert HashMap to List

Hi,

Today we will talk about how we can convert Hashmap into ArrayList.

1. we have converted keys of HashMap into List.

2. we have converted all values of Map into List.

3. we have converted all entries(key  & values) of HashMap into List.

In below i have explained programme stepwise:- 

package com.neesri.sorting;

 

import java.util.ArrayList;

import java.util.Collection;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map.Entry;

import java.util.Set;

 

public class ConvertMapToList {

     // we can convert Hashmap into Arraylist through three ways

     public static void main(String[] args) {

 

           HashMap<String, Integer> hashmapObj = new HashMap<>();

           hashmapObj.put("Zimbabwe", 600);

           hashmapObj.put("India", 700);

           hashmapObj.put("SriLanka", 400);

           hashmapObj.put("Nepal", 800);

 

           // 1-----convert Hashmap Key into List

           // using keyset method retun set view

           Set<String> setViewOfMapKeys = hashmapObj.keySet();

           // convert set into list

           List<String> listObj = new ArrayList<>(setViewOfMapKeys);

 

           System.out.println("Hashmap Keys into list------>");

           for (String printList : listObj) {

                System.out.println(printList);

           }

 

           // 2. Convert Hashmap Values into List

 

           Collection<Integer> values = hashmapObj.values();

           List<Integer> list = new ArrayList<>(values);

           System.out.println("Hashmap values into List----->");

           for (Integer hashmapValuesIntoList : list) {

                System.out.println(hashmapValuesIntoList);

           }

 

           // 3. Convert hashmap all entries into List

 

           Set<Entry<String, Integer>> entriesObjSet = hashmapObj.entrySet();

           List<Entry<String, Integer>> listEntries = new ArrayList<>(entriesObjSet);

 

           // System.out.println("Hashmap all entries into List======>");

           /*

            * for(Object obj:listEntries) {

            *

            * System.out.println(obj); }

            */

           // OR

 

           /*

            * for(Entry<String, Integer> obj:listEntries) {

            *

            * System.out.println(obj); }

            */

 

           // OR

           System.out.println("Printing thorough iterator======>");

           Iterator<Entry<String, Integer>> iterator = listEntries.iterator();

 

           while (iterator.hasNext()) {

                Entry<String, Integer> entry = iterator.next();

 

                System.out.println(entry);

           }

 

     }

 

}

 

==============OUTPUT======

Hashmap Keys into list------>

SriLanka

Zimbabwe

Nepal

India

Hashmap values into List----->

400

600

800

700

Printing thorough iterator======>

SriLanka=400

Zimbabwe=600

Nepal=800

India=700

 

 

Happy Learning :) 

No comments:

Post a Comment