Click here to watch in Youtube :
https://www.youtube.com/watch?v=WwNbKSkEBdQ&list=UUhwKlOVR041tngjerWxVccw
CountryInfo.java
Client.java
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; } }
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()); } }
countryList size : 0
https://sites.google.com/site/javaee4321/java-collections/CollectionsDemo-EmptyList-Country.zip?attredirects=0&d=1
See also:
No comments:
Post a Comment