Java 8 使用Java8删除列表中特定索引处的列

Java 8 使用Java8删除列表中特定索引处的列,java-8,Java 8,我有一个元素列表(datalst),我想根据另一个列表(removeIndex)删除其中的一些索引元素,我如何在Java8中执行该操作。请帮忙 private static List<List> removeUnwantedRecords(List<List> datalst, List<Integer> removeIndex ){ datalst.forEach( row-> { for(int counte

我有一个元素列表(
datalst
),我想根据另一个列表(
removeIndex
)删除其中的一些索引元素,我如何在
Java8
中执行该操作。请帮忙

private static List<List> removeUnwantedRecords(List<List> datalst, List<Integer> removeIndex ){
         datalst.forEach( row-> {
            for(int counter = 0 ; counter< removeIndex.size();counter++) {
                 List<Object> evenIndexedNames1 = IntStream
                          .range(0, row.size())
                          .filter(i -> i != 4 )
                          .mapToObj(i -> row.get(i))
                          .collect(Collectors.toList());

                 evenIndexedNames1.forEach(str-> System.out.print(str));
            }
     });
    datalst.forEach(str-> System.out.println(str));
    return datalst;
}
private static List removeUnventedRecords(List datalst,List removeIndex){
datalst.forEach(行->{
对于(int counter=0;计数器i!=4)
.mapToObj(i->row.get(i))
.collect(Collectors.toList());
evenIndexedNames1.forEach(str->System.out.print(str));
}
});
datalst.forEach(str->System.out.println(str));
返回数据LST;
}
这很好,但不是4,我想迭代一个列表(
removeIndex
),您能在
Java8
中建议其他方法吗


  • 喜欢泛型而不是原始类型(
    List实际上,通过循环通过索引删除列表元素是不安全的

    dataList.forEach(list -> list.removeAll(removeIndex
                .stream()
                .filter(i -> i < list.size())
                .map(list::get).collect(Collectors.toList()))
    );
    
    dataList.forEach(list->list.removeAll(removeIndex
    .stream()
    .filter(i->i

    事实上,当您从列表中删除元素时,元素的索引正在修改,它仅适用于第一个索引,其他索引不正确。

    这是有效的Java 8代码。任务accomplished@Michael,它是如何完成的??用户询问
    这很好,但不是4…他想要…
    。我想要迭代或loop通过removeIndex列表并从datalst中删除该索引处的所有元素。将
    i!=4
    替换为
    !removeIndex.contains(i)
    @HadiJ,这非常危险。我不知道这是否会调用
    remove(Object)
    remove(int)
    dataList.forEach(list -> list.removeAll(removeIndex
                .stream()
                .filter(i -> i < list.size())
                .map(list::get).collect(Collectors.toList()))
    );