Suppose you have 2 Properties objects. One contains master properties, the other one is a target. Your job is to compare the two.
masterValue = masterProperties.getProperty(masterKey);
for (Properties targetFileProperty : targetFileList) {
if (targetFileProperty.containsKey(masterKey)) {
targetValue = targetFileProperty.getProperty(masterKey);
if (masterValue.equals(targetValue)) { //<---- this is where the problem is
// do something clever
} else {
// do something clever
The problem i am facing in this example is this:
When master key is “A” and master value is “10” and target key is “A” and “target key is ” 10 “, code above thinks that these are the same. In other words it is either trimming or ignoring white space.
Can you eithe point out an error in my logic or suggest a better way to assert that white space is not to be ignored? Thank you.
java.util.Propertiesinherits itsequals(Object)fromHashtable, which implementsMapequality:So if you want to compare if two
Propertiescontain the same keys and values, you can just doprops1.equals(props2).As for your problem, I don’t think you’ve identified the real problem.
" 10 ".equals("10")is definitelyfalse. The problem may be that the strings were trimmed before entered as values to theProperties(you can print the values that you’re comparing to see if this is indeed the case).If the whitespaces are significant, you must escape it in the properties file. Here’s a snippet to show how it’s done:
This prints (I’ve substituted
_for space for clarity):