简体   繁体   English

从 hashmap 中的 arraylist 中删除 Object

[英]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.我需要 Graph 中的一种方法来从 hashmap 中的 ArrayList 中删除 Arch。

I tried like this in the class Graph:我在 class 图表中这样尝试:


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.但是当我在我的测试主体中对其进行测试时,结果是 hashmap 中的 ArrayList 没有任何变化。

someone can help me please!请有人可以帮助我!

thank you all谢谢你们

I am 90% certain that your problem's root cause is that your classes are not overriding Object#equals(Object) and Object#hashCode() methods.我 90% 确定您的问题的根本原因是您的类没有覆盖Object#equals(Object)Object#hashCode()方法。 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. 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.但是,总而言之,您的类必须实现这些方法,并且在复杂类(包含其他 object 引用的类)的情况下,这些类中的每一个都必须覆盖这些方法。 The reason why classes like String work in these cases is because the String class overrides these methods.String这样的类在这些情况下起作用的原因是String class 覆盖了这些方法。 For your custom classes, you must do the same.对于您的自定义类,您必须这样做。 Think about this logically.从逻辑上思考这个问题。 How could a collection magically remove and object if there is no way for the code to decide which objects are equal to what you are looking for?如果代码无法确定哪些对象与您要查找的对象相等,那么集合如何神奇地删除和 object?

I suspect deleteArch throws a ConcurrentModificationException because you are modifying archsForNode while iterating over it:我怀疑deleteArch会抛出ConcurrentModificationException ,因为您在迭代它时正在修改archsForNode

   for(Arch<N,T> a : archsForNode){                                        
        if(a.start==start || a.end == end) {                                
            archsForNode.remove(a);                                         
        }                                                                                                                                             
    } 

This is not allowed.这是不允许的。
For more information see this Q&A.有关更多信息,请参阅问答。

ArrayList 之谜<object>在 HashMap<div id="text_translate"><p> 我只是在运行下面的代码,我对结果感到惊讶。 有人可以解释一下这里发生了什么吗? My question is.. when I set a value using an object setter into a new copy of original arraylist, why is the object value in the original arrayList also getting changed?</p><p> 如果我在这里做错了什么,请告诉我。</p><pre> public class TestHashMap { public static void main(String[] args) { HashMap&lt;String, ArrayList&lt;ResultData&gt;&gt; hmData = new HashMap&lt;String, ArrayList&lt;ResultData&gt;&gt;(); ArrayList&lt;ResultData&gt; arrData = new ArrayList&lt;ResultData&gt;(); for (int i = 0; i &lt; 10; i++) { ResultData rData = new ResultData(); rData.setDy(i + 1); arrData.add(rData); } hmData.put("test",arrData); System.out.println("Before &gt;&gt;&gt;&gt; "+hmData.get("test").get(1).getDy()); ArrayList&lt;ResultData&gt; newList = new ArrayList&lt;ResultData&gt;(); newList = hmData.get("test"); ResultData rNew = newList.get(1); rNew.setDy(1234566); System.out.println("##### &gt;&gt;&gt;&gt;&gt; "+arrData.get(1).getDy()); System.out.println("After &gt;&gt;&gt;&gt; "+hmData.get("test").get(1).getDy()); }}</pre></div></object> - Mystery with ArrayList<Object> in a HashMap

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从ArrayList中删除dulpicates <HashMap<String,Object> &gt;在Java中? - How to remove the dulpicates from ArrayList<HashMap<String,Object>> in java? 如何从 ArrayList 中删除项目<HashMap<String, Object> &gt; - How to remove an item from ArrayList<HashMap<String, Object>> 从哈希图中的数组列表中删除一个值 - Remove a value from a arraylist who is in a hashmap 从ArrayList中删除一个对象 - remove an object from an ArrayList 从ArrayList中删除对象 - Remove Object From ArrayList 如何将 String 对象保存到 ArrayList 并删除此 ArrayList 中的 HashMap <HashMap<String, String> &gt;? - How to save String object into an ArrayList and also remove the HashMap inside of this ArrayList<HashMap<String, String>>? 从 object ArrayList 中删除 object - Remove object from object ArrayList 可序列化的对象到Hashmap的Arraylist - Serializable object to Arraylist of Hashmap ArrayList 之谜<object>在 HashMap<div id="text_translate"><p> 我只是在运行下面的代码,我对结果感到惊讶。 有人可以解释一下这里发生了什么吗? My question is.. when I set a value using an object setter into a new copy of original arraylist, why is the object value in the original arrayList also getting changed?</p><p> 如果我在这里做错了什么,请告诉我。</p><pre> public class TestHashMap { public static void main(String[] args) { HashMap&lt;String, ArrayList&lt;ResultData&gt;&gt; hmData = new HashMap&lt;String, ArrayList&lt;ResultData&gt;&gt;(); ArrayList&lt;ResultData&gt; arrData = new ArrayList&lt;ResultData&gt;(); for (int i = 0; i &lt; 10; i++) { ResultData rData = new ResultData(); rData.setDy(i + 1); arrData.add(rData); } hmData.put("test",arrData); System.out.println("Before &gt;&gt;&gt;&gt; "+hmData.get("test").get(1).getDy()); ArrayList&lt;ResultData&gt; newList = new ArrayList&lt;ResultData&gt;(); newList = hmData.get("test"); ResultData rNew = newList.get(1); rNew.setDy(1234566); System.out.println("##### &gt;&gt;&gt;&gt;&gt; "+arrData.get(1).getDy()); System.out.println("After &gt;&gt;&gt;&gt; "+hmData.get("test").get(1).getDy()); }}</pre></div></object> - Mystery with ArrayList<Object> in a HashMap 从ArrayList中删除重复项 <ArrayList<String> &gt;对象 - Remove Duplicates from ArrayList<ArrayList<String>> Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM