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

标签 java spring-boot mapstruct

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

我得到一个: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/

相关文章:

java - 如何淡入隐藏的 JavaFX 标签

java - 使用 Converter 时在 Vaadin DateField 中设置范围

java - Spring Boot Controller 内容协商

java - 发现映射属性不明确的映射方法(java.util.Map 和 java.util.Object)

java - 在另一个测试中使用映射器的映射结构返回 null

java - 强制mapstruct不调用has*方法

java - 使用谓词按字符串值对映射进行排序

java - Guice 场景中多个注入(inject)器引用出现是不可避免的

java - 如何禁用在 IntelliJ IDEA 中显示 Spring @Value 注释的值?

java - spring boot扫描并注入(inject)外部非spring bean