当 Java 属性文件中有重复的键时会发生什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12558210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:22:14  来源:igfitidea点击:

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

javapropertiesconfiguration

提问by aaaidan

What is the defined behaviour when there are duplicate keys in a Java .propertiesfile?

当 Java.properties文件中存在重复键时,定义的行为是什么?

thing.valueA = 1
thing.valueB = 2

thing.valueA = 99

Which value is guaranteed to be used for thing.valueA? 1, 99, or undefined? Is this behaviour documented anywhere?

哪个值保证用于thing.valueA?1、99 还是未定义?这种行为是否记录在任何地方?

NB. I am not asking whether duplicate keys are considered best practice.

注意。我不是问重复键是否被认为是最佳实践。

采纳答案by Geoffrey Wiseman

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:345as 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.

但是,因为 java.util.Properties 继承自 java.utils.Hashtable,所以最有可能的实现与@jozefg 所描述的完全一样,您可以在 OpenJDK 源代码中看到 Sun 实现是这样工作的(Properties.java:345as在撰写本文时)。读取每一行,解析它以决定是否需要附加其他行,将键和值分开,将键/值放在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.

现在,当然,要完全确定您想查看所有可能的 JVM 或至少查看代码的目标 JVM 以确保实现没有不同,但我认为这个实现是最有可能的。

回答by Daniel Gratzer

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

根据我对 的理解Properties,load 方法的工作方式与此类似:

  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
  1. 将文件分成几行,
  2. 看下一行,
  3. 使用一些规则确定键值对(请参阅此处
  4. 以类似于put()方法的方式将键值对放入Properties实例中

This would mean that your example would display 99.

这意味着您的示例将显示99.

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

load 方法基本上设计为就像您已经坐下并输入一样工作

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.

要了解此行为,请参阅Hashtable.put()指定它使用新值更新任何重复项的文档。由于 Hashtable 是 Properties 的超类,Properties 也复制了这种行为。

回答by Shane

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

这对我有用。我没有使用属性,而是实例化了一个 NaehasProperties,并覆盖了 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);
   }
}

回答by Raghavan

It usually takes the last value,in your case it will be 99.

它通常采用最后一个值,在您的情况下它将是 99。

Thanks, Raghavan

谢谢,拉格万