{"id":371,"date":"2014-06-18T03:38:00","date_gmt":"2014-06-18T03:38:00","guid":{"rendered":"http:\/\/www.java2blog.com\/?p=371"},"modified":"2021-01-12T14:35:56","modified_gmt":"2021-01-12T09:05:56","slug":"comparable-in-java","status":"publish","type":"post","link":"https:\/\/java2blog.com\/comparable-in-java\/","title":{"rendered":"Comparable in java"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><ul><li><a href=\"#Comparable_interface\">Comparable interface:<\/a><\/li><\/ul><\/li><li><a href=\"#Java_code\">Java code:<\/a><ul><li><a href=\"#For_Comparable\">For Comparable:<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<div dir=\"ltr\" style=\"text-align: left;\">\n<p>In this post, we will see\u00a0 how you can use comparable to sort list of objects in java.<\/p>\n<h4 style=\"text-align: left;\"><span id=\"Comparable_interface\">Comparable interface:<\/span><\/h4>\n<p>Class whose objects to be sorted must implement this interface.In this,we have to implement compareTo(Object) method.<br \/>\nFor example:<\/p>\n<pre name=\"code\">public class Country implements Comparable{\n\u00a0\u00a0 \u00a0\u00a0\u00a0 @Override\n\u00a0\u00a0\u00a0 public int compareTo(Country country) {\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 return (this.countryId < country.countryId ) ? -1: (this.countryId > country.countryId ) ? 1:0 ;\n}}<\/pre>\n<p>If any class implements comparable inteface then collection of that object can be sorted automatically using Collection.sort() or Arrays.sort().Object will be sort on the basis of compareTo method in that class.<br \/>\nObjects which implement Comparable in java can be used as keys in a SortedMap like TreeMap or SortedSet like TreeSet without implementing any other interface.<\/p>\n<p><span lang=\"EN-US\">Sorting logic must be in same class whose objects are being sorted. Hence this is called natural ordering of objects<\/span><br \/>\n<!-- adsense --><\/p>\n<h3 style=\"text-align: left;\"><span id=\"Java_code\">Java code:<\/span><\/h3>\n<h4 style=\"text-align: left;\"><span id=\"For_Comparable\">For Comparable:<\/span><\/h4>\n<p>We will create class country having attribute id and name.This class will implement Comparable interface and implement CompareTo method to sort collection of country object by id.<\/p>\n<p><b>1. Country.java <\/b><\/p>\n<pre name=\"code\">package org.arpit.java2blog;\n\/\/If this.cuntryId < country.countryId:then compare method will return -1\n\/\/If this.countryId > country.countryId:then compare method will return 1\n\/\/If this.countryId==country.countryId:then compare method will return 0\npublic class Country implements Comparable{\n\u00a0\u00a0 \u00a0int countryId;\n\u00a0\u00a0 \u00a0String countryName;\n\u00a0\u00a0 \n\u00a0\u00a0 \n\u00a0\u00a0 \u00a0public Country(int countryId, String countryName) {\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0super();\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.countryId = countryId;\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.countryName = countryName;\n\u00a0\u00a0 \u00a0}\n\n\u00a0\u00a0 \u00a0@Override\n\u00a0\u00a0 \u00a0public int compareTo(Country country) {\n\u00a0\u00a0 \u00a0\u00a0\u00a0 return (this.countryId < country.countryId ) ? -1: (this.countryId > country.countryId ) ? 1:0 ;\n\u00a0\u00a0 \u00a0}\n\n\u00a0\u00a0 \u00a0public int getCountryId() {\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return countryId;\n\u00a0\u00a0 \u00a0}\n\n\u00a0\u00a0 \u00a0public void setCountryId(int countryId) {\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.countryId = countryId;\n\u00a0\u00a0 \u00a0}\n\n\u00a0\u00a0 \u00a0public String getCountryName() {\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return countryName;\n\u00a0\u00a0 \u00a0}\n\n\u00a0\u00a0 \u00a0public void setCountryName(String countryName) {\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.countryName = countryName;\n\u00a0\u00a0 \u00a0}\n\u00a0\u00a0 \n}<\/pre>\n<p><b>2.ComparableMain.java <\/b><\/p>\n<pre class=\"java\" name=\"code\">\npackage org.arpit.java2blog;\n\nimport java.util.ArrayList;\nimport java.util.Collections;\nimport java.util.List;\n\npublic class ComparableMain {\n\n    \/**\n     * @author Arpit Mandliya\n     *\/\n    public static void main(String[] args) {\n        Country indiaCountry=new Country(1, \"India\");\n        Country chinaCountry=new Country(4, \"China\");\n        Country nepalCountry=new Country(3, \"Nepal\");\n        Country bhutanCountry=new Country(2, \"Bhutan\");\n\n        List listOfCountries = new ArrayList();\n        listOfCountries.add(indiaCountry);\n        listOfCountries.add(chinaCountry);\n        listOfCountries.add(nepalCountry);\n        listOfCountries.add(bhutanCountry);\n\n        System.out.println(\"Before Sort  : \");\n        for (int i = 0; i < listOfCountries.size(); i++) {\n            Country country=(Country) listOfCountries.get(i);\n            System.out.println(\"Country Id: \"+country.getCountryId()+\"||\"+\"Country name: \"+country.getCountryName());\n        }\n        Collections.sort(listOfCountries);\n\n        System.out.println(\"After Sort  : \");\n        for (int i = 0; i < listOfCountries.size(); i++) {\n            Country country=(Country) listOfCountries.get(i);\n            System.out.println(\"Country Id: \"+country.getCountryId()+\"|| \"+\"Country name: \"+country.getCountryName());\n        }\n    }\n\n}\n<\/pre>\n<div style=\"text-align: left;\"><b>Output:<\/b><\/div>\n<pre name=\"code\">Before Sort  : \nCountry Id: 1||Country name: India\nCountry Id: 4||Country name: China\nCountry Id: 3||Country name: Nepal\nCountry Id: 2||Country name: Bhutan\nAfter Sort  : \nCountry Id: 1|| Country name: India\nCountry Id: 2|| Country name: Bhutan\nCountry Id: 3|| Country name: Nepal\nCountry Id: 4|| Country name: China<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsComparable interface:Java code:For Comparable: In this post, we will see\u00a0 how you can use comparable to sort list of objects in java. Comparable interface: Class whose objects to be sorted must implement this interface.In this,we have to implement compareTo(Object) method. For example: public class Country implements Comparable{ \u00a0\u00a0 \u00a0\u00a0\u00a0 @Override \u00a0\u00a0\u00a0 public int [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[44,144],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/371"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=371"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/371\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}