Friday, 10 July 2015

Java : Collection Framework : Collections (emptyList Country)


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

CountryInfo.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CountryInfo
{

    List<String> countryList = new ArrayList<String>()
    {
        private static final long serialVersionUID = 1L;

        {
            add("India");
            add("Pakistan");
            add("China");
            add("Iran");
        }
    };

    public List<String> getCountryList(String startingWith)
    {

        if (startingWith == null)
        {
            /*
             * You should always return an emptyList instead of null
             */
            //return null;
            return Collections.emptyList();
        }

        ArrayList<String> filteredCountryList = new ArrayList<String>();
        for (String countryName : countryList)
        {
            if (countryName.startsWith(startingWith))
            {
                filteredCountryList.add(countryName);
            }
        }
        return filteredCountryList;
    }

}
Client.java
import java.util.List;

/*
 Method: 

 public static final <T> List<T> emptyList()

 Parameters:

 c - the collection for which an enumeration is to be returned.

 Returns:

 an empty immutable list.

 */

public class Client
{

    public static void main(String[] args)
    {

        CountryInfo countryInfo = new CountryInfo();
        List<String> countryList = countryInfo.getCountryList(null);

        System.out.println("countryList size : " + countryList.size());

    }

}
Output
countryList size : 0
To Download CollectionsDemo-EmptyList-Country Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/CollectionsDemo-EmptyList-Country.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