Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java从列表中删除符合某些复制规则的元素_Java - Fatal编程技术网

java从列表中删除符合某些复制规则的元素

java从列表中删除符合某些复制规则的元素,java,Java,我有这样一份清单: List<Map<String, String>> list = new ArrayList<Map<String, String>>(); Map<String, String> row; row = new HashMap<String, String>(); row.put("page", "page1"); row.put("section", "section1"); row.put("ind

我有这样一份清单:

List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> row;

row = new HashMap<String, String>();
row.put("page", "page1");
row.put("section", "section1");
row.put("index", "index1");
list.add(row);

row = new HashMap<String, String>();
row.put("page", "page2");
row.put("section", "section2");
row.put("index", "index2");
list.add(row);

row = new HashMap<String, String>();
row.put("page", "page3");
row.put("section", "section1");
row.put("index", "index1");
list.add(row);
更新2:此操作失败,但出现相同的异常:

Iterator<Map<String, String>> it = list.iterator();
while (it.hasNext()) {
    Map<String, String> row = it.next();
    for (Map<String, String> el : list) {
        if (row.get("section").equals(el.get("section")) && row.get("index").equals(el.get("index"))) {
            list.remove(row);
        }
    }
}
Iterator<Map<String, String>> it = list.iterator();
while (it.hasNext()) {
    Map<String, String> row = it.next();
    Iterator<Map<String, String>> innerIt = list.iterator();
    while (innerIt.hasNext()) {
        Map<String, String> el = innerIt.next();
        if (row.get("section").equals(el.get("section")) && row.get("index").equals(el.get("index"))) {
            innerIt.remove();
            //it.remove(); //fails as well
        }
    }
}
Iterator it=list.Iterator();
while(it.hasNext()){
Map row=it.next();
迭代器innerIt=list.Iterator();
while(innerIt.hasNext()){
Map el=innerIt.next();
if(row.get(“section”).equals(el.get(“section”)&&row.get(“index”).equals(el.get(“index”)){
innerIt.remove();
//it.remove();//也会失败
}
}
}
更新3,解决方案:非常简单:

for (int i = 0; i < list.size(); i++) {
    for (int j = 0; j < list.size(); j++) {
        if (list.get(i).get("section").equals(list.get(j).get("section")) && list.get(i).get("index").equals(list.get(j).get("index"))) {
            list.remove(i);
        }
    }
}
for(int i=0;i

更新4:“解决方案”未按预期工作。现在选择了正确答案。

在对集合进行迭代时不能添加/删除集合元素,除非通过

请参见以在地图上获取迭代器

请参阅,了解如何在迭代集合时从集合中删除元素

您可以这样构造代码:

//Get an iterator on your list.
Iterator<Map<String, String>> itr = list.iterator();

//iterate
while(itr.hasNext()) {
  Map<String, String> elt= itr.next();
  if(isDuplicate(list, elt)) {
    itr.remove();
  }
}
这就是抛出ConcurrentModificationException的方法

每次修改列表时,ArrayList都会更新
modCount
。因此,如果在没有迭代器的情况下修改列表,
modCount
将变为!=<代码>预期模块计数
。在下一次调用迭代器的任何方法时,都会得到异常

使用for each循环时,将隐式创建一个迭代器,并在每个循环结束时调用next()


每次通过迭代器中的方法修改列表时,
expectedModCount
将更新为
modCount
,避免出现ConcurrentModificationException。

在迭代时不能添加/删除集合元素,除非通过

请参见以在地图上获取迭代器

请参阅,了解如何在迭代集合时从集合中删除元素

您可以这样构造代码:

//Get an iterator on your list.
Iterator<Map<String, String>> itr = list.iterator();

//iterate
while(itr.hasNext()) {
  Map<String, String> elt= itr.next();
  if(isDuplicate(list, elt)) {
    itr.remove();
  }
}
这就是抛出ConcurrentModificationException的方法

每次修改列表时,ArrayList都会更新
modCount
。因此,如果在没有迭代器的情况下修改列表,
modCount
将变为!=<代码>预期模块计数。在下一次调用迭代器的任何方法时,都会得到异常

使用for each循环时,将隐式创建一个迭代器,并在每个循环结束时调用next()


每次通过迭代器中的方法修改列表时,
expectedModCount
都会更新为
modCount
,从而避免出现ConcurrentModificationException。

保留要删除的元素列表,并在之后删除它们

List<Map<String, String> removeList = new List<Map<String, String>();
for (Map<String, String> row : list) 
    for (Map<String, String> el : list)
        if (row.get("section").equals(el.get("section")) && row.get("index").equals(el.get("index")))
            removeList.add( el );


 for( Map< String, String > i : removeList )
     list.remove( i );

List保留一个要删除的元素列表,并在删除后删除它们

List<Map<String, String> removeList = new List<Map<String, String>();
for (Map<String, String> row : list) 
    for (Map<String, String> el : list)
        if (row.get("section").equals(el.get("section")) && row.get("index").equals(el.get("index")))
            removeList.add( el );


 for( Map< String, String > i : removeList )
     list.remove( i );

List一种方法是复制要修改的HashMap。迭代副本并更改原始副本

试试这个

Map<String, String> copyOfList = new HashMap<String, String>(list);

for (Map<String, String> row : copyOfList ) {
    for (Map<String, String> el : copyOfList ) {
        if (row.get("section").equals(el.get("section")) && row.get("index").equals(el.get("index"))) {
            list.remove(el);
        }
    }
}
Map copyOfList=新的HashMap(列表);
用于(地图行:复制列表){
用于(地图el:copyOfList){
if(row.get(“section”).equals(el.get(“section”)&&row.get(“index”).equals(el.get(“index”)){
列表。删除(el);
}
}
}

一种方法是复制要修改的HashMap。迭代副本并更改原始副本

试试这个

Map<String, String> copyOfList = new HashMap<String, String>(list);

for (Map<String, String> row : copyOfList ) {
    for (Map<String, String> el : copyOfList ) {
        if (row.get("section").equals(el.get("section")) && row.get("index").equals(el.get("index"))) {
            list.remove(el);
        }
    }
}
Map copyOfList=新的HashMap(列表);
用于(地图行:复制列表){
用于(地图el:copyOfList){
if(row.get(“section”).equals(el.get(“section”)&&row.get(“index”).equals(el.get(“index”)){
列表。删除(el);
}
}
}

如果显式使用迭代器,可以从中删除。您将无法将其与“foreach”循环或其他迭代器组合,除非在这种情况下,一旦找到匹配项,内部循环将结束

[注意:我修复了该条件,因为它不排除自匹配。]

Iterator<Map<String,String>> outerIt = list.iterator();
while (outerIt.hasNext()) {
    Map<String,String> outer = outerIt.next();

    for (Map<String, String> inner : list) {
        if ((inner != outer) && outer.get("section").equals(inner.get("section")) && outer.get("index").equals(inner.get("index"))) {
            // Match;  de-dup.
            //   -- no longer iterating the 'inner' loop, so we don't need a copy.
            outerIt.remove();
            break;
        }
    }
}
Iterator outerIt=list.Iterator();
while(outerIt.hasNext()){
Map outer=outerIt.next();
用于(地图内部:列表){
如果((inner!=outer)和&outer.get(“section”).equals(inner.get(“section”))和&outer.get(“index”).equals(inner.get(“index”)){
//匹配;重复数据消除。
//--不再迭代“内部”循环,因此不需要副本。
outerIt.remove();
打破
}
}
}

对于无法精确构造内部迭代的情况,最简单的方法是在循环开始之前复制原始列表&只需迭代副本即可保证稳定可靠的迭代。

如果显式使用迭代器,可以从中删除。您将无法将其与“foreach”循环或其他迭代器组合,除非在这种情况下,一旦找到匹配项,内部循环将结束

[注意:我修复了该条件,因为它不排除自匹配。]

Iterator<Map<String,String>> outerIt = list.iterator();
while (outerIt.hasNext()) {
    Map<String,String> outer = outerIt.next();

    for (Map<String, String> inner : list) {
        if ((inner != outer) && outer.get("section").equals(inner.get("section")) && outer.get("index").equals(inner.get("index"))) {
            // Match;  de-dup.
            //   -- no longer iterating the 'inner' loop, so we don't need a copy.
            outerIt.remove();
            break;
        }
    }
}
Iterator outerIt=list.Iterator();
while(outerIt.hasNext()){
Map outer=outerIt.next();
用于(地图内部:列表){
如果((inner!=outer)和&outer.get(“section”).equals(inner.get(“section”))和&outer.get(“index”).equals(inner.get(“index”)){
//匹配;重复数据消除。
//--不再迭代“内部”循环,因此不需要副本。
outerIt.remove();
打破
}
}
}

对于无法精确构造内部迭代的情况,最简单的方法是在循环开始之前复制原始列表,只需迭代副本即可确保稳定可靠的迭代。

您可以使用传统的For循环,而不使用其他循环

   for(int i=0;i<list.size();i++)
    {

    for(int j=0;j<list.size();j++)
    {

     if (list.get(i).get("section").equals(list.get(j).get("section")) && list.get(i).get("index").equals(list.get(j).get("index"))) {

            list.remove(j);
            j -= 1 ;
      }

    }

    }

for(inti=0;i您可以使用传统的for循环,而不使用其他循环

   for(int i=0;i<list.size();i++)
    {

    for(int j=0;j<list.size();j++)
    {

     if (list.get(i).get("section").equals(list.get(j).get("section")) && list.get(i).get("index").equals(list.get(j).get("index"))) {

            list.remove(j);
            j -= 1 ;
      }

    }

    }
for(inti=0;i这里是一个简单的l