I have an IntPair class which has two methods I use from it: "getFirst()" and "getSecond()". While in this method I'm currently working, I want to check if "hashMap j" contains a specific value and then do the actions. I think I have the problem in these lines:
Object obj = j.values();
t.moveCursor(((IntPair)obj).getFirst(), ((IntPair)obj).getSecond());
I don't know if I'm casting ok to the Object, or if the first line "Object obj = j.values()" should be replaced with another method call. I tested with a System.out.print ("Message") after j.containsValue("0") and I got the message back.
This is a part of the method I'm trying to make it work.
public static HashMap<IntPair, String> j = new HashMap<>();
j.put(new IntPair(firstInt, secondInt), value);
if (j.containsValue("0"))
{
Object obj = j.values();
t.moveCursor(((IntPair)obj).getFirst(), ((IntPair)obj).getSecond());
t.putCharacter('x');
}
else if (j.containsValue("1"))
{
Object obj = j.values();
t.moveCursor(((IntPair)obj).getFirst(), ((IntPair)obj).getSecond());
t.putCharacter('v');
}
IntPair Class:
public class IntPair {
private final int first;
private final int second;
public IntPair(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public int hashCode() {
int hash = 3;
hash = 89 * hash + this.first;
hash = 89 * hash + this.second;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final IntPair other = (IntPair) obj;
if (this.first != other.first) {
return false;
}
if (this.second != other.second) {
return false;
}
return true;
}
public int getFirst() {
return first;
}
public int getSecond() {
return second;
}
}
Any help would be really appreciated. Thank you!
"this is the method...". 2) I assume (and certainly hope) that your IntPair class overrides equals and hashCode() appropriately, correct? 3) I am not sure what exactly it is you're asking or that you're stuck on. You mention a question about casting -- is your attempt to cast causing an error or exception?