Skip to content
Advertisement

Remove an Object from an arraylist in a hashmap

I need your help, I created this two classes

public class Arch <N,T>{
    T label;
    N start;
    N end;
...constructor...

public class Graph <N,T>{
    private ArrayList<N> nodes;                                                                                    
    private HashMap<N,ArrayList<T>> arch;  //for each Node I have saved an arrayList of Archs 
                                            //connected to him                                                                        
    private int nNode;                                                                                             
    private int nArch;                                                                                             
    private boolean oriented;
...constructor..

I need a method in Graph to delete an Arch from the ArrayList that I have in the hashmap.

I tried like this in the class Graph:

public void deleteArch(N start, N end){                           
    ArrayList<Arch<N,T>> archsForNode=(ArrayList<Arch<N,T>>)arch.get(start);
    for(Arch<N,T> a : archsForNode){                                        
        if(a.start==start || a.end == end) {                                
            archsForNode.remove(a);                                         
        }                                                                   
                                                                            
    }                                                                       
} 

but when I tested it in my test main the result is that nothing changed in my ArrayList in the hashmap.

someone can help me please!

thank you all

Advertisement

Answer

I am 90% certain that your problem’s root cause is that your classes are not overriding Object#equals(Object) and Object#hashCode() methods. In order for a remove() function to work, it needs to compare the object being passed to the function against members of the collection you are iterating over so that the object that’s equal to the argument is removed.

I suggest you research overriding these methods for proper implementation. But, in summary, your classes must implement these methods and in the case of complex classes (classes containing other object references) each one of these classes must override these methods. The reason why classes like String work in these cases is because the String class overrides these methods. For your custom classes, you must do the same. Think about this logically. How could a collection magically remove an object if there is no way for the code to decide which objects are equal to what you are looking for?

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement