{"action":"created","comment":{"url":"https://api.github.com/repos/elo7/tech-blog/pulls/comments/88952697","pull_request_review_id":9500345,"id":88952697,"diff_hunk":"@@ -0,0 +1,176 @@\n+---\n+title: 'Introdução à programação funcional com Java 8'\n+date: 2016-11-07\n+category: back-end\n+layout: post\n+description: Disponível desde 2014, o Java 8 ainda deixa muitas dúvidas, principalmente quanto às novidades em relação à sua versão anterior. Nesse post vou falar sobre as principais novidades do Java 8.\n+author: diegocesar\n+tags:\n+  - java\n+  - java8\n+  - back-end\n+---\n+\n+Disponível desde 2014, o Java 8 ainda deixa muitas dúvidas, principalmente quanto às novidades em relação à sua versão anterior e a incorporação de conceitos de programação funcional. Nesse post vou falar sobre as principais novidades do Java 8. É importante observar que o Java 8 foi desenvolvido pensando na retrocompatibilidade, ou seja, aplicações desenvolvidas na versão 7 funcionarão perfeitamente nas versões mais atuais da linguagem. Esse é um dos pontos fortes do Java, que ajudam a garantir o seu sucesso.\n+\n+Veremos a seguir um pouco sobre as principais novidades implementadas no Java 8:\n+\n+## Lambda expressions\n+Uma das principais e mais comentadas mudanças do Java 8, são as **Lambda Expressions** que trazem um pouco do paradigma da programação funcional para o Java. [Segundo a Oracle](http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html), Lambda é uma forma clara e objetiva de representar um método usando apenas uma expressão. É importante lembrar que as Lambda Expressions só funcionarão para **interfaces funcionais**, que são interfaces que possuem apenas um método.\n+\n+Exemplo:\n+```java\n+interface Operator {\n+\tabstract float calc(float number1, float number2);\n+}\n+\n+public class Calculator {\n+\tpublic static void main(String[] args) {\n+\t\tSystem.out.println(\n+\t\t\tcalculate(15, 10, (number1, number2) -> { return number1 + number2; })\n+\t\t);\n+\t}\n+\n+\tpublic static float calculate(float number1, float number2, Operator operator) {\n+\t\treturn operator.calc(number1, number2);\n+\t}\n+}\n+```\n+\n+Resultado: `25.0`\n+\n+## Stream\n+Stream, é uma abstração que permite processar dados de coleções de forma declarativa, usando Lambda Expressions. Os passos para fazer uso do stream são:\n+\n+1. Obter o stream a partir de uma Collection;\n+2. Adicionar uma ou mais operações no pipeline como filtros, ordenações, etc;\n+3. Invocar o método collect, este é responsável por processar o pipeline e retornar o resultado no formato solicitado.\n+\n+Veja algumas das operações que você pode executar em um Stream:\n+\n+### filter\n+\n+Filtra *strings* de acordo com a regra passada como parâmetro. Neste caso, filtra todas as '*strings* não vazias'.\n+\n+```java\n+public void removeEmptyStrings() {\n+\tList<String> strings = Arrays.asList(\"abc\", \"\", \"bc\", \"abcd\", \"\", \"xyz\", \"foo\");\n+\tstrings = strings.stream().filter(var -> !var.isEmpty()).collect(Collectors.toList());\n+}\n+```\n+\n+Resultado: `[abc, bc, abcd, xyz, foo]`\n+\n+### sorted\n+\n+Tem como função ordenar os itens de uma *Collection*, como no seguinte exemplo:\n+\n+```java\n+public void sortListAlphabetically() {\n+\tList<String> strings = Arrays.asList(\"z\", \"x\", \"a\", \"c\", \"b\", \"y\");\n+\tstrings = strings.stream().sorted().collect(Collectors.toList());\n+}\n+```\n+\n+Resultado: `[a, b, c, x, y, z]`\n+\n+\n+Também é possível ordenar utilizando regras diferentes da natural:\n+\n+```java\n+public void sortNumbersReversed() {\n+\tList<Integer> numbers = Arrays.asList(3, 5, 1, 4, 2);\n+\tnumbers = numbers.stream().sorted((num1, num2) -> num2 - num1).collect(Collectors.toList());\n+}\n+```\n+\n+Resultado: `[5, 4, 3, 2, 1]`\n+\n+### map\n+\n+O map aceita uma função como argumento que pode transformar cada um dos itens em um novo elemento:\n+\n+```java\n+public void countStringCharacters() {\n+\tList<String> strings = Arrays.asList(\"one\", \"two\", \"three\", \"four\");\n+\tList<Integer> totals = strings.stream().map(str -> str.length()).collect(Collectors.toList());\n+}\n+```\n+\n+Resultado: `[3, 3, 5, 4]`\n+\n+### reduce\n+\n+O objetivo do reduce, como o nome sugere, é reduzir o número de itens de uma *Collection*. Para isso ele recebe um argumento [opcional] que será utilizado como valor inicial do segundo argumento, que por sua vez é obrigatório. O segundo argumento é uma função que deve receber dois parâmetros, tratá-los da maneira desejada e devolver um resultado, este resultado será o primeiro parâmetro desta mesma função na próxima iteração em conjunto com o próximo elemento da *Collection*.\n+\n+Exemplo: Retornar a soma dos números pares de uma lista:\n+\n+```java\n+public void sumEvenNumbers() {\n+\tList<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);\n+\tInteger result = numbers.stream().reduce(0, (num1, num2) -> (num2 % 2) == 0 ? num1 + num2 : num1);\n+}\n+```\n+\n+Resultado: `12`\n+\n+### distinct\n+\n+O distinct devolve uma lista sem nenhuma entrada duplicada.\n+\n+```java\n+public void distinctElements() {\n+\tList<Integer> numbers = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 5);\n+\tnumbers = numbers.stream().distinct().collect(Collectors.toList());\n+}\n+```\n+\n+Resultado: `[1, 2, 3, 4, 5]`\n+\n+## Parallel Stream\n+O resultado final do Parallel Stream é o mesmo do Stream, a diferença entre eles é que neste caso os dados são processados de forma paralela, em várias threads, diferente do segundo que é serial. O processamento paralelo por si só não garante melhora de performance, exceto em casos onde exista um grande volume de dados ou múltiplos núcleos de processamento.","path":"src/posts/introducao-a-programacao-funcional-com-java8.html.md","position":131,"original_position":131,"commit_id":"e3d6dffeb5f375a44d52f70847f23775f29fd6a7","original_commit_id":"e3d6dffeb5f375a44d52f70847f23775f29fd6a7","user":{"login":"luiz","id":46065,"avatar_url":"https://avatars.githubusercontent.com/u/46065?v=3","gravatar_id":"","url":"https://api.github.com/users/luiz","html_url":"https://github.com/luiz","followers_url":"https://api.github.com/users/luiz/followers","following_url":"https://api.github.com/users/luiz/following{/other_user}","gists_url":"https://api.github.com/users/luiz/gists{/gist_id}","starred_url":"https://api.github.com/users/luiz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/luiz/subscriptions","organizations_url":"https://api.github.com/users/luiz/orgs","repos_url":"https://api.github.com/users/luiz/repos","events_url":"https://api.github.com/users/luiz/events{/privacy}","received_events_url":"https://api.github.com/users/luiz/received_events","type":"User","site_admin":false},"body":"> a diferença entre eles\r\n\r\nAcho que aqui deveria ser o início de uma nova frase.\r\n\r\n@luiz","created_at":"2016-11-21T18:06:16Z","updated_at":"2016-11-21T18:06:16Z","html_url":"https://github.com/elo7/tech-blog/pull/37#discussion_r88952697","pull_request_url":"https://api.github.com/repos/elo7/tech-blog/pulls/37","_links":{"self":{"href":"https://api.github.com/repos/elo7/tech-blog/pulls/comments/88952697"},"html":{"href":"https://github.com/elo7/tech-blog/pull/37#discussion_r88952697"},"pull_request":{"href":"https://api.github.com/repos/elo7/tech-blog/pulls/37"}}},"pull_request":{"url":"https://api.github.com/repos/elo7/tech-blog/pulls/37","id":90680742,"html_url":"https://github.com/elo7/tech-blog/pull/37","diff_url":"https://github.com/elo7/tech-blog/pull/37.diff","patch_url":"https://github.com/elo7/tech-blog/pull/37.patch","issue_url":"https://api.github.com/repos/elo7/tech-blog/issues/37","number":37,"state":"open","locked":false,"title":"[POST] Introdução à programação funcional com Java 8","user":{"login":"rapahaeru","id":3389749,"avatar_url":"https://avatars.githubusercontent.com/u/3389749?v=3","gravatar_id":"","url":"https://api.github.com/users/rapahaeru","html_url":"https://github.com/rapahaeru","followers_url":"https://api.github.com/users/rapahaeru/followers","following_url":"https://api.github.com/users/rapahaeru/following{/other_user}","gists_url":"https://api.github.com/users/rapahaeru/gists{/gist_id}","starred_url":"https://api.github.com/users/rapahaeru/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rapahaeru/subscriptions","organizations_url":"https://api.github.com/users/rapahaeru/orgs","repos_url":"https://api.github.com/users/rapahaeru/repos","events_url":"https://api.github.com/users/rapahaeru/events{/privacy}","received_events_url":"https://api.github.com/users/rapahaeru/received_events","type":"User","site_admin":false},"body":"@diegocesar \n","created_at":"2016-10-24T19:54:29Z","updated_at":"2016-11-21T18:15:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8ef3333c813817e50b21cf2eefb786faba1bce26","assignee":{"login":"diegocesar","id":12488185,"avatar_url":"https://avatars.githubusercontent.com/u/12488185?v=3","gravatar_id":"","url":"https://api.github.com/users/diegocesar","html_url":"https://github.com/diegocesar","followers_url":"https://api.github.com/users/diegocesar/followers","following_url":"https://api.github.com/users/diegocesar/following{/other_user}","gists_url":"https://api.github.com/users/diegocesar/gists{/gist_id}","starred_url":"https://api.github.com/users/diegocesar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/diegocesar/subscriptions","organizations_url":"https://api.github.com/users/diegocesar/orgs","repos_url":"https://api.github.com/users/diegocesar/repos","events_url":"https://api.github.com/users/diegocesar/events{/privacy}","received_events_url":"https://api.github.com/users/diegocesar/received_events","type":"User","site_admin":false},"assignees":[{"login":"diegocesar","id":12488185,"avatar_url":"https://avatars.githubusercontent.com/u/12488185?v=3","gravatar_id":"","url":"https://api.github.com/users/diegocesar","html_url":"https://github.com/diegocesar","followers_url":"https://api.github.com/users/diegocesar/followers","following_url":"https://api.github.com/users/diegocesar/following{/other_user}","gists_url":"https://api.github.com/users/diegocesar/gists{/gist_id}","starred_url":"https://api.github.com/users/diegocesar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/diegocesar/subscriptions","organizations_url":"https://api.github.com/users/diegocesar/orgs","repos_url":"https://api.github.com/users/diegocesar/repos","events_url":"https://api.github.com/users/diegocesar/events{/privacy}","received_events_url":"https://api.github.com/users/diegocesar/received_events","type":"User","site_admin":false}],"milestone":null,"commits_url":"https://api.github.com/repos/elo7/tech-blog/pulls/37/commits","review_comments_url":"https://api.github.com/repos/elo7/tech-blog/pulls/37/comments","review_comment_url":"https://api.github.com/repos/elo7/tech-blog/pulls/comments{/number}","comments_url":"https://api.github.com/repos/elo7/tech-blog/issues/37/comments","statuses_url":"https://api.github.com/repos/elo7/tech-blog/statuses/e3d6dffeb5f375a44d52f70847f23775f29fd6a7","head":{"label":"elo7:java8","ref":"java8","sha":"e3d6dffeb5f375a44d52f70847f23775f29fd6a7","user":{"login":"elo7","id":1469534,"avatar_url":"https://avatars.githubusercontent.com/u/1469534?v=3","gravatar_id":"","url":"https://api.github.com/users/elo7","html_url":"https://github.com/elo7","followers_url":"https://api.github.com/users/elo7/followers","following_url":"https://api.github.com/users/elo7/following{/other_user}","gists_url":"https://api.github.com/users/elo7/gists{/gist_id}","starred_url":"https://api.github.com/users/elo7/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elo7/subscriptions","organizations_url":"https://api.github.com/users/elo7/orgs","repos_url":"https://api.github.com/users/elo7/repos","events_url":"https://api.github.com/users/elo7/events{/privacy}","received_events_url":"https://api.github.com/users/elo7/received_events","type":"Organization","site_admin":false},"repo":{"id":61552932,"name":"tech-blog","full_name":"elo7/tech-blog","owner":{"login":"elo7","id":1469534,"avatar_url":"https://avatars.githubusercontent.com/u/1469534?v=3","gravatar_id":"","url":"https://api.github.com/users/elo7","html_url":"https://github.com/elo7","followers_url":"https://api.github.com/users/elo7/followers","following_url":"https://api.github.com/users/elo7/following{/other_user}","gists_url":"https://api.github.com/users/elo7/gists{/gist_id}","starred_url":"https://api.github.com/users/elo7/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elo7/subscriptions","organizations_url":"https://api.github.com/users/elo7/orgs","repos_url":"https://api.github.com/users/elo7/repos","events_url":"https://api.github.com/users/elo7/events{/privacy}","received_events_url":"https://api.github.com/users/elo7/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/elo7/tech-blog","description":"Blog técnico do Elo7","fork":false,"url":"https://api.github.com/repos/elo7/tech-blog","forks_url":"https://api.github.com/repos/elo7/tech-blog/forks","keys_url":"https://api.github.com/repos/elo7/tech-blog/keys{/key_id}","collaborators_url":"https://api.github.com/repos/elo7/tech-blog/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/elo7/tech-blog/teams","hooks_url":"https://api.github.com/repos/elo7/tech-blog/hooks","issue_events_url":"https://api.github.com/repos/elo7/tech-blog/issues/events{/number}","events_url":"https://api.github.com/repos/elo7/tech-blog/events","assignees_url":"https://api.github.com/repos/elo7/tech-blog/assignees{/user}","branches_url":"https://api.github.com/repos/elo7/tech-blog/branches{/branch}","tags_url":"https://api.github.com/repos/elo7/tech-blog/tags","blobs_url":"https://api.github.com/repos/elo7/tech-blog/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/elo7/tech-blog/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/elo7/tech-blog/git/refs{/sha}","trees_url":"https://api.github.com/repos/elo7/tech-blog/git/trees{/sha}","statuses_url":"https://api.github.com/repos/elo7/tech-blog/statuses/{sha}","languages_url":"https://api.github.com/repos/elo7/tech-blog/languages","stargazers_url":"https://api.github.com/repos/elo7/tech-blog/stargazers","contributors_url":"https://api.github.com/repos/elo7/tech-blog/contributors","subscribers_url":"https://api.github.com/repos/elo7/tech-blog/subscribers","subscription_url":"https://api.github.com/repos/elo7/tech-blog/subscription","commits_url":"https://api.github.com/repos/elo7/tech-blog/commits{/sha}","git_commits_url":"https://api.github.com/repos/elo7/tech-blog/git/commits{/sha}","comments_url":"https://api.github.com/repos/elo7/tech-blog/comments{/number}","issue_comment_url":"https://api.github.com/repos/elo7/tech-blog/issues/comments{/number}","contents_url":"https://api.github.com/repos/elo7/tech-blog/contents/{+path}","compare_url":"https://api.github.com/repos/elo7/tech-blog/compare/{base}...{head}","merges_url":"https://api.github.com/repos/elo7/tech-blog/merges","archive_url":"https://api.github.com/repos/elo7/tech-blog/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/elo7/tech-blog/downloads","issues_url":"https://api.github.com/repos/elo7/tech-blog/issues{/number}","pulls_url":"https://api.github.com/repos/elo7/tech-blog/pulls{/number}","milestones_url":"https://api.github.com/repos/elo7/tech-blog/milestones{/number}","notifications_url":"https://api.github.com/repos/elo7/tech-blog/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/elo7/tech-blog/labels{/name}","releases_url":"https://api.github.com/repos/elo7/tech-blog/releases{/id}","deployments_url":"https://api.github.com/repos/elo7/tech-blog/deployments","created_at":"2016-06-20T14:14:12Z","updated_at":"2016-11-17T11:44:24Z","pushed_at":"2016-11-11T19:58:39Z","git_url":"git://github.com/elo7/tech-blog.git","ssh_url":"git@github.com:elo7/tech-blog.git","clone_url":"https://github.com/elo7/tech-blog.git","svn_url":"https://github.com/elo7/tech-blog","homepage":null,"size":5053,"stargazers_count":2,"watchers_count":2,"language":"CSS","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":17,"forks":0,"open_issues":17,"watchers":2,"default_branch":"master"}},"base":{"label":"elo7:master","ref":"master","sha":"1c0ca091c035de1c1583c4578b2a4b72ada51151","user":{"login":"elo7","id":1469534,"avatar_url":"https://avatars.githubusercontent.com/u/1469534?v=3","gravatar_id":"","url":"https://api.github.com/users/elo7","html_url":"https://github.com/elo7","followers_url":"https://api.github.com/users/elo7/followers","following_url":"https://api.github.com/users/elo7/following{/other_user}","gists_url":"https://api.github.com/users/elo7/gists{/gist_id}","starred_url":"https://api.github.com/users/elo7/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elo7/subscriptions","organizations_url":"https://api.github.com/users/elo7/orgs","repos_url":"https://api.github.com/users/elo7/repos","events_url":"https://api.github.com/users/elo7/events{/privacy}","received_events_url":"https://api.github.com/users/elo7/received_events","type":"Organization","site_admin":false},"repo":{"id":61552932,"name":"tech-blog","full_name":"elo7/tech-blog","owner":{"login":"elo7","id":1469534,"avatar_url":"https://avatars.githubusercontent.com/u/1469534?v=3","gravatar_id":"","url":"https://api.github.com/users/elo7","html_url":"https://github.com/elo7","followers_url":"https://api.github.com/users/elo7/followers","following_url":"https://api.github.com/users/elo7/following{/other_user}","gists_url":"https://api.github.com/users/elo7/gists{/gist_id}","starred_url":"https://api.github.com/users/elo7/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elo7/subscriptions","organizations_url":"https://api.github.com/users/elo7/orgs","repos_url":"https://api.github.com/users/elo7/repos","events_url":"https://api.github.com/users/elo7/events{/privacy}","received_events_url":"https://api.github.com/users/elo7/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/elo7/tech-blog","description":"Blog técnico do Elo7","fork":false,"url":"https://api.github.com/repos/elo7/tech-blog","forks_url":"https://api.github.com/repos/elo7/tech-blog/forks","keys_url":"https://api.github.com/repos/elo7/tech-blog/keys{/key_id}","collaborators_url":"https://api.github.com/repos/elo7/tech-blog/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/elo7/tech-blog/teams","hooks_url":"https://api.github.com/repos/elo7/tech-blog/hooks","issue_events_url":"https://api.github.com/repos/elo7/tech-blog/issues/events{/number}","events_url":"https://api.github.com/repos/elo7/tech-blog/events","assignees_url":"https://api.github.com/repos/elo7/tech-blog/assignees{/user}","branches_url":"https://api.github.com/repos/elo7/tech-blog/branches{/branch}","tags_url":"https://api.github.com/repos/elo7/tech-blog/tags","blobs_url":"https://api.github.com/repos/elo7/tech-blog/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/elo7/tech-blog/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/elo7/tech-blog/git/refs{/sha}","trees_url":"https://api.github.com/repos/elo7/tech-blog/git/trees{/sha}","statuses_url":"https://api.github.com/repos/elo7/tech-blog/statuses/{sha}","languages_url":"https://api.github.com/repos/elo7/tech-blog/languages","stargazers_url":"https://api.github.com/repos/elo7/tech-blog/stargazers","contributors_url":"https://api.github.com/repos/elo7/tech-blog/contributors","subscribers_url":"https://api.github.com/repos/elo7/tech-blog/subscribers","subscription_url":"https://api.github.com/repos/elo7/tech-blog/subscription","commits_url":"https://api.github.com/repos/elo7/tech-blog/commits{/sha}","git_commits_url":"https://api.github.com/repos/elo7/tech-blog/git/commits{/sha}","comments_url":"https://api.github.com/repos/elo7/tech-blog/comments{/number}","issue_comment_url":"https://api.github.com/repos/elo7/tech-blog/issues/comments{/number}","contents_url":"https://api.github.com/repos/elo7/tech-blog/contents/{+path}","compare_url":"https://api.github.com/repos/elo7/tech-blog/compare/{base}...{head}","merges_url":"https://api.github.com/repos/elo7/tech-blog/merges","archive_url":"https://api.github.com/repos/elo7/tech-blog/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/elo7/tech-blog/downloads","issues_url":"https://api.github.com/repos/elo7/tech-blog/issues{/number}","pulls_url":"https://api.github.com/repos/elo7/tech-blog/pulls{/number}","milestones_url":"https://api.github.com/repos/elo7/tech-blog/milestones{/number}","notifications_url":"https://api.github.com/repos/elo7/tech-blog/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/elo7/tech-blog/labels{/name}","releases_url":"https://api.github.com/repos/elo7/tech-blog/releases{/id}","deployments_url":"https://api.github.com/repos/elo7/tech-blog/deployments","created_at":"2016-06-20T14:14:12Z","updated_at":"2016-11-17T11:44:24Z","pushed_at":"2016-11-11T19:58:39Z","git_url":"git://github.com/elo7/tech-blog.git","ssh_url":"git@github.com:elo7/tech-blog.git","clone_url":"https://github.com/elo7/tech-blog.git","svn_url":"https://github.com/elo7/tech-blog","homepage":null,"size":5053,"stargazers_count":2,"watchers_count":2,"language":"CSS","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":17,"forks":0,"open_issues":17,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/elo7/tech-blog/pulls/37"},"html":{"href":"https://github.com/elo7/tech-blog/pull/37"},"issue":{"href":"https://api.github.com/repos/elo7/tech-blog/issues/37"},"comments":{"href":"https://api.github.com/repos/elo7/tech-blog/issues/37/comments"},"review_comments":{"href":"https://api.github.com/repos/elo7/tech-blog/pulls/37/comments"},"review_comment":{"href":"https://api.github.com/repos/elo7/tech-blog/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/elo7/tech-blog/pulls/37/commits"},"statuses":{"href":"https://api.github.com/repos/elo7/tech-blog/statuses/e3d6dffeb5f375a44d52f70847f23775f29fd6a7"}}}}
{"actor":{"display_login":"luiz"}}