The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java : Collection Framework : Collections (UnmodifiableCountryList)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java : Collection Framework : Collections (UnmodifiableCountryList) Posted: Jul 8, 2015 11:09 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java : Collection Framework : Collections (UnmodifiableCountryList)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube :
https://www.youtube.com/watch?v=d02V7ZmaWnk&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);
}
}
/*
* Returns an unmodifiable view of the specified list.
*
* If we don't want calling client method to modify the
* filteredCountryList then we can make unmodifiable like below.
*/

return Collections.unmodifiableList(filteredCountryList);
}

}
Client.java
import java.util.List;

/*
Method:

public static <T> List<T> unmodifiableList(List<? extends T> list)

Parameters:

list - the list for which an unmodifiable view is to be returned.

Returns:

an unmodifiable view of the specified list.

*/


public class Client
{

public static void main(String[] args)
{

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

System.out.println("countryList : " + countryList + "\n");

countryList.add("USA");

}

}
Output
countryList : [India, Iran]

Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055)
at Client.main(Client.java:29)
To Download CollectionsDemoUnmodifiableCountryListApp Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/CollectionsDemoUnmodifiableCountryListApp.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
  • Read: Java : Collection Framework : Collections (UnmodifiableCountryList)

    Topic: Java : Collection Framework : Collections (Singleton) Previous Topic   Next Topic Topic: Java : Collection Framework : Collections [UnModifiableList]

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use