Convert Properties List into Map in Java



In this article, we will learn how to convert a Properties list into a Map in Java. The Properties class is commonly used to store key-value pairs, but sometimes you may need to work with these pairs in a Map structure. We will demonstrate how to take the properties and convert them into a HashMap.

Problem Statement

Write a program in Java to convert the properties list into a Map structure ?

Output

Key and Value of the Map...
P: 1
Q: 2
R: 3
S: 4
T: 5
U: 6
V: 7

Steps to convert the properties list into a Map ?

Following are the steps to convert the properties list into a Map ?

  • First, import the necessary classes from the java.util package, such as HashMap, Map, Properties, and Set.
  • Create an instance of the Properties class using Properties p = new Properties().
  • Use the setProperty() method on the Properties object to add key-value pairs.
  • Convert the Properties object into a HashMap by passing the Properties instance to the HashMap constructor.
  • Retrieve the entries from the HashMap using the entrySet() method.
  • Iterate over the map entries using a for loop and print each key-value pair.

Java program to convert properties list into a Map

Below is the Java program to convert the properties list into a Map ?

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

public class Demo {
   public static void main(String args[]) {
      Properties p = new Properties();
      p.setProperty("P", "1");
      p.setProperty("Q", "2");
      p.setProperty("R", "3");
      p.setProperty("S", "4");
      p.setProperty("T", "5");
      p.setProperty("U", "6");
      p.setProperty("V", "7");

      // Manually convert Properties to HashMap
      HashMap<String, String> map = new HashMap<>();
      for (String key : p.stringPropertyNames()) {
         map.put(key, p.getProperty(key));
      }

      Set<Map.Entry<String, String>> set = map.entrySet();
      System.out.println("Key and Value of the Map... ");
      for (Map.Entry<String, String> m : set) {
         System.out.print(m.getKey() + ": ");
         System.out.println(m.getValue());
      }
   }
}

Output

Key and Value of the Map...
P: 1
Q: 2
R: 3
S: 4
T: 5
U: 6
V: 7

Code Explanation

To convert the properties list into a map, let us first create an object of Properties class ?

Properties p = new Properties();

Now, use setProperty() to set key-value pair ?

p.setProperty("P", "1");
p.setProperty("Q", "2");
p.setProperty("R", "3");
p.setProperty("S", "4");
p.setProperty("T", "5");
p.setProperty("U", "6");

With that, the following is how the Properties list is converted into a Map ?

HashMap<String, String>map = new HashMap<String, String>((Map) p);

Then, it manually converts the Properties to a HashMap by looping through the property names using stringPropertyNames() and fetching each value with getProperty(). Finally, the program iterates over the entries of the HashMap and prints the key-value pairs.

Updated on: 2024-09-19T21:55:48+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements