如何使用 java 从多值数组列表中更新和删除元素/项目列表?
How to update and remove list of elements / item from a multivalued array list using 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)中输出):
Movie [rating=8.7, name=Star Wars, year=1977]
Movie [rating=8.8, name=Empire Strikes Back, year=1980]
Movie [rating=8.4, name=Return of the Jedi, year=1983]
Movie [rating=8.3, name=Force Awakens, year=2015]
所以我的问题是如何使用 java 从列表中更新和删除上述任何一项。我应该在这里应用什么逻辑????
P.S :我尝试了 java remove() 命令但在下面抛出了一个异常:
Exception in thread "main" java.util.ConcurrentModificationException
at
java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:937)
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:891) at
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 表达式对于过滤集合或数组中的数据非常有用。
这里是示例代码,
*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)中输出):
Movie [rating=8.7, name=Star Wars, year=1977]
Movie [rating=8.8, name=Empire Strikes Back, year=1980]
Movie [rating=8.4, name=Return of the Jedi, year=1983]
Movie [rating=8.3, name=Force Awakens, year=2015]
所以我的问题是如何使用 java 从列表中更新和删除上述任何一项。我应该在这里应用什么逻辑????
P.S :我尝试了 java remove() 命令但在下面抛出了一个异常:
Exception in thread "main" java.util.ConcurrentModificationException at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:937) at java.base/java.util.ArrayList$Itr.next(ArrayList.java:891) at 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 表达式对于过滤集合或数组中的数据非常有用。