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!
There is a big issue with the code you have written In line
t.moveCursor(((IntPair)obj).getFirst(), ((IntPair)obj).getSecond());
the expression
((IntPair)obj).getFirst()
wont return the getFirst value as obj here is not of type IntPair but obj is a collection of elements IntPair returned by
Object obj = j.values();
so you have to retrieve IntPair element from this collection and then you can read getFirst() I have written a small program to make my point
public static HashMap<IntPair, String> j = new HashMap<IntPair, String>();
public static void main(String[] args) {
j.put(new IntPair(2, 3), "0");
if (j.containsValue("0")) {
Set<Entry<IntPair, String>> pairs = j.entrySet();
Iterator<Entry<IntPair, String>> it = pairs.iterator();
Entry e;
while (it.hasNext()) {
e = it.next();
if (e.getValue().equals("0")) {
IntPair resultObj = (IntPair) e.getKey();
}
}
}
}
Note that the values()
method returns a Collection of the values objects, here a Collection of Strings. You can't cast this to IntPair, and I'm surprised that your attempt is not causing a compiler error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.