» » » Convert values of a Map to a List

  1. package com.instanceofjavaforus;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6.  
  7. public class MapValueToList {
  8.  
  9.  Map<Integer, String> map;
  10.  
  11.  public MapKeyToList(Map<Integer, String> map) {
  12.       this.map = map;
  13.  }
  14.  public List<String> convertValuesToList() {
  15.     return new ArrayList(map.values());
  16.  }
  17.  
  18.  public static void main(String[] args) {           
  19.  Map<Integer, String> map = new HashMap<>();
  20.  
  21.     map.put(1, "one");
  22.     map.put(2, "two");
  23.     map.put(3, "three");
  24.     map.put(4, "Four");
  25.     map.put(5, "Five");
  26.     map.put(6, "Six");
  27.     map.put(7, "Seven");
  28.     map.put(8, "Eight");
  29.     map.put(9, "Nine");
  30.     map.put(10, "Ten");
  31.  
  32.      MapValueToList  conv = new MapValueToList (map);
  33.      List<String> keysList = conv.convertValuesToList();
  34.      System.out.println("Values:");
  35.     for (String val : keysList) {
  36.        System.out.println(val);
  37. }
  38.  
  39.  }
  40. }
  41. OutPut:
  42. Values:
  43. one
  44. two
  45. three
  46. Four
  47. Five
  48. Six
  49. Seven
  50. Eight
  51. Nine
  52. Ten

     


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

1 comments for Convert values of a Map to a List

  1. Map map = new HashMap<>(); this statement will work from java 7

    ReplyDelete

Select Menu