Tuesday, March 27, 2018

Convert HashMap To ArrayList


1. a) Convert HashMap Keys Into ArrayList :

             We should use keySet() method of HashMap which will returns the Set containing all keys of the HashMap. After that, we should pass those while creating ArrayList.


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class HelloWorld{

     public static void main(String []args){
        //Creating a HashMap object
         
HashMap<String, String> map = new HashMap<String, String>();
map.put("1", "One");
map.put("2", "Two");
map.put("3", "Three");
map.put("4", "Four");
 
//Getting Set of keys from HashMap
         
Set<String> keySet = map.keySet();
         
//Creating an ArrayList of keys by passing the keySet
         
ArrayList<String> listOfKeys = new ArrayList<String>(keySet);
System.out.println("output is :" + listOfKeys);
     }
}

output is : [1, 2, 3, 4]


b) Convert HashMap Values Into ArrayList :

           We should use values() method of HashMap which will returns all values of the HashMap. After that, we should use this to create the ArrayList. let see an example below.


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class HelloWorld{

     public static void main(String []args){

//Creating a HashMap object
         
HashMap<String, String> map = new HashMap<String, String>();
 
map.put("1", "One");
map.put("2", "Two");
map.put("3", "Three");
map.put("4", "Four");
map.put("5", "Five");
//Getting Collection of values from HashMap
         
Collection<String> values = map.values();
         
//Creating an ArrayList of values
         
ArrayList<String> listOfValues = new ArrayList<String>(values);

System.out.println("output is " + listOfValues);
     }
}

output is : [One, Two, Three, Four, Five]



c) Convert HashMap Key-Value Pairs into ArrayList :

          We should use entrySet() method of HashMap which will returns the Set of Entry<K, V> objects where each Entry object represents one key-value pair. After that, We should pass this Set to ArrayList. let see below an example.


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class HelloWorld{

     public static void main(String []args){

HashMap<String, String> map = new HashMap<String, String>();

map.put("1", "One");
map.put("2", "Two");
map.put("3", "Three");
map.put("4", "Four");
map.put("5", "Five");

//Getting the Set of entries
         
Set<Entry<String, String>> entrySet = map.entrySet();
         
//Creating an ArrayList Of Entry objects
         
ArrayList<Entry<String, String>> listOfEntry = new ArrayList<Entry<String,String>>(entrySet);

System.out.println("output is " + listOfEntry);
     }
}

output is : [1=One, 2=Two, 3=Three, 4=Four, 5=Five]






No comments:

Post a Comment