How to convert a Map to List in Java

1 Answer

0 votes
import java.util.Map;
import java.util.Set;
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");
  
        Set<Map.Entry<String, String>> set = map.entrySet();
        
        List<Map.Entry<String, String>> list = new ArrayList<>(set);
  
        System.out.println(list);
    }
}
 

  
  
  
/*
run:
  
[Red=#FF0000, Blue=#0000FF, Yellow=#FFFF00, Green=#00FF00]

*/

 



answered Nov 17, 2023 by avibootz

Related questions

1 answer 133 views
133 views asked Nov 17, 2023 by avibootz
2 answers 182 views
4 answers 171 views
171 views asked Mar 25, 2023 by avibootz
1 answer 130 views
130 views asked Mar 23, 2023 by avibootz
1 answer 154 views
...