Skip to main content
The 2024 Developer Survey results are live! See the results

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • 1
    Is there any way to get the field values eg: Stream.of(id, name), remove null fields and return List of values of not null fields
    – Rinsen S
    Commented Mar 3, 2018 at 13:31
  • 1
    @Rinsen you can use filter() but that's a different question and it's likely to have already been asked on SO.
    – Didier L
    Commented Mar 3, 2018 at 13:39
  • If you're using Spring, it has a nice hasText method. If you need to check if at least one field is not null and has text (not empty), it could look like this(intended as a class method):: Stream.of(field1, field2,...fieldN).anyMatch(StringUtils::hasText);
    – RHronza
    Commented Jul 3 at 13:44
  • @RHronza That one exists for internal use within the Spring framework, I would recommend using the StringUtils of Apache Commons Lang instead, as recommended by Spring’s StringUtils javadoc itself!
    – Didier L
    Commented Jul 3 at 14:49
  • @DidierL: I stated that it is only used with Spring, the advantage is not only null safe, but also empty safe
    – RHronza
    Commented Jul 4 at 14:20