如何将东西放在哈希映射中并使用具有相同字段值的新实例来获取相应的值
How to put stuff in a hash map and use new instances with the same value of fields to get the corresponding value
我正在开发一个 android 应用程序来帮助小孩子学习数学。它会显示一些问题,用户可以回答这些问题。在开始页面会有一些选项可供选择。例如,位数和运算类型(即+、-、*、+/-)以及是否启用定时器。所以我创建了一个QuestionOptions
class。
public class QuestionOptions implements Parcelable{
protected QuestionOptions(Parcel in) {
digitCount = in.readInt ();
}
public static final Creator<QuestionOptions> CREATOR = new Creator<QuestionOptions> () {
@Override
public QuestionOptions createFromParcel(Parcel in) {
return new QuestionOptions (in);
}
@Override
public QuestionOptions[] newArray(int size) {
return new QuestionOptions[size];
}
};
public QuestionOptions (OperationType operationType, int digitCount, boolean timerEnabled) {
this.operationType = operationType;
this.digitCount = digitCount;
this.timerEnabled = timerEnabled;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable (this, flags);
}
public enum OperationType {
ADDITION,
SUBTRACTION,
ADD_AND_SUB,
MULTIPLICATION
}
public boolean isTimerEnabled() {
return timerEnabled;
}
public void setTimerEnabled(boolean timerEnabled) {
this.timerEnabled = timerEnabled;
}
public OperationType getOperationType() {
return operationType;
}
public void setOperationType(OperationType operationType) {
this.operationType = operationType;
}
public int getDigitCount() {
return digitCount;
}
public void setDigitCount(int digitCount) {
this.digitCount = digitCount;
}
private OperationType operationType;
private int digitCount;
private boolean timerEnabled;
}
请注意,我实现 Parcelable 是因为我想将它发送给另一个 activity。
到目前为止,一切都很好。但是后来我想添加一个功能来给用户奖励。现在我在网上找了一些照片作为奖杯之类的。以下是我希望他们的工作方式:对于每个可能的问题选项,都有不同的奖励。经过一番计算,我发现有22个可能的问题选项!所以我决定用一个hashmap来存储可能的问题选项(key)和奖杯图片的资源id(value)。
因为这是我第一次使用哈希映射,所以我用它做了一些实验,很快发现我不能使用它。
HashMap.get
方法比较问题选项的引用而不是其中的字段。这是我的测试:
HashMap<QuestionOptions, Integer> map = new HashMap<> ();
map.put(new QuestionOptions (QuestionOptions.OperationType.ADDITION,
1, false), 0);
map.put(new QuestionOptions (QuestionOptions.OperationType.ADDITION,
2, false), 1);
map.put(new QuestionOptions (QuestionOptions.OperationType.ADDITION,
3, false), 2);
System.out.println (map.get(
new QuestionOptions(QuestionOptions.OperationType.ADDITION,
1, false)));
并打印 "null"...
看完了,你们一定睡着了...我只想问:
如何将所有可能的问题选项放在一个hash map中,并使用新实例将它们取出来,或者有其他方法可以实现要求吗?如果是,如何?
您需要实现 QuestionOptions 的 hashcode() 和 equals() 方法。正如你所说
The HashMap.get method compares the references.
这是 HashMap 用来比较键的 hashCode() 方法的默认实现
您可以阅读更多关于 hashCode and equals method here
我正在开发一个 android 应用程序来帮助小孩子学习数学。它会显示一些问题,用户可以回答这些问题。在开始页面会有一些选项可供选择。例如,位数和运算类型(即+、-、*、+/-)以及是否启用定时器。所以我创建了一个QuestionOptions
class。
public class QuestionOptions implements Parcelable{
protected QuestionOptions(Parcel in) {
digitCount = in.readInt ();
}
public static final Creator<QuestionOptions> CREATOR = new Creator<QuestionOptions> () {
@Override
public QuestionOptions createFromParcel(Parcel in) {
return new QuestionOptions (in);
}
@Override
public QuestionOptions[] newArray(int size) {
return new QuestionOptions[size];
}
};
public QuestionOptions (OperationType operationType, int digitCount, boolean timerEnabled) {
this.operationType = operationType;
this.digitCount = digitCount;
this.timerEnabled = timerEnabled;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable (this, flags);
}
public enum OperationType {
ADDITION,
SUBTRACTION,
ADD_AND_SUB,
MULTIPLICATION
}
public boolean isTimerEnabled() {
return timerEnabled;
}
public void setTimerEnabled(boolean timerEnabled) {
this.timerEnabled = timerEnabled;
}
public OperationType getOperationType() {
return operationType;
}
public void setOperationType(OperationType operationType) {
this.operationType = operationType;
}
public int getDigitCount() {
return digitCount;
}
public void setDigitCount(int digitCount) {
this.digitCount = digitCount;
}
private OperationType operationType;
private int digitCount;
private boolean timerEnabled;
}
请注意,我实现 Parcelable 是因为我想将它发送给另一个 activity。 到目前为止,一切都很好。但是后来我想添加一个功能来给用户奖励。现在我在网上找了一些照片作为奖杯之类的。以下是我希望他们的工作方式:对于每个可能的问题选项,都有不同的奖励。经过一番计算,我发现有22个可能的问题选项!所以我决定用一个hashmap来存储可能的问题选项(key)和奖杯图片的资源id(value)。
因为这是我第一次使用哈希映射,所以我用它做了一些实验,很快发现我不能使用它。
HashMap.get
方法比较问题选项的引用而不是其中的字段。这是我的测试:
HashMap<QuestionOptions, Integer> map = new HashMap<> ();
map.put(new QuestionOptions (QuestionOptions.OperationType.ADDITION,
1, false), 0);
map.put(new QuestionOptions (QuestionOptions.OperationType.ADDITION,
2, false), 1);
map.put(new QuestionOptions (QuestionOptions.OperationType.ADDITION,
3, false), 2);
System.out.println (map.get(
new QuestionOptions(QuestionOptions.OperationType.ADDITION,
1, false)));
并打印 "null"...
看完了,你们一定睡着了...我只想问:
如何将所有可能的问题选项放在一个hash map中,并使用新实例将它们取出来,或者有其他方法可以实现要求吗?如果是,如何?
您需要实现 QuestionOptions 的 hashcode() 和 equals() 方法。正如你所说
The HashMap.get method compares the references.
这是 HashMap 用来比较键的 hashCode() 方法的默认实现
您可以阅读更多关于 hashCode and equals method here