How does one convert a HashMap to a List in Java?

后端 未结 7 1145
春和景丽
春和景丽 2020-12-08 01:35

In Java, how does one get the values of a HashMap returned as a List?

相关标签:
7条回答
  • 2020-12-08 02:42

    Collection Interface has 3 views

    • keySet
    • values
    • entrySet

    Other have answered to to convert Hashmap into two lists of key and value. Its perfectly correct

    My addition: How to convert "key-value pair" (aka entrySet)into list.

          Map m=new HashMap();
              m.put(3, "dev2");
              m.put(4, "dev3");
    
          List<Entry> entryList = new ArrayList<Entry>(m.entrySet());
    
          for (Entry s : entryList) {
            System.out.println(s);
          }
    

    ArrayList has this constructor.

    0 讨论(0)
提交回复
热议问题