比较两个Properties对象中的值的最佳方法是什么?

时间:2021-11-12 23:04:21

I have two Properties instances which contain same keys but may contain different values(strings). What is the best way to check if the values are identical in both the instances.

我有两个属性实例,它们包含相同的键但可能包含不同的值(字符串)。检查两个实例中的值是否相同的最佳方法是什么。

Right now I am using if conditions to check like below

现在我正在使用条件如下检查

if(!p1.getProperty("x").equals(p2.getProperty("x")) {
    return true;
}

2 个解决方案

#1


3  

Properties is a subclass of HashTable, which overrides equals. You can simply compare the instances using equals:

Properties是HashTable的子类,它会覆盖equals。您可以使用equals简单地比较实例:

properties1.equals(properties2)

But this won't tell you what is different.

但这不会告诉你有什么不同。

To do that, you can get the keys using properties.keySet(), and then compare the values between the two instances:

为此,您可以使用properties.keySet()获取密钥,然后比较两个实例之间的值:

for (String key : properties1.keySet()) {
  String value1 = properties1.get(key);
  String value2 = properties2.get(key);

  // Compare, e.g. value1.equals(value2).
  // But may need to take into account missing values.
}

Note that this is asymmetrical, in the sense that it looks for the values for which values exist in properties1. If you want to search for the intersection (or union) of the keys, just build that set first:

请注意,这是不对称的,因为它会查找properties1中存在值的值。如果要搜索键的交集(或联合),只需先构建该组:

Set<String> keys = new HashSet<>(properties1.keySet());
// For intersection:
keys.retainAll(properties2.keySet());
// For union:
// keys.addAll(properties2.keySet());

for (String key : keys) { ... }

#2


0  

String value=p1.getProperty("x");
if (value == null) {
                    if (!(p2.getProperty("x")==null && p2.containsKey("x")))
                        return true;
} else {
                    if (!value.equals(p2.getProperty("x")))
                        return true;
}

#1


3  

Properties is a subclass of HashTable, which overrides equals. You can simply compare the instances using equals:

Properties是HashTable的子类,它会覆盖equals。您可以使用equals简单地比较实例:

properties1.equals(properties2)

But this won't tell you what is different.

但这不会告诉你有什么不同。

To do that, you can get the keys using properties.keySet(), and then compare the values between the two instances:

为此,您可以使用properties.keySet()获取密钥,然后比较两个实例之间的值:

for (String key : properties1.keySet()) {
  String value1 = properties1.get(key);
  String value2 = properties2.get(key);

  // Compare, e.g. value1.equals(value2).
  // But may need to take into account missing values.
}

Note that this is asymmetrical, in the sense that it looks for the values for which values exist in properties1. If you want to search for the intersection (or union) of the keys, just build that set first:

请注意,这是不对称的,因为它会查找properties1中存在值的值。如果要搜索键的交集(或联合),只需先构建该组:

Set<String> keys = new HashSet<>(properties1.keySet());
// For intersection:
keys.retainAll(properties2.keySet());
// For union:
// keys.addAll(properties2.keySet());

for (String key : keys) { ... }

#2


0  

String value=p1.getProperty("x");
if (value == null) {
                    if (!(p2.getProperty("x")==null && p2.containsKey("x")))
                        return true;
} else {
                    if (!value.equals(p2.getProperty("x")))
                        return true;
}