Reputation: 798
I have two Hashmaps filled here:
Properties properties = new Properties();
try {
properties.load(openFileInput("xmlfilesnames.xml"));
} catch (IOException e) {
e.printStackTrace();
}
for (String key : properties.stringPropertyNames()) {
xmlFileMap.put(key, properties.get(key).toString());
}
try {
properties.load(openFileInput("comparexml.xml"));
} catch (IOException e) {
e.printStackTrace();
}
for (String key : properties.stringPropertyNames()) {
compareMap.put(key, properties.get(key).toString());
}
Declaration:
public Map<String,String> compareMap = new HashMap<>();
public Map<String, String> xmlFileMap = new HashMap<>();
they look like:
How can I check if the job_id
changed of maybe if it's null?
Sometimes the job_id doesn't really exists. So the job_id
is missing in them.
And Sometimes in compareMap
are more then one job_id
How can I compare just the job_id
's and get a boolean
value when compared?
Upvotes: 4
Views: 806
Reputation: 100139
Seems that you want to find the map key based on specific pattern. This can be done by iterating over all keys:
private static String PREFIX = "<job_id>";
private static String SUFFIX = "</job_id>";
public static String extractJobId(Map<String, ?> map) {
for(String key : map.keySet()) {
if(key.startsWith(PREFIX) && key.endsWith(SUFFIX))
return key.substring(PREFIX.length(), key.length()-SUFFIX.length());
}
// no job_id found
return null;
}
If you may have several job_id keys and want to check whether all of them are the same, you can build an intermediate set instead:
public static Set<String> extractJobIds(Map<String, ?> map) {
Set<String> result = new HashSet<>();
for(String key : map.keySet()) {
if(key.startsWith(PREFIX) && key.endsWith(SUFFIX))
result.add(key.substring(PREFIX.length(), key.length()-SUFFIX.length()));
}
return result;
}
Now you can use this method to compare job_id of different maps:
if(Objects.equals(extractJobIds(xmlFileMap), extractJobIds(compareMap))) {
// ...
}
Upvotes: 3