如何使用java更新和删除多值数组列表中的元素/项列表



这是一个示例代码,

*ArrayList<Movie> list = new ArrayList<Movie>();

list.add(new Movie("Force Awakens", 8.3, 2015));
list.add(new Movie("Star Wars", 8.7, 1977));
list.add(new Movie("Empire Strikes Back", 8.8, 1980));
list.add(new Movie("Return of the Jedi", 8.4, 1983));*
/* etc.... */
*for (Movie empl :list) {
System.out.println(empl);
}*

我的数组列表如下(控制台(eclipse(中的输出(:

电影[评级=8.7,名称=星球大战,年份=1977]

电影[评分=8.8,名称=《帝国反击战》,年份=1980]

电影[评分=8.4,名称=绝地归来,年份=1983]

电影[评分=8.3,名称=原力觉醒,年份=2015]

所以我的问题是如何更新&使用java从列表中删除以上任何一项。我应该在这里应用什么逻辑

p。S:我尝试了java remove((命令,但在下面抛出了一个异常:

线程中的异常"主";java.util.ConcurrentModificationException在java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:937(位于java.base/java.util.ArrayList$Itr.next(ArrayList.java:891(mypackage。MainModule.main(MainModule.java:45(

当不允许同时修改对象时,会发生ConcurrentModificationException。当使用Java Collection类(在您的例子中是ArrayList(时,通常会出现这种异常。

1-使用Collections.synchronizedList()获取列表的线程安全版本:

List<Movie> list = Collections.synchronizedList(yourMoviesArray);

然后在遍历列表时使用迭代器,并在迭代器上调用remove()方法来删除项目:

Iterator<Movie> it = list.iterator();
while(it.hasNext()) {
if(condition) {
it.remove();
}
}

2-使用ArrayList的线程安全变体是CopyOnWriteArrayList

CopyOnWriteArrayList<Movie> list = new CopyOnWriteArrayList<>(); 
list.add(); // add all your movies just like you add in a normal list
// conditional remove elemnts 
list.forEach(e -> {
if (shouldRemove(e))
list.remove(e);
});

您也可以尝试使用lambda表达式。

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class Movie {
private String name;
private Double rate;
private Integer year;
}
class MovieCRUD {
@Test
public void deleteTest() {
ArrayList<Movie> list = new ArrayList<Movie>();
list.add(new Movie("Force Awakens", 8.3, 2015));
list.add(new Movie("Star Wars", 8.7, 1977));
list.add(new Movie("Empire Strikes Back", 8.8, 1980));
list.add(new Movie("Return of the Jedi", 8.4, 1983));
/*Equivalent to deleting star wars*/
List<Movie> newList = list.stream()
.filter(movie -> !movie.getName().equals("Star Wars"))
.collect(Collectors.toList());//Only elements whose condition returns true will be retained
/*
* Movie(name=Force Awakens, store=8.3, year=2015)
* Movie(name=Empire Strikes Back, store=8.8, year=1980)
* Movie(name=Return of the Jedi, store=8.4, year=1983)
*/
newList.forEach(System.out::println);
}

@Test
public void updateTest() {
ArrayList<Movie> list = new ArrayList<Movie>();
list.add(new Movie("Force Awakens", 8.3, 2015));
list.add(new Movie("Star Wars", 8.7, 1977));
list.add(new Movie("Empire Strikes Back", 8.8, 1980));
list.add(new Movie("Return of the Jedi", 8.4, 1983));

List<Object> newList = list.stream()
.map(movie -> movie.getRate() == 8.8 ? movie.setRate(7.8) : movie)
.collect(Collectors.toList());
/*
*
*   Movie(name=Force Awakens, store=8.3, year=2015)
*   Movie(name=Star Wars, store=8.7, year=1977)
*  Movie(name=Empire Strikes Back, store=7.8, year=1980) //Updated the movie's ratings
*  Movie(name=Return of the Jedi, store=8.4, year=1983)
*/
newList.forEach(System.out::println);
}
}

Lambda表达式对于筛选集合或数组中的数据非常有用。

最新更新