I would like to check if two bundles are equal, is there any way to do that instead of checking them key by key?
I have tested Sam's answer and it contains a flaw. Plus I am loving Kotlin at the moment, so here is my version.
Bundle
then test recursively.Code:
fun equalBundles(one: Bundle, two: Bundle): Boolean {
if (one.size() != two.size())
return false
if (!one.keySet().containsAll(two.keySet()))
return false
for (key in one.keySet()) {
val valueOne = one.get(key)
val valueTwo = two.get(key)
if (valueOne is Bundle && valueTwo is Bundle) {
if (!equalBundles(valueOne , valueTwo)) return false
} else if (valueOne != valueTwo) return false
}
return true
}
private static boolean equalsBundles(Bundle a, Bundle b) {
Set<String> aks = a.keySet();
Set<String> bks = b.keySet();
if (!aks.containsAll(bks)) {
return false;
}
for (String key : aks) {
if (!a.get(key).equals(b.get(key))) {
return false;
}
}
return true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With