我有一堂有各种布尔和整数的课。
class Animal {
boolean mHappy = false;
boolean mHungry = false;
boolean mSleeping = false;
int mCost = 0;
int mWeight = 0;
boolean isEmpty() {
return !mHappy && !mHungry && !mSleeping && mCost == 0 && mWeight == 0;
}
}
boolean isEmpty() 方法会告诉我所有值是否为空。
现在,我想将我所有的数据移到 HashMaps 中:
class Animal {
HashMap<String, Boolean> mBools = new HashMap<String, Boolean>(){{
put("mHappy", false);
put("mHungry", false);
put("mSleeping", false);
}
};
HashMap<String, Integer> mInts = new HashMap<String, Integer>(){{
put("mCost", 0);
put("mWeight", 0);
}
};
boolean isEmpty() {
// MY QUESTION: How can I make this function iterate through each HashMap,
// regardless of size, and check to make sure it's "false" or "0" like this
// line did when I only was using static booleans and integers?
return !mHappy && !mHungry && !mSleeping && mCost == 0 && mWeight == 0;
}
}
我的问题是关于“boolean isEmpty()”方法,如何让这个函数遍历每个 HashMap,无论大小,并检查以确保每个值是“false”或“0”?