{"id":142,"date":"2024-03-05T01:42:06","date_gmt":"2024-03-05T01:42:06","guid":{"rendered":"https:\/\/blog.kevinsiraki.com\/?p=142"},"modified":"2024-03-11T06:37:12","modified_gmt":"2024-03-11T06:37:12","slug":"a-stream-api-example-in-java-8","status":"publish","type":"post","link":"https:\/\/blog.kevinsiraki.com\/?p=142","title":{"rendered":"A Stream API Example In Java 8"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Streams<\/h1>\n\n\n\n<p>To start with, we will go over a simple yet interesting problem in Java: finding the second smallest value in an array. There are a few different approaches we could take. The most obvious would be sorting the array, skipping the first element (and any duplicates). However, a more efficient approach could be taken as follows:<\/p>\n\n\n\n<p>First, instantiate a simple int array and find the minimum as you usually would.<\/p>\n\n\n\n<pre class=\"wp-block-code has-contrast-color has-text-color has-link-color has-small-font-size wp-elements-f6b01b0f70f5b7a7a9b5f15001f6f9be\"><code>int&#91;] arr = {5, 44, 0, 0, 99};\nNoSuchElementException exception = new NoSuchElementException(\"Array is empty\");\nint min1 = Arrays.stream(arr)\n  .min()\n  .orElseThrow(() -&gt; exception);<\/code><\/pre>\n\n\n\n<p>Then, simply filter out that first minimum value from the array and find the minimum of that subarray respectively.<\/p>\n\n\n\n<pre class=\"wp-block-code has-contrast-color has-text-color has-link-color has-small-font-size wp-elements-11ee78182dae88147e5d6eec46f0b7c7\"><code>int min2 = Arrays.stream(arr)\n  .filter(x -&gt; x != min1)\n  .min()\n  .orElseThrow(() -&gt; exception);\nSystem.out.print(min2);<\/code><\/pre>\n\n\n\n<p>This approach saves on time complexity and feels more readable in my personal opinion. I thought it would be interesting to share this cool snippet with everyone!<\/p>\n\n\n\n<p>Coming Soon in Part 2: Information about CompletableFuture in Java 8!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Streams To start with, we will go over a simple yet interesting problem in Java: finding the second smallest value in an array. There are a few different approaches we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_EventAllDay":false,"_EventTimezone":"","_EventStartDate":"","_EventEndDate":"","_EventStartDateUTC":"","_EventEndDateUTC":"","_EventShowMap":false,"_EventShowMapLink":false,"_EventURL":"","_EventCost":"","_EventCostDescription":"","_EventCurrencySymbol":"","_EventCurrencyCode":"","_EventCurrencyPosition":"","_EventDateTimeSeparator":"","_EventTimeRangeSeparator":"","_EventOrganizerID":[],"_EventVenueID":[],"_OrganizerEmail":"","_OrganizerPhone":"","_OrganizerWebsite":"","_VenueAddress":"","_VenueCity":"","_VenueCountry":"","_VenueProvince":"","_VenueState":"","_VenueZip":"","_VenuePhone":"","_VenueURL":"","_VenueStateProvince":"","_VenueLat":"","_VenueLng":"","_VenueShowMap":false,"_VenueShowMapLink":false,"footnotes":""},"categories":[8,7],"tags":[34],"class_list":["post-142","post","type-post","status-publish","format-standard","hentry","category-java-programming-2","category-programming-2","tag-algorithms"],"_links":{"self":[{"href":"https:\/\/blog.kevinsiraki.com\/index.php?rest_route=\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kevinsiraki.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kevinsiraki.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kevinsiraki.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kevinsiraki.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=142"}],"version-history":[{"count":1,"href":"https:\/\/blog.kevinsiraki.com\/index.php?rest_route=\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":299,"href":"https:\/\/blog.kevinsiraki.com\/index.php?rest_route=\/wp\/v2\/posts\/142\/revisions\/299"}],"wp:attachment":[{"href":"https:\/\/blog.kevinsiraki.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kevinsiraki.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kevinsiraki.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}