简体   繁体   English

无法从数组列表中删除哈希映射

[英]Cannot remove hash map from array list

public class PurchaseOrderWrapper extends ArrayList<HashMap<String, String>> {

        public boolean contains(Object o) {
            HashMap<String, String> ob = (HashMap<String, String>) o;
            for (HashMap<String, String> map : this) {
                Log.i("kasun",ob.get("part_no")+" "+ map.get("part_no")+" = "+Boolean.toString(ob.get("part_no").equalsIgnoreCase(map.get("part_no"))));
                 if(ob.get("part_no").equalsIgnoreCase(map.get("part_no"))){
                     return true;
                 }
            }
        return false;
    }
}

if (wrapper.contains(removitems)){      
    wrapper.remove(removitems);     
} 

Wrapper is a instance of a PurchaseOrderWrapper class.it contains Hash Map with with 5 attributes including part no.remove items also contains 4 items inclding part no as i mention on the above example.So i need to remove the hashmap from Wrapper instance which has same part no in remove items hash map.But it does not work Wrapper是PurchaseOrderWrapper类的一个实例。它包含具有5个属性的Hash Map,其中包括部件号.remove项目还包含4个项目,其中包括部件号,如我在上面的示例中提到的。因此,我需要从具有以下属性的Wrapper实例中删除哈希图删除项目哈希图中没有相同的部分,但是不起作用

You're extending ArrayList (which you should never do), and breaking its contract. 您正在扩展ArrayList(永远不要这样做),并破坏其合同。

You override contains() by saying that your list contains a given object if this object is a HashMap and happens to contain the same value for the "part_no" key as another map in the list. 如果此对象是HashMap且您的列表包含一个给定的对象,并且您碰巧包含的“ part_no”键值与列表中的另一个映射相同,则可以覆盖contains() But you don't override remove(). 但是,您不会覆盖remove()。 And remove() keeps using the ArrayList's method, which removes the element in the list that is equal to the object passed as argument. 并且remove()继续使用ArrayList的方法,该方法删除列表中与作为参数传递的对象相等的元素。

Don't, ever, extend the collection classes. 永远不要扩展收集类。 Instead, wrap them inside your own class. 而是将它们包装在您自己的类中。

To implement the remove method, you'll probably have to iterate over the list, find the map in the list which has the same value for the "part_no" key as the map passed as argument, and remove that map from the list. 要实现remove方法,您可能必须遍历列表,在列表中找到与作为参数传递的映射具有相同的“ part_no”键值的映射,然后从列表中移除该映射。

I have the suspicion that you're using Maps where you should define your own class ad well, but that doesn't change much. 我怀疑您在使用Google Maps时应该很好地定义自己的类广告,但这并没有太大变化。

Here's the skeleton of the class that you should have. 这是您应该拥有的课程的骨架。 You should figure out the implementation by yourself based on the above indications: 您应该根据上述指示自行确定实现方式:

public class PurchaseOrderWrapper {
    private List<Map<String, String>> list = new ArrayList<>();

    public boolean containsElementWithSamePartNoAs(Map<String, String> map) {
        // iterate over the list and find if an element has the same part_no
        // as the map passed as argument
    }

    public void removeElementWithSamePartNoAs(Map<String, String> map) {
        // iterate over the list and find the element which has the same part_no
        // as the map passed as argument. Once found, remove this element from the list
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM