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,938 questions

51,875 answers

573 users

How to convert Map values to List in Java

2 Answers

0 votes
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Collection;
 
public class MyClass 
{
    public static void main(String[] args)
    {
        Map<String, String> map = new HashMap<>();

        map.put("Red", "#FF0000");
        map.put("Green", "#00FF00");
        map.put("Blue", "#0000FF");
        map.put("Yellow", "#FFFF00");
  
        Collection<String> values = map.values();
        
        List<String> list = new ArrayList<>(values);

        System.out.println(list);
    }
}
 

  
  
  
/*
run:
  
[#FF0000, #0000FF, #FFFF00, #00FF00]

*/

 



answered Nov 17, 2023 by avibootz
0 votes
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;

public class MyClass 
{
    public static void main(String[] args)
    {
        Map<String, String> map = new HashMap<>();

        map.put("Red", "#FF0000");
        map.put("Green", "#00FF00");
        map.put("Blue", "#0000FF");
        map.put("Yellow", "#FFFF00");

        List<String> list = new ArrayList<>(map.values());

        System.out.println(list);
    }
}

  
  
  
/*
run:
  
[#FF0000, #0000FF, #FFFF00, #00FF00]

*/

 



answered Nov 17, 2023 by avibootz

Related questions

2 answers 136 views
3 answers 106 views
2 answers 66 views
1 answer 66 views
2 answers 126 views
...