Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to convert HashMap to ArrayList in Java

1 Answer

0 votes
import java.util.Collection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MyClass {

    public static void main(String args[]) {
        HashMap<String, Integer> hmp = new HashMap<String, Integer>();
    
        hmp.put("java", 4);
        hmp.put("c++", 2);
        hmp.put("c", 7);
        hmp.put("python", 5);
        hmp.put("rust", 6);
        hmp.put("c#", 1);
        hmp.put("php", 3);
        
        Set<String> keySet = hmp.keySet(); 
  
        // ArrayList of keys 
        ArrayList<String> ALKeys = new ArrayList<String>(keySet); 
  
        Collection<Integer> values = hmp.values(); 
  
        // ArrayList of values 
        ArrayList<Integer> ALValues = new ArrayList<>(values); 
  
        System.out.println(ALKeys); 
  
        System.out.println(ALValues); 
    }
}
        
        
        
        
/*
run:
        
[c#, rust, c++, python, java, c, php]
[1, 6, 2, 5, 4, 7, 3]

*/

 



answered Oct 8, 2023 by avibootz

Related questions

1 answer 173 views
1 answer 168 views
1 answer 118 views
118 views asked Nov 7, 2021 by avibootz
1 answer 135 views
...