{"id":1116,"date":"2020-11-13T10:20:02","date_gmt":"2020-11-13T10:20:02","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1116"},"modified":"2020-12-10T07:09:53","modified_gmt":"2020-12-10T07:09:53","slug":"javastream-characters","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2020\/11\/13\/javastream-characters\/","title":{"rendered":"JavaStream: Characters"},"content":{"rendered":"<p>Continuing from the last blog how about checking a pangram using streams.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>public class PangramChecker {\n\n    private String allChars = \"abcdefghijklmnopqrstuvwxyz\";\n\n    public boolean isPangram(String input) {\n        if (input.length() &lt;= 0 || input.isBlank()){\n            return false;\n        }\n        return (allChars.chars().map(x -&gt; (char) x).filter(x -&gt; input.toLowerCase().indexOf(x) &gt;= 0).count() == 26);\n    }\n\n}\n<\/code><\/pre>\n\n\n\n<p>All the for loops have been squeezed into a simple line<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Logic<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Define a <code>String<\/code> for all the characters in the English language &#8211; <code>allChars<\/code><\/li><li>Get the Stream using <code>allChars.chars()<\/code><\/li><li>Since it returns an <code>int<\/code> map it to <code>Char<\/code><\/li><li>Look for every character one by one in the <code>Input String<\/code> using <code><a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/String.html#indexOf(int)\" data-type=\"URL\" data-id=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/String.html#indexOf(int)\">indexOf<\/a><\/code>. (It returns -1 for character not found.)<\/li><li>Get the count of all the characters found, if anyone was not found we will have less than 26.<\/li><\/ul>\n\n\n\n<p>It can be further optimized&#8230; want to try?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Continuing from the last blog how about checking a pangram using streams. All the for loops have been squeezed into a simple line The Logic Define a String for all the characters in the English language &#8211; allChars Get the Stream using allChars.chars() Since it returns an int map it to Char Look for every [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1166,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[120,34],"tags":[122],"class_list":["post-1116","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-java","category-technical","tag-streams","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=1116"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1116\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1166"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}