gpt4 book ai didi

java - 如何从可选 Java 对象的数组中删除所有项目?

转载 作者:行者123 更新时间:2023-11-30 01:57:00 24 4
gpt4 key购买 nike

我在帖子和标签(主题)实体之间有多对多关系。因此,在删除帖子之前,我需要获取它,删除其标签和主题,保存帖子然后删除它,但我找不到遍历帖子对象并删除标签和主题项的方法(如果有)任何。

我得到一个:java.util.ConcurrentModificationException:null

@Override
public void delete(Long id) {
log.debug("Request to delete Post : {}", id);
Optional<Post> postOpt = postRepository.findById(id);
Post post = postOpt.get();
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!" + post);
for (Tag tag : post.getTags()) {
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!" + tag.toString());
post.removeTag(tag);
}
for (Topic topic : post.getTopics()) {
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!" + topic.toString());
post.removeTopic(topic);
}
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!" + post);
// postRepository.deleteById(id);
// postSearchRepository.deleteById(id);
}

其中DTO对象是这样的:

[
{
"id": 0,
"publicationDate": "2019-01-07T10:24:15.892Z",
"quote": "string",
"tags": [
{
"id": 0,
"tagName": "string"
}
],
"topics": [
{
"id": 0,
"topicName": "string"
}
],
"userId": 0,
}
]

删除 POST 中的方法:

    public Set<Tag> getTags() {
return tags;
}

public Post tags(Set<Tag> tags) {
this.tags = tags;
return this;
}

public Post addTag(Tag tag) {
this.tags.add(tag);
tag.getPosts().add(this);
return this;
}

public Post removeTag(Tag tag) {
this.tags.remove(tag);
tag.getPosts().remove(this);
return this;
}

public void setTags(Set<Tag> tags) {
this.tags = tags;
}

public Set<Topic> getTopics() {
return topics;
}

public Post topics(Set<Topic> topics) {
this.topics = topics;
return this;
}

public Post addTopic(Topic topic) {
this.topics.add(topic);
topic.getPosts().add(this);
return this;
}

public Post removeTopic(Topic topic) {
this.topics.remove(topic);
topic.getPosts().remove(this);
return this;
}

删除标签中的方法:

public Set<Post> getPosts() {
return posts;
}

public Tag posts(Set<Post> posts) {
this.posts = posts;
return this;
}

public Tag addPost(Post post) {
this.posts.add(post);
post.getTags().add(this);
return this;
}

public Tag removePost(Post post) {
this.posts.remove(post);
post.getTags().remove(this);
return this;
}

public void setPosts(Set<Post> posts) {
this.posts = posts;
}

谢谢

更改后:有效。

    @Override
public void delete(Long id) {
log.debug("Request to delete Post : {}", id);

Optional<Post> postOpt = postRepository.findById(id);
Post post = postOpt.get();

ArrayList<Tag> arrayTags = new ArrayList<Tag>();
arrayTags.addAll(post.getTags());
Iterator<Tag> copyOfTags = arrayTags.iterator();
while (copyOfTags.hasNext()) {
Tag tag = copyOfTags.next();
tag.removePost(post);
}

ArrayList<Topic> arrayTopics = new ArrayList<Topic>();
arrayTopics.addAll(post.getTopics());
Iterator<Topic> copyOfTopics = arrayTopics.iterator();
while (copyOfTopics.hasNext()) {
Topic topic = copyOfTopics.next();
topic.removePost(post);
}

postRepository.save(post);

postRepository.deleteById(id);
postSearchRepository.deleteById(id);
}

再次感谢,对于给您带来的不便,我们深表歉意!

最佳答案

您不能迭代集合并同时从集合中删除元素。

您想通过方法removeTagremoveTopic删除标签和主题,因此您不想使用iterate.remove方法.

最适合您的是创建标签集合的副本,并迭代复制的集合。

Set<Tag> copyOfTags = new HashSet<Tag>(post.getTags());
for (Tag tag : copyOfTags) {
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!" + tag.toString());
post.removeTag(tag);
}

关于java - 如何从可选 Java 对象的数组中删除所有项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54078715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com