Java Collection How to - Convert a Properties list into a map








Question

We would like to know how to convert a Properties list into a map.

Answer

  /*from w  w w  .  j a v a2 s. co  m*/

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Main {
  public static void main(String args[]) {

    Properties prop = new Properties();

    prop.setProperty("A", "t@h.com");
    prop.setProperty("B", "k@h.com");
    prop.setProperty("C", "R@h.com");
    prop.setProperty("D", "S@h.com");

    HashMap<String, String> propMap = new HashMap<String, String>((Map) prop);

    Set<Map.Entry<String, String>> propSet;
    propSet = propMap.entrySet();

    System.out.println("Contents of map: ");
    for (Map.Entry<String, String> me : propSet) {
      System.out.print(me.getKey() + ": ");
      System.out.println(me.getValue());
    }
  }
}

The code above generates the following result.