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

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

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

Disclosure: My content contains affiliate links.

43,239 questions

56,142 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 229 views
3 answers 210 views
2 answers 158 views
1 answer 135 views
2 answers 207 views
...