From: gu.martinm@gmail.com Date: Mon, 9 Sep 2013 19:20:58 +0000 (+0200) Subject: Comparable VS Comparator: example X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=b3444b80885f0f1c0df8014fe721a4508c3af276;p=JavaForFun Comparable VS Comparator: example --- diff --git a/Allgemeines/ComparableVSComparator/src/de/comparable/example/Country.java b/Allgemeines/ComparableVSComparator/src/de/comparable/example/Country.java new file mode 100644 index 0000000..b19434a --- /dev/null +++ b/Allgemeines/ComparableVSComparator/src/de/comparable/example/Country.java @@ -0,0 +1,27 @@ +package de.comparable.example; + + +public class Country implements Comparable { + private final int countryId; + private final String countryName; + + public Country(final int countryId, final String countryName) { + this.countryId = countryId; + this.countryName = countryName; + } + + @Override + public int compareTo(final Country country) { + return (this.countryId < country.countryId) ? -1 + : (this.countryId > country.countryId) ? 1 : 0; + } + + public int getCountryId() { + return this.countryId; + } + + public String getCountryName() { + return this.countryName; + } + +} \ No newline at end of file diff --git a/Allgemeines/ComparableVSComparator/src/de/comparable/example/CountryNoComparable.java b/Allgemeines/ComparableVSComparator/src/de/comparable/example/CountryNoComparable.java new file mode 100644 index 0000000..dbb43d7 --- /dev/null +++ b/Allgemeines/ComparableVSComparator/src/de/comparable/example/CountryNoComparable.java @@ -0,0 +1,19 @@ +package de.comparable.example; + +public class CountryNoComparable { + private final int countryId; + private final String countryName; + + public CountryNoComparable(final int countryId, final String countryName) { + this.countryId = countryId; + this.countryName = countryName; + } + + public int getCountryId() { + return this.countryId; + } + + public String getCountryName() { + return this.countryName; + } +} diff --git a/Allgemeines/ComparableVSComparator/src/de/comparable/example/CountrySortByIdComparator.java b/Allgemeines/ComparableVSComparator/src/de/comparable/example/CountrySortByIdComparator.java new file mode 100644 index 0000000..4084fe1 --- /dev/null +++ b/Allgemeines/ComparableVSComparator/src/de/comparable/example/CountrySortByIdComparator.java @@ -0,0 +1,13 @@ +package de.comparable.example; + +import java.util.Comparator; + +public class CountrySortByIdComparator implements Comparator { + + @Override + public int compare(final Country country1, final Country country2) { + return (country1.getCountryId() < country2.getCountryId()) ? -1 + : (country1.getCountryId() > country2.getCountryId()) ? 1 : 0; + } + +} diff --git a/Allgemeines/ComparableVSComparator/src/de/comparable/example/Example.java b/Allgemeines/ComparableVSComparator/src/de/comparable/example/Example.java new file mode 100644 index 0000000..61e3754 --- /dev/null +++ b/Allgemeines/ComparableVSComparator/src/de/comparable/example/Example.java @@ -0,0 +1,117 @@ +package de.comparable.example; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +/** + *

+ * Comparable: to be used by objects which want to be compared. + *

+ *

+ * Comparator: to be used by sort algorithms to change the sort comparison or + * when trying to sort objects which do not implement the Comparable interface + * because they did not intend to be compared. + *

+ */ +public class Example { + + public static void main(final String[] args) { + final Country indiaCountry = new Country(1, "India"); + final Country chinaCountry = new Country(4, "China"); + final Country nepalCountry = new Country(3, "Nepal"); + final Country bhutanCountry = new Country(2, "Bhutan"); + final Country bhutanCountryAgain = new Country(2, "Bhutan"); + + // 1. Using Comparable. + List listOfCountries = new ArrayList(); + listOfCountries.add(indiaCountry); + listOfCountries.add(chinaCountry); + listOfCountries.add(nepalCountry); + listOfCountries.add(bhutanCountry); + listOfCountries.add(bhutanCountryAgain); + + System.out.println("Before Sort : "); + for (int i = 0; i < listOfCountries.size(); i++) { + final Country country = listOfCountries.get(i); + System.out.println("Country Id: " + country.getCountryId() + "||" + + "Country name: " + country.getCountryName()); + } + + Collections.sort(listOfCountries); + + System.out.println("After Sort : "); + for (int i = 0; i < listOfCountries.size(); i++) { + final Country country = listOfCountries.get(i); + System.out.println("Country Id: " + country.getCountryId() + "|| " + + "Country name: " + country.getCountryName()); + } + + + // 2. Using Comparator (the same result) + listOfCountries = new ArrayList(); + listOfCountries.add(indiaCountry); + listOfCountries.add(chinaCountry); + listOfCountries.add(nepalCountry); + listOfCountries.add(bhutanCountry); + listOfCountries.add(bhutanCountryAgain); + + System.out.println("Before Sort by id : "); + for (int i = 0; i < listOfCountries.size(); i++) { + final Country country=listOfCountries.get(i); + System.out.println("Country Id: " + country.getCountryId() + "||" + + "Country name: " + country.getCountryName()); + } + + Collections.sort(listOfCountries,new CountrySortByIdComparator()); + + System.out.println("After Sort by id: "); + for (int i = 0; i < listOfCountries.size(); i++) { + final Country country=listOfCountries.get(i); + System.out.println("Country Id: " + country.getCountryId() + "|| " + + "Country name: " + country.getCountryName()); + } + + + // 3. Using Comparator (with new sort) + // Country objects could not be designed to be compared, but by means of + // Comparator interface we could compare them even if the were not + // designed to be compared. + final CountryNoComparable indiaCountryNoComparable = new CountryNoComparable(1, "India"); + final CountryNoComparable chinaCountryNoComparable = new CountryNoComparable(4, "China"); + final CountryNoComparable nepalCountryNoComparable = new CountryNoComparable(3, "Nepal"); + final CountryNoComparable bhutanCountryNoComparable = new CountryNoComparable(2, "Bhutan"); + final CountryNoComparable bhutanCountryAgainNoComparable = new CountryNoComparable(2, "Bhutan"); + + final List listOfCountriesNoComparable = new ArrayList(); + listOfCountriesNoComparable.add(indiaCountryNoComparable); + listOfCountriesNoComparable.add(chinaCountryNoComparable); + listOfCountriesNoComparable.add(nepalCountryNoComparable); + listOfCountriesNoComparable.add(bhutanCountryNoComparable); + listOfCountriesNoComparable.add(bhutanCountryAgainNoComparable); + + System.out.println("Before Sort by name : "); + for (int i = 0; i < listOfCountries.size(); i++) { + final Country country = listOfCountries.get(i); + System.out.println("Country Id: " + country.getCountryId() + "||" + + "Country name: " + country.getCountryName()); + } + + Collections.sort(listOfCountriesNoComparable, new Comparator() { + + @Override + public int compare(final CountryNoComparable o1, final CountryNoComparable o2) { + // We can compare even if CountryNoComparable was not designed for it :) + return o1.getCountryName().compareTo(o2.getCountryName()); + } + }); + + System.out.println("After Sort by name: "); + for (int i = 0; i < listOfCountries.size(); i++) { + final Country country=listOfCountries.get(i); + System.out.println("Country Id: " + country.getCountryId() + "|| " + + "Country name: " + country.getCountryName()); + } + } +}