How to convert Map keys to List in Java

1 Answer

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.keySet());

        System.out.println(list);
    }
}

  
  
  
/*
run:
  
[Red, Blue, Yellow, Green]

*/

 



answered Nov 17, 2023 by avibootz

Related questions

1 answer 165 views
3 answers 159 views
2 answers 151 views
2 answers 161 views
1 answer 117 views
117 views asked Nov 17, 2023 by avibootz
1 answer 125 views
...