{"id":7863,"date":"2015-11-01T05:45:10","date_gmt":"2015-11-01T05:45:10","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/01\/guava-how-to-combine-filter-and-transform-open-source-projects-google-guava\/"},"modified":"2015-11-01T05:45:10","modified_gmt":"2015-11-01T05:45:10","slug":"guava-how-to-combine-filter-and-transform-open-source-projects-google-guava","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/01\/guava-how-to-combine-filter-and-transform-open-source-projects-google-guava\/","title":{"rendered":"Guava: how to combine filter and transform?-open source projects google\/guava"},"content":{"rendered":"<p>I have a collection of Strings, and I would like to convert it to a collection of strings were all empty or null Strings are removed and all others are trimmed.<\/p>\n<p>I can do it in two steps:<\/p>\n<pre><code>final List tokens =\n    Lists.newArrayList(\" some \", null, \"stuff\\t\", \"\", \" \\nhere\");\nfinal Collection filtered =\n    Collections2.filter(\n        Collections2.transform(tokens, new Function(){\n\n            \/\/ This is a substitute for StringUtils.stripToEmpty()\n            \/\/ why doesn't Guava have stuff like that?\n            @Override\n            public String apply(final String input){\n                return input == null ? \"\" : input.trim();\n            }\n        }), new Predicate(){\n\n            @Override\n            public boolean apply(final String input){\n                return !Strings.isNullOrEmpty(input);\n            }\n\n        });\nSystem.out.println(filtered);\n\/\/ Output, as desired: [some, stuff, here]\n<\/code><\/pre>\n<p>But is there a Guava way of combining the two actions into one step?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a collection of Strings, and I would like to convert it to a collection of strings were all empty or null Strings are removed and all others are trimmed. I can do it in two steps: final List tokens = Lists.newArrayList(&#8221; some &#8220;, null, &#8220;stuff\\t&#8221;, &#8220;&#8221;, &#8221; \\nhere&#8221;); final Collection filtered = Collections2.filter( [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7863","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7863"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7863\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}