Thursday, 2 July 2015

Java : Collection Framework : Collections (Sort List of User defined Objects Asc Using Comparator)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=sqdFzUCnNHU&list=UUhwKlOVR041tngjerWxVccw

Country.java
public class Country
{
    private int countryId;
    private String countryName;

    public Country(int countryId, String countryName)
    {
        super();
        this.countryId = countryId;
        this.countryName = countryName;
    }

    public int getCountryId()
    {
        return countryId;
    }

    public void setCountryId(int countryId)
    {
        this.countryId = countryId;
    }

    public String getCountryName()
    {
        return countryName;
    }

    public void setCountryName(String countryName)
    {
        this.countryName = countryName;
    }
    
}
CountrySortByIdComparator.java
import java.util.Comparator;

public class CountrySortByIdComparator implements Comparator<Country>
{

    /*
     * This method has logic to arrange the country objects in Ascending order based on Country Id.
     * 
     * If country1.getCountryId()<country2.getCountryId():then compare method
     * will return -1
     * 
     * If country1.getCountryId()>country2.getCountryId():then compare method
     * will return 1
     * 
     * If country1.getCountryId()==country2.getCountryId():then compare method
     * will return 0
     */

    @Override
    public int compare(Country country1, Country country2)
    {

        System.out
                .println("Compare method in CountrySortByIdComparator has been called"
                        + "\nin order to arrange the country objects 
                           in ascending order \nbased on Country Id\n");

        return (country1.getCountryId() < country2.getCountryId()) ? -1
                : (country1.getCountryId() > country2.getCountryId()) ? 1 : 0;
    }

}
CollectionsSortExample.java
import java.util.ArrayList;
import java.util.Collections;

/*
 *  Example of sort(List<T> list, Comparator<? super T> c) method.
 */

public class CollectionsSortExample
{

    public static void main(String[] args)
    {
        Country india = new Country(1, "India");
        Country china = new Country(4, "China");
        Country usa = new Country(3, "USA");
        Country srilanka = new Country(2, "Srilanka");

        ArrayList<Country> countryList = new ArrayList<Country>();
        countryList.add(india);
        countryList.add(china);
        countryList.add(usa);
        countryList.add(srilanka);

        System.out.println("Before Sort  : \n");

        for (Country country : countryList)
        {
            System.out.println("Country Id: " + country.getCountryId() + " || "
                    + "Country Name: " + country.getCountryName());
        }

        System.out.println("-------------------------------------------------");

        /*
         * Sorts the specified list according to the order induced by the
         * specified comparator.
         * 
         * All elements in the list must be mutually comparable using the
         * specified comparator (that is, c.compare(e1, e2) must not throw a
         * ClassCastException for any elements e1 and e2 in the list).
         */

        Collections.sort(countryList, new CountrySortByIdComparator());

        System.out.println("-------------------------------------------------");
        System.out.println("\nAfter Sort  : \n");

        for (Country country : countryList)
        {
            System.out.println("Country Id: " + country.getCountryId() + " || "
                    + "Country Name: " + country.getCountryName());
        }

    }

}
Output
Before Sort  : 

Country Id: 1 || Country Name: India
Country Id: 4 || Country Name: China
Country Id: 3 || Country Name: USA
Country Id: 2 || Country Name: Srilanka
-------------------------------------------------
Compare method in CountrySortByIdComparator has been called
in order to arrange the country objects in ascending order 
based on Country Id

Compare method in CountrySortByIdComparator has been called
in order to arrange the country objects in ascending order 
based on Country Id

Compare method in CountrySortByIdComparator has been called
in order to arrange the country objects in ascending order 
based on Country Id

Compare method in CountrySortByIdComparator has been called
in order to arrange the country objects in ascending order 
based on Country Id

Compare method in CountrySortByIdComparator has been called
in order to arrange the country objects in ascending order 
based on Country Id

Compare method in CountrySortByIdComparator has been called
in order to arrange the country objects in ascending order 
based on Country Id

-------------------------------------------------

After Sort  : 

Country Id: 1 || Country Name: India
Country Id: 2 || Country Name: Srilanka
Country Id: 3 || Country Name: USA
Country Id: 4 || Country Name: China
To Download CollectionsSortDemoUserDefinedCountryListComparatorApp Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/CollectionsSortDemoUserDefinedCountryListComparatorApp.zip?attredirects=0&d=1

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment