What happens when there are duplicate keys in Java properties file?

Based on my understanding of Properties, the load method works in a similar fashion to this:

  1. Split the file into lines,
  2. Look at the next line,
  3. Determine the Key-Value pair using some rules (See here)
  4. Put the key value pair into the Properties instance in a fashion similar to the put() method

This would mean that your example would display 99.

The load method is basically designed to work as though you had sat down and typed out

propInstance.put("Key", "Value");
propInstance.put("Other", "Thing");
etc etc

To understand this behavior, see the documentation for Hashtable.put() which specifies that it updates any duplicates with the new value. Since Hashtable is the superclass for Properties, Properties also replicates this behaviour.


Because this isn't defined in the spec for the class, I'd say the most correct answer to this question is that the result is undefined, and could vary from implementation to implementation.

However, because java.util.Properties inherits from java.utils.Hashtable, the most likely implementation is exactly as described by @jozefg, and you can see in the OpenJDK source that the Sun implementation works that way (Properties.java:345 as of the time of this writing). Read each line, parse it to decide if you need to append other lines, separate key and value, put key/value in Hashtable.

There's no:

  • check to see if the key exists
  • exception thrown based on the presence of the key
  • avoidance of overwriting values
  • out-of-order processing

It's all very simple and basically assumes either that you haven't used duplicate keys, or that if you have, it's your problem to sort out.

Now, of course, to be totally sure you'd want to look at all the likely JVMs or at least the target JVM for your code to make sure the implementation doesn't differ, but I think this implementation is the most likely one.


This worked for me. Instead of using Properties, I instantiated a NaehasProperties, and overrode the HashTable put().

/**
 *  Purpose:  Properties doesn't detect duplicate keys.  So this exists.
 *  @author shaned
 */
package com.naehas.tests.configs;

import java.util.Properties;

import org.apache.log4j.Logger;

public class NaehasProperties extends Properties
{
   private static final long   serialVersionUID = 1L;

   private static final Logger log              = Logger.getLogger(NaehasProperties.class);

   public NaehasProperties()
   {
      super();
   }

   /**
    * @param defaults
    */
   public NaehasProperties(Properties defaults)
   {
      super(defaults);
   }

   /**
    * Overriding the HastTable put() so we can check for duplicates
    * 
    */
   public synchronized Object put(Object key, Object value)
   {
      // Have we seen this key before?
      //
      if (get(key) != null)
      {
         StringBuffer message = new StringBuffer("Duplicate key found: " + key + " with value: " + value);
         message.append(". Original value is: " + (String) get(key));

         log.error(message.toString());

         // Setting key to null will generate an exception and cause an exit.
         // Can not change the signature by adding a throws as it's not compatible
         // with HashTables put().
         //
         key = null;
      }

      return super.put(key, value);
   }
}