How to iterate Properites in Java

Posted on January 3, 2017


In this post, we will show you how to iterate the java.util.Properties using -

  • java.util.Enumeration
  • For-Each loop + Properties's stringPropertyNames() method
  • For-Each loop + entry set
  • forEach() method ( Introduced in Java 8)

Iterating Properties using Enumeration

The propertyNames() method of the Properties return an enumeration of all the keys.

IterateProperitesExample1.java

package com.boraji.tutorial;

import java.util.Enumeration;
import java.util.Properties;

/**
 * @author imssbora
 */
public class IterateProperitesExample1 {
  public static void main(String[] args) {
    Properties props = new Properties();
    props.setProperty("1", "One");
    props.setProperty("2", "Two");
    props.setProperty("3", "Three");
    props.setProperty("4", "Four");
    props.setProperty("5", "Five");

    // Iterating properties using Enumeration

    @SuppressWarnings("unchecked")
    Enumeration<String> enums = (Enumeration<String>) props.propertyNames();
    while (enums.hasMoreElements()) {
      String key = enums.nextElement();
      String value = props.getProperty(key);
      System.out.println(key + " : " + value);
    }
  }
}

 

Iterating Properties using For-Each loop + Properties's stringPropertyNames() method

The stringPropertyNames() method of the Properties return a set of keys, where the key and its corresponding value are strings.

IterateProperitesExample2.java

package com.boraji.tutorial;

import java.util.Properties;
import java.util.Set;

/**
 * @author imssbora
 */
public class IterateProperitesExample2 {
  public static void main(String[] args) {
    Properties props = new Properties();
    props.setProperty("1", "One");
    props.setProperty("2", "Two");
    props.setProperty("3", "Three");
    props.setProperty("4", "Four");
    props.setProperty("5", "Five");

    // Iterating properties using For-Each

    Set<String> keys = props.stringPropertyNames();
    for (String key : keys) {
      System.out.println(key + " : " + props.getProperty(key));
    }
  }
}

 

Iterating Properties using For-Each loop + entry set

The following example demonstrates the use of the entrySet() method of the Properties class.

IterateProperitesExample3.java

package com.boraji.tutorial;

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

/**
 * @author imssbora
 */
public class IterateProperitesExample3 {
  public static void main(String[] args) {
    Properties props = new Properties();
    props.setProperty("1", "One");
    props.setProperty("2", "Two");
    props.setProperty("3", "Three");
    props.setProperty("4", "Four");
    props.setProperty("5", "Five");

    // Iterating properties using Entry Set

    Set<Entry<Object, Object>> entries = props.entrySet();
    for (Entry<Object, Object> entry : entries) {
      System.out.println(entry.getKey() + " : " + entry.getValue());
    }

  }
}

 

Iterating Properties using forEach() method

Here is an example of using the forEach() method of the Properties class.

IterateProperitesExample4.java

package com.boraji.tutorial;

import java.util.Properties;

/**
 * @author imssbora
 */
public class IterateProperitesExample4 {
  public static void main(String[] args) {
    Properties props = new Properties();
    props.setProperty("1", "One");
    props.setProperty("2", "Two");
    props.setProperty("3", "Three");
    props.setProperty("4", "Four");
    props.setProperty("5", "Five");

    // Iterating properties using Java 8 forEach

    props.forEach((key, value) -> System.out.println(key + " : " + value));

  }
}