{"id":105092,"date":"2021-10-01T11:00:00","date_gmt":"2021-10-01T08:00:00","guid":{"rendered":"https:\/\/examples.javacodegeeks.com\/?p=105092"},"modified":"2021-12-17T13:25:18","modified_gmt":"2021-12-17T11:25:18","slug":"java-8-stream-api-distinct-count-sorted-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/","title":{"rendered":"Java 8  Stream API &#8211; distinct(), count() &#038; sorted() Example"},"content":{"rendered":"<p>Hello. In this tutorial, we will explore the Stream API methods: sorted(), count(), and distinct() methods introduced in Java 8.<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-1-introduction\">1. Introduction<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"alignright size-full\"><a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\"><img decoding=\"async\" width=\"150\" height=\"150\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\" alt=\"java 8 distinct count\" class=\"wp-image-1204\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg 150w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo-70x70.jpg 70w\" sizes=\"(max-width: 150px) 100vw, 150px\" \/><\/a><\/figure>\n<\/div>\n<p>Before diving deep into the practice stuff let us understand the methods we will be covering in this tutorial.<\/p>\n<ul class=\"wp-block-list\">\n<li>The <code>distinct()<\/code> method returns a stream of unique elements. It is a stateful intermediate operation and returns a new stream. This method uses <code>hashCode()<\/code> and <code>equals()<\/code> to get the unique elements<\/li>\n<li>The <code>sorted()<\/code> method returns a stream consisting of elements in the sorted natural order. This method can also accept a comparator to provide customized sorting. It is a stateful intermediate operation and returns a new stream<\/li>\n<li>The <code>count()<\/code> method returns the count of elements in a stream<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"h-2-practice\">2. Practice<\/h2>\n<p>Let us dive into some practice stuff from here and I am assuming that you already have the Java 1.8 or greater installed in your local machine. I am using <a href=\"https:\/\/www.jetbrains.com\/idea\/\" target=\"_blank\" rel=\"noopener\">JetBrains IntelliJ IDEA<\/a> as my preferred IDE. You&#8217;re free to choose the IDE of your choice.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-2-1-model-class\">2.1 Model class<\/h3>\n<p>Create a java file in the <code>com.java8.util<\/code> package and add the following code. The class will act as a model class for the creation of the employee list.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Employee.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java; wrap-lines:false;\">package com.java8.util;\n\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class Employee {\n    private final String email;\n    private final int age;\n\n    private Employee(String email, int age) {\n        this.email = email;\n        this.age = age;\n    }\n\n    public static List&lt;Employee&gt; create() {\n        List&lt;Employee&gt; employees = new ArrayList&lt;&gt;();\n        employees.add(new Employee(\"john@email.com\", 21));\n        employees.add(new Employee(\"martin@email.com\", 19));\n        employees.add(new Employee(\"marry@email.com\", 31));\n        employees.add(new Employee(\"john@email.com\", 27));\n        employees.add(new Employee(\"marry@email.com\", 25));\n\n        return employees;\n    }\n\n    public String getEmail() {\n        return email;\n    }\n\n    public int getAge() {\n        return age;\n    }\n\n    @Override\n    public String toString() {\n        return \"Employee [email=\" + email + \", age=\" + age + \"]\";\n    }\n}\n<\/pre>\n<h3 class=\"wp-block-heading\" id=\"h-2-2-understanding-distinct-method\">2.2 Understanding distinct() method<\/h3>\n<p>Create a java file in the <code>com.java8<\/code> package and add the following code. The class will show the <code>distinct()<\/code> method implementation in different ways.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>DistinctDemo.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java; wrap-lines:false;\">package com.java8;\n\nimport com.java8.util.Employee;\n\nimport java.util.Arrays;\nimport java.util.List;\nimport java.util.Set;\nimport java.util.concurrent.ConcurrentHashMap;\nimport java.util.function.Function;\nimport java.util.function.Predicate;\nimport java.util.stream.Collectors;\n\npublic class DistinctDemo {\n\n    \/\/ distinct() method returns a stream of unique elements\n    \/\/ uses the hashCode() and equals() method to get the unique elements\n\n    \/\/ removing duplicates from primitive type\n    private static void method1() {\n        List&lt;Integer&gt; duplicates = Arrays.asList(1, 2, 2, 3, 4, 5, 6, 6, 1, 8, 0, 10);\n        print(\"Original list: \", duplicates);\n\n        List&lt;Integer&gt; unique = duplicates.stream()\n                .distinct()\n                .collect(Collectors.toList());\n        print(\"Distinct list: \", unique);\n    }\n\n\n    \/\/ removing duplicates from object\n    private static void method2() {\n        List&lt;Employee&gt; employees = Employee.create();\n        print(\"Original list: \", employees);\n\n        List&lt;Employee&gt; distinct = employees.stream()\n                .filter(distinctByKey(Employee::getEmail))    \/\/ using the predicate to remove the duplicated\n                .collect(Collectors.toList());\n        print(\"Distinct list: \", distinct);\n    }\n\n    \/\/ predicate to filter the duplicates by the given key extractor\n    \/\/ does the job to remove the duplicates\n    private static &lt;T&gt; Predicate&lt;T&gt; distinctByKey(Function&lt;? super T, ?&gt; keyExtractor) {\n        Set&lt;Object&gt; seen = ConcurrentHashMap.newKeySet();\n        return t -&gt; seen.add(keyExtractor.apply(t));\n    }\n\n    \/\/ print the list\n    private static void print(String message, List&lt;?&gt; some) {\n        System.out.println(message + some);\n    }\n\n    \/\/ driver code\n    public static void main(String[] args) {\n        System.out.println(\"-- Streams distinct() method --\\n\");\n\n        method1();\n\n        System.out.println(\"\\n\");\n\n        method2();\n    }\n}\n<\/pre>\n<p>Run the file as a java application and if everything goes well the following output will be logged in the IDE console.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><span style=\"text-decoration: underline;\"><em>Console output<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:plain; wrap-lines:false;\">-- Streams distinct() method --\n\nOriginal list: [1, 2, 2, 3, 4, 5, 6, 6, 1, 8, 0, 10]\nDistinct list: [1, 2, 3, 4, 5, 6, 8, 0, 10]\n\n\nOriginal list: [Employee [email=john@email.com, age=21], Employee [email=martin@email.com, age=19], Employee [email=marry@email.com, age=31], Employee [email=john@email.com, age=27], Employee [email=marry@email.com, age=25]]\nDistinct list: [Employee [email=john@email.com, age=21], Employee [email=martin@email.com, age=19], Employee [email=marry@email.com, age=31]]\n<\/pre>\n<h3 class=\"wp-block-heading\" id=\"h-2-3-understanding-count-method\">2.3 Understanding count() method<\/h3>\n<p>Create a java file in the <code>com.java8<\/code> package and add the following code. The class will show the <code>count()<\/code> method implementation in different ways.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>CountDemo.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java; wrap-lines:false;\">package com.java8;\n\nimport com.java8.util.Employee;\n\nimport java.util.Arrays;\nimport java.util.List;\n\npublic class CountDemo {\n\n    \/\/ count() method counts the number of elements in the stream\n\n    private static void method1() {\n        List&lt;Integer&gt; list = Arrays.asList(1, 2, 2, 3, 4, 5, 6, 6, 1, 8, 0, 10);\n\n        long total = list.stream()\n                .count();\n        System.out.println(\"Total elements = \" + total);\n    }\n\n    private static void method2() {\n        List&lt;Employee&gt; employees = Employee.create();\n\n        long total = employees.stream().count();\n        System.out.println(\"Total employees = \" + total);\n\n        long freshers = employees.stream()\n                .filter(employee -&gt; employee.getAge() &lt;= 21)\n                .count();\n        System.out.println(\"Total employees after filter(..) op = \" + freshers);\n    }\n\n    \/\/ driver code\n    public static void main(String[] args) {\n        System.out.println(\"-- Streams count() method --\\n\");\n\n        method1();\n\n        System.out.println(\"\\n\");\n\n        method2();\n    }\n}\n<\/pre>\n<p>Run the file as a java application and if everything goes well the following output will be logged in the IDE console.[ulp id=&#8217;mp4qw6feirku6wM7&#8242; name=&#8217;Java 8 Features Inline&#8217;]<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Console output<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:plain; wrap-lines:false;\">-- Streams count() method --\n\nTotal elements = 12\n\n\nTotal employees = 5\nTotal employees after filter(..) op = 2\n<\/pre>\n<h3 class=\"wp-block-heading\" id=\"h-2-4-understanding-sorted-method\">2.4 Understanding sorted() method<\/h3>\n<p>Create a java file in the <code>com.java8<\/code> package and add the following code. The class will show the <code>sorted()<\/code> method implementation in different ways.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>SortedDemo.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java; wrap-lines:false;\">package com.java8;\n\nimport com.java8.util.Employee;\n\nimport java.util.Arrays;\nimport java.util.Comparator;\nimport java.util.List;\nimport java.util.stream.Collectors;\n\npublic class SortedDemo {\n\n\t\/\/ sorted() method returns a stream consisting of elements in a sorted order\n\t\/\/ it is a stateful intermediate operation\n\n\t\/\/ sorting primitive type list\n\tprivate static void method1() {\n\t\tList&lt;Integer&gt; unsorted = Arrays.asList(1, 2, 2, 3, 4, 5, 6, 6, 1, 8, 0, 10);\n\t\tSystem.out.println(\"Unsorted stream:\" + unsorted);\n\n\t\tList&lt;Integer&gt; sorted = unsorted.stream()\n\t\t\t\t.sorted()\n\t\t\t\t.collect(Collectors.toList());\n\t\tSystem.out.println(\"Sorted stream:\" + sorted);\n\t}\n\n\t\/\/ sorting object list\n\tprivate static void method2() {\n\t\tList&lt;Employee&gt; employees = Employee.create();\n\n\t\tList&lt;Employee&gt; sorted = employees.stream()\n\t\t\t\t.sorted(Comparator.comparing(Employee::getAge))    \/\/ sorting the employees by age\n\t\t\t\t.collect(Collectors.toList());\n\t\tSystem.out.println(\"Sorted list : \" + sorted);\n\t}\n\n\t\/\/ driver code\n\tpublic static void main(String[] args) {\n\t\tSystem.out.println(\"-- Streams sorted() method --\\n\");\n\n\t\tmethod1();\n\n\t\tSystem.out.println(\"\\n\");\n\n\t\tmethod2();\n\t}\n}\n<\/pre>\n<p>Run the file as a java application and if everything goes well the following output will be logged in the IDE console.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Console output<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:plain; wrap-lines:false;\">-- Streams sorted() method --\n\nUnsorted stream:[1, 2, 2, 3, 4, 5, 6, 6, 1, 8, 0, 10]\nSorted stream:[0, 1, 1, 2, 2, 3, 4, 5, 6, 6, 8, 10]\n\n\nSorted list : [Employee [email=martin@email.com, age=19], Employee [email=john@email.com, age=21], Employee [email=marry@email.com, age=25], Employee [email=john@email.com, age=27], Employee [email=marry@email.com, age=31]]\n<\/pre>\n<p>That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-4-summary\">4. Summary<\/h2>\n<p>In this tutorial, we learned the <code>sorted()<\/code>, <code>count()<\/code>, and <code>distinct()<\/code> methods introduced in java8 programming along with the implementation. You can download the source code from the <a href=\"#projectDownload\">Downloads<\/a> section.<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-5-download-the-project\"><a name=\"projectDownload\"><\/a>5. Download the Project<\/h2>\n<p>This was a tutorial on learning and implementing the <code>sorted()<\/code>, <code>count()<\/code>, and <code>distinct()<\/code> methods in Java 8.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>You can download the full source code of this example here: <a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/09\/Java-8-Stream-API-distinct-count-sorted-Example.zip\"><strong>Java 8 Stream API &#8211; distinct(), count() &amp; sorted() Example<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello. In this tutorial, we will explore the Stream API methods: sorted(), count(), and distinct() methods introduced in Java 8. 1. Introduction Before diving deep into the practice stuff let us understand the methods we will be covering in this tutorial. The distinct() method returns a stream of unique elements. It is a stateful intermediate &hellip;<\/p>\n","protected":false},"author":119,"featured_media":1204,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[478,774,212],"class_list":["post-105092","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-java","tag-java-8","tag-java-basics-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java 8 Stream API: distinct() count() sorted() - Examples Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In Java 8, distinct() returns a stream of unique elements, and sorted() returns a stream consisting of elements in the sorted natural order.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java 8 Stream API: distinct() count() sorted() - Examples Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In Java 8, distinct() returns a stream of unique elements, and sorted() returns a stream consisting of elements in the sorted natural order.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-10-01T08:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-17T11:25:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yatin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yatin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/\"},\"author\":{\"name\":\"Yatin\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13\"},\"headline\":\"Java 8 Stream API &#8211; distinct(), count() &#038; sorted() Example\",\"datePublished\":\"2021-10-01T08:00:00+00:00\",\"dateModified\":\"2021-12-17T11:25:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/\"},\"wordCount\":452,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"keywords\":[\"Java\",\"Java 8\",\"java basics\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/\",\"name\":\"Java 8 Stream API: distinct() count() sorted() - Examples Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"datePublished\":\"2021-10-01T08:00:00+00:00\",\"dateModified\":\"2021-12-17T11:25:18+00:00\",\"description\":\"In Java 8, distinct() returns a stream of unique elements, and sorted() returns a stream consisting of elements in the sorted natural order.\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"Bipartite Graph\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Java 8 Stream API &#8211; distinct(), count() &#038; sorted() Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13\",\"name\":\"Yatin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg\",\"caption\":\"Yatin\"},\"description\":\"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).\",\"sameAs\":[\"https:\/\/www.javacodegeeks.com\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java 8 Stream API: distinct() count() sorted() - Examples Java Code Geeks","description":"In Java 8, distinct() returns a stream of unique elements, and sorted() returns a stream consisting of elements in the sorted natural order.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/","og_locale":"en_US","og_type":"article","og_title":"Java 8 Stream API: distinct() count() sorted() - Examples Java Code Geeks","og_description":"In Java 8, distinct() returns a stream of unique elements, and sorted() returns a stream consisting of elements in the sorted natural order.","og_url":"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2021-10-01T08:00:00+00:00","article_modified_time":"2021-12-17T11:25:18+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","type":"image\/jpeg"}],"author":"Yatin","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yatin","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/"},"author":{"name":"Yatin","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13"},"headline":"Java 8 Stream API &#8211; distinct(), count() &#038; sorted() Example","datePublished":"2021-10-01T08:00:00+00:00","dateModified":"2021-12-17T11:25:18+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/"},"wordCount":452,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","keywords":["Java","Java 8","java basics"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/","name":"Java 8 Stream API: distinct() count() sorted() - Examples Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","datePublished":"2021-10-01T08:00:00+00:00","dateModified":"2021-12-17T11:25:18+00:00","description":"In Java 8, distinct() returns a stream of unique elements, and sorted() returns a stream consisting of elements in the sorted natural order.","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/java-8-stream-api-distinct-count-sorted-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"},{"@type":"ListItem","position":4,"name":"Java 8 Stream API &#8211; distinct(), count() &#038; sorted() Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13","name":"Yatin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","caption":"Yatin"},"description":"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/105092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/119"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=105092"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/105092\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1204"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=105092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=105092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=105092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}