java - Filter collection's elements -


i have topic list of comment:

public optional<topic> gettopic() {     return optional.ofnullable(new topic(collections.singletonlist(new comment("comment1")))); }  public class topic {      private list<comment> comments;      public topic(list<comment> comments) {         this.comments = comments;     }      public list<comment> getcomments() {         return comments;     }  }  public class comment {      private string name;      public comment(string name) {         this.name = name;     }      public string getname() {         return name;     }  } 

i want call gettopic, fetch comments map , filter entries of list this:

gettopic()     .map(topic::getcomments)     .filter(comment -> "comment1".equals(comment.getname())); //doesn't work 

it doesn't work, because in filter have comments, not single comment. how can receive new list after filter comments lambdas?

optional<topic> topic = gettopic(); if (topic.ispresent()) {     list<comment> comments = topic.get().getcomments()         .stream()         .filter(comment -> "comment1".equals(comment.getname()))         .collect(collectors.tolist()); } 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -