AntoniRyszard656
asked on
Java Hashmap:
Hi
I am trying to write a simple hashmap program, to understand its use. I managed to add some objects to the hashmap, and some edges using this code. Now I would like to write a removeObject method and understand this involves testing if there are any edges then removing the object x set value from all the keysets. Could anyone look over my removeObject method here, tell me where I am going wrong?
Thanks
((Set)theMap.get(x)).add(y );
((Set)theMap.get(y)).add(x );
HashMap theMap = new HashMap();
boolean removeObject(Object x){
boolean boolAns = containsObject(x);
if(boolAns == true){
((Set)theMap.get(x));
boolean boolEdge = containsEdge(x, y)
if(boolEdge == true){
Set keys = theMap.keySet();
Iterator keyIter = keys.iterator();
while(keyIter.hasNext){
//remove set values
}
theMap.remove(x);
}
else{
theMap.remove(x);
}
}
return boolAns;
}
I am trying to write a simple hashmap program, to understand its use. I managed to add some objects to the hashmap, and some edges using this code. Now I would like to write a removeObject method and understand this involves testing if there are any edges then removing the object x set value from all the keysets. Could anyone look over my removeObject method here, tell me where I am going wrong?
Thanks
((Set)theMap.get(x)).add(y
((Set)theMap.get(y)).add(x
HashMap theMap = new HashMap();
boolean removeObject(Object x){
boolean boolAns = containsObject(x);
if(boolAns == true){
((Set)theMap.get(x));
boolean boolEdge = containsEdge(x, y)
if(boolEdge == true){
Set keys = theMap.keySet();
Iterator keyIter = keys.iterator();
while(keyIter.hasNext){
//remove set values
}
theMap.remove(x);
}
else{
theMap.remove(x);
}
}
return boolAns;
}
think you need to post a little more code...
Going through the code, I'm a little confused. You have a call to ((Set)theMap.get(x)); which doesn't actually do anything, but I'm presuming you wanted to store the resultant Set into a variable?
What you then have is a call to another method (code would help) that determines if the passed map, and some undefined variable y, contains an edge. If it does, then you completely empty the HashMap (which you could have done with theMap.clear(), unless there is some specific tidy-up code or something?), and then you try to remove the object from the HashMap, but if it contained an edge, the HashMap would now be empty.
What you then have is a call to another method (code would help) that determines if the passed map, and some undefined variable y, contains an edge. If it does, then you completely empty the HashMap (which you could have done with theMap.clear(), unless there is some specific tidy-up code or something?), and then you try to remove the object from the HashMap, but if it contained an edge, the HashMap would now be empty.
ASKER
I have posted the main methods, the program adds objects to a hashmap, and allows the objects to linked by an edge. The problem is when a object is removed from the hashmap, the edges are broken, and I think I would need to check in remove Object if the edge exists and remove all the links. I think:
x/y y/x y/x then I need to remove all the x links before removing the object x.
void addObject(Object x){
boolean boolAns = containsNode(x);
if(boolAns != true){
theMap.put(x, new HashSet());
}
}
boolean removeObject(Object x){
boolean boolAns = containsNode(x);
if(boolAns == true){
((Set)theMap.get(x));
boolean boolEdge = containsObjectEdge(x, y)
if(boolEdge == true){
Set keys = theMap.keySet();
Iterator keyIter = keys.iterator();
while(keyIter.hasNext){
//remove set values
}
theMap.remove(x);
}
else{
theMap.remove(x);
}
}
return boolAns;
}
void addObjectEdge(Object x, Object y){
boolean boolAns = theMap.containsKey(x);
if(boolAns == false){
addObject(x);
}
boolAns = theMap.containsKey(y);
if(boolAns == false){
addObject(y);
}
((Set)theMap.get(x)).add(y );
((Set)theMap.get(y)).add(x );
}
x/y y/x y/x then I need to remove all the x links before removing the object x.
void addObject(Object x){
boolean boolAns = containsNode(x);
if(boolAns != true){
theMap.put(x, new HashSet());
}
}
boolean removeObject(Object x){
boolean boolAns = containsNode(x);
if(boolAns == true){
((Set)theMap.get(x));
boolean boolEdge = containsObjectEdge(x, y)
if(boolEdge == true){
Set keys = theMap.keySet();
Iterator keyIter = keys.iterator();
while(keyIter.hasNext){
//remove set values
}
theMap.remove(x);
}
else{
theMap.remove(x);
}
}
return boolAns;
}
void addObjectEdge(Object x, Object y){
boolean boolAns = theMap.containsKey(x);
if(boolAns == false){
addObject(x);
}
boolAns = theMap.containsKey(y);
if(boolAns == false){
addObject(y);
}
((Set)theMap.get(x)).add(y
((Set)theMap.get(y)).add(x
}
ASKER
Can anyone offer guidance with removeObject method?
ASKER
I have posted the main methods, the program adds objects to a hashmap, and allows the objects to linked by an edge. The problem is when a object is removed from the hashmap, the edges are broken, and I think I would need to check in remove Object if the edge exists and remove all the links. I think:
x/y y/x y/x then I need to remove all the x links before removing the object x.
void addObject(Object x){
boolean boolAns = containsNode(x);
if(boolAns != true){
theMap.put(x, new HashSet());
}
}
boolean removeObject(Object x){
boolean boolAns = containsNode(x);
if(boolAns == true){
((Set)theMap.get(x));
boolean boolEdge = containsObjectEdge(x, y)
if(boolEdge == true){
Set keys = theMap.keySet();
Iterator keyIter = keys.iterator();
while(keyIter.hasNext){
//remove set values
}
theMap.remove(x);
}
else{
theMap.remove(x);
}
}
return boolAns;
}
void addObjectEdge(Object x, Object y){
boolean boolAns = theMap.containsKey(x);
if(boolAns == false){
addObject(x);
}
boolAns = theMap.containsKey(y);
if(boolAns == false){
addObject(y);
}
((Set)theMap.get(x)).add(y );
((Set)theMap.get(y)).add(x );
}
x/y y/x y/x then I need to remove all the x links before removing the object x.
void addObject(Object x){
boolean boolAns = containsNode(x);
if(boolAns != true){
theMap.put(x, new HashSet());
}
}
boolean removeObject(Object x){
boolean boolAns = containsNode(x);
if(boolAns == true){
((Set)theMap.get(x));
boolean boolEdge = containsObjectEdge(x, y)
if(boolEdge == true){
Set keys = theMap.keySet();
Iterator keyIter = keys.iterator();
while(keyIter.hasNext){
//remove set values
}
theMap.remove(x);
}
else{
theMap.remove(x);
}
}
return boolAns;
}
void addObjectEdge(Object x, Object y){
boolean boolAns = theMap.containsKey(x);
if(boolAns == false){
addObject(x);
}
boolAns = theMap.containsKey(y);
if(boolAns == false){
addObject(y);
}
((Set)theMap.get(x)).add(y
((Set)theMap.get(y)).add(x
}
ASKER
I have posted the main methods, the program adds objects to a hashmap, and allows the objects to linked by an edge. The problem is when a object is removed from the hashmap, the edges are broken, and I think I would need to check in remove Object if the edge exists and remove all the links. I think:
x/y y/x y/x then I need to remove all the x links before removing the object x.
void addObject(Object x){
boolean boolAns = containsNode(x);
if(boolAns != true){
theMap.put(x, new HashSet());
}
}
boolean removeObject(Object x){
boolean boolAns = containsNode(x);
if(boolAns == true){
((Set)theMap.get(x));
boolean boolEdge = containsObjectEdge(x, y)
if(boolEdge == true){
Set keys = theMap.keySet();
Iterator keyIter = keys.iterator();
while(keyIter.hasNext){
//remove set values
}
theMap.remove(x);
}
else{
theMap.remove(x);
}
}
return boolAns;
}
void addObjectEdge(Object x, Object y){
boolean boolAns = theMap.containsKey(x);
if(boolAns == false){
addObject(x);
}
boolAns = theMap.containsKey(y);
if(boolAns == false){
addObject(y);
}
((Set)theMap.get(x)).add(y );
((Set)theMap.get(y)).add(x );
}
x/y y/x y/x then I need to remove all the x links before removing the object x.
void addObject(Object x){
boolean boolAns = containsNode(x);
if(boolAns != true){
theMap.put(x, new HashSet());
}
}
boolean removeObject(Object x){
boolean boolAns = containsNode(x);
if(boolAns == true){
((Set)theMap.get(x));
boolean boolEdge = containsObjectEdge(x, y)
if(boolEdge == true){
Set keys = theMap.keySet();
Iterator keyIter = keys.iterator();
while(keyIter.hasNext){
//remove set values
}
theMap.remove(x);
}
else{
theMap.remove(x);
}
}
return boolAns;
}
void addObjectEdge(Object x, Object y){
boolean boolAns = theMap.containsKey(x);
if(boolAns == false){
addObject(x);
}
boolAns = theMap.containsKey(y);
if(boolAns == false){
addObject(y);
}
((Set)theMap.get(x)).add(y
((Set)theMap.get(y)).add(x
}
ASKER
Hello all,
I posted this question on hashmaps and hashsets several days ago, but I have never had any further code, after I posted more of the code as suggested.
I would much appreicate any guidance.
I posted this question on hashmaps and hashsets several days ago, but I have never had any further code, after I posted more of the code as suggested.
I would much appreicate any guidance.
Hi AntoniRyszard656,
Sorry, but I'm a bit busy at the moment. I'll try to get back to this question this evening (or tomorrow). I've got the reference to your other question now, so if I've answered the original question you asked here, please accept one of my answers. I'll continue in your other question when I get time.
Thanx.
Sorry, but I'm a bit busy at the moment. I'll try to get back to this question this evening (or tomorrow). I've got the reference to your other question now, so if I've answered the original question you asked here, please accept one of my answers. I'll continue in your other question when I get time.
Thanx.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
;-)