我想检查两个捆绑包是否相等,有没有办法做到这一点而不是按密钥检查它们?
答案 0 :(得分:29)
这是测试两个Bundle是否相同的一种方法:
one
中某个键的值可以为null
,因此请确保这两个值均为null
且{em> 键存在于two
中1}} 代码:
public boolean equalBundles(Bundle one, Bundle two) {
if(one.size() != two.size())
return false;
Set<String> setOne = new HashSet<>(one.keySet());
setOne.addAll(two.keySet());
Object valueOne;
Object valueTwo;
for(String key : setOne) {
if (!one.containsKey(key) || !two.containsKey(key))
return false;
valueOne = one.get(key);
valueTwo = two.get(key);
if(valueOne instanceof Bundle && valueTwo instanceof Bundle &&
!equalBundles((Bundle) valueOne, (Bundle) valueTwo)) {
return false;
}
else if(valueOne == null) {
if(valueTwo != null)
return false;
}
else if(!valueOne.equals(valueTwo))
return false;
}
return true;
}
答案 1 :(得分:5)
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;
}
答案 2 :(得分:2)
我已经测试了{{3}}的答案,它包含一个漏洞。此外,我现在很喜欢Kotlin,所以这是我的版本。
Bundle
,则递归测试。代码:
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
}
答案 3 :(得分:0)
为了处理:
我想出了这个:
private static boolean equalBundles(final Bundle left, final Bundle right) {
if (!left.keySet().containsAll(right.keySet()) || !right.keySet().containsAll(left.keySet())) {
return false;
}
for (final String key : left.keySet()) {
final Object leftValue = left.get(key);
final Object rightValue = right.get(key);
if (leftValue instanceof Collection && rightValue instanceof Collection) {
final Collection leftCollection = (Collection) leftValue;
final Collection rightCollection = (Collection) rightValue;
if (leftCollection.size() != rightCollection.size()) {
return false;
}
final Iterator leftIterator = leftCollection.iterator();
final Iterator rightIterator = rightCollection.iterator();
while (leftIterator.hasNext()) {
if (!equalBundleObjects(leftIterator.next(), rightIterator.next())) {
return false;
}
}
} else if (!equalBundleObjects(leftValue, rightValue)) {
return false;
}
}
return true;
}
private static boolean equalBundleObjects(final Object left, final Object right) {
if (left instanceof Bundle && right instanceof Bundle) {
return equalBundles((Bundle) left, (Bundle) right);
} else {
return left == right || (left != null && left.equals(right));
}
}
答案 4 :(得分:-1)
单行方法:
static boolean isEqualBundle(Bundle a, Bundle b) {
return a == b || (a != null && b != null && a.keySet().equals(b.keySet()) && a.keySet().stream().allMatch(s -> Objects.equals(a.get(s), b.get(s))));
}
static boolean isDeepEqualBundle(Bundle a, Bundle b) {
return a == b || (a != null && b != null && a.keySet().equals(b.keySet()) && a.keySet().stream().map(s -> new Pair<>(a.get(s), b.get(s))).allMatch(s -> s.first instanceof Bundle && s.second instanceof Bundle ? isDeepEqualBundle((Bundle) s.first, (Bundle) s.second) : Objects.equals(s.first, s.second)));
}
//测试用例
final Bundle a = new Bundle(), b = new Bundle(), c = new Bundle(), d = new Bundle(), e = new Bundle(), f = new Bundle(), aa = new Bundle(), bb = new Bundle(), cc = new Bundle();
a.putString("1", "A");
b.putString("1", "A");
c.putString("1", "C");
d.putString("1", null);
e.putString("1", null);
f.putString("2", null);
aa.putString("2", "A");
aa.putBundle("2", a);
bb.putString("2", "A");
bb.putBundle("2", b);
cc.putString("2", "C");
cc.putBundle("2", c);
boolean isEquals1 = isEqualBundle(a, b); //true
boolean isEquals2 = isEqualBundle(a, c); //false
boolean isEquals3 = isEqualBundle(d, e); //true
boolean isEquals4 = isEqualBundle(d, f); //false
boolean isEquals5 = isEqualBundle(aa, bb); //false
boolean isEquals6 = isEqualBundle(aa, cc); //false
boolean isEquals7 = isDeepEqualBundle(aa, bb); //true
boolean isEquals8 = isDeepEqualBundle(aa, cc); //false