如何从 Optional Java 对象中的数组中删除所有项目?

How to remove all the items from an array in an Optional Java Object?

我在 Post 和标签(主题)实体之间存在多对多关系。所以在删除 Post 之前,我需要获取它,删除它的标签和主题,保存 Post 然后删除它,但是我找不到通过 Post 的方法反对并删除标签和主题项(如果有)。

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

@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);
    }