Wednesday, 1 July 2015

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


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

Country.java
public class Country implements Comparable<Object>
{
    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;
    }

    /*
     * This method has logic to arrange the Country objects in Ascending order based
     * on CountryId
     */
    @Override
    public int compareTo(Object object)
    {

        System.out.println("\n"+"compareTo method is called by sort method : "+ object);
        /*
         * If this.countryId < country.countryId:then compare method will return
         * -1
         * 
         * If this.countryId > country.countryId:then compare method will return
         * 1
         * 
         * If this.countryId==country.countryId:then compare method will return
         * 0
         */

        Country country = (Country) object;

        return (this.countryId < country.countryId) ? -1
                : (this.countryId > country.countryId) ? 1 : 0;
    }

}

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

/*
 *  Example of sort(List<T> list) 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  : ");

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

        /*
         * Sorts the specified list into ascending order, according to the
         * natural ordering of its elements.
         * 
         * All elements in the list must implement the Comparable interface.
         */
        Collections.sort(countryList);

        
        System.out.println("\nAfter Sort  : ");
        
        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

compareTo method is called by sort method : Country@659e0bfd

compareTo method is called by sort method : Country@2a139a55

compareTo method is called by sort method : Country@2a139a55

compareTo method is called by sort method : Country@659e0bfd

compareTo method is called by sort method : Country@15db9742

compareTo method is called by sort method : Country@659e0bfd

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 CollectionsSortDemoUserDefinedCountryListApp Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/CollectionsSortDemoUserDefinedCountryListApp.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

    Google+ Followers

    Google+ Badge