How To Convert HashMap To ArrayList In Java?

As HashMap contains key-value pairs, there are three ways you can convert given HashMap to ArrayList. You can convert HashMap keys into ArrayList or you can convert HashMap values into ArrayList or you can convert key-value pairs into ArrayList.

examples:

a) Conversion Of HashMap Keys Into ArrayList :
        
HashMap<String, String> map = new HashMap<String, String>();
Set<String> keySet = map.keySet();   
ArrayList<String> listOfKeys = new ArrayList<String>(keySet);

b) Conversion Of HashMap Values Into ArrayList :

HashMap<String, String> map = new HashMap<String, String>();
Collection<String> values = map.values();
ArrayList<String> listOfValues = new ArrayList<String>(values);

c) Conversion Of HashMap’s Key-Value Pairs Into ArrayList :
     
HashMap<String, String> map = new HashMap<String, String>();
Set<Entry<String, String>> entrySet = map.entrySet();      
ArrayList<Entry<String, String>> listOfEntry = new ArrayList<Entry<String,String>
>(entrySet);

No comments:

Post a Comment

Genuine websites to earn money.

If you are interested in PTC sites then this article is for you. I have personally tried many of the sites and found that the best thing ...