java - How to add value into collection -
i have hashtable this:
hashtable<string, string> ht = new hashtable<string, string>(); ht.put("a", "one"); ht.put("b", "two"); then called values() methods values , stored in collection object this:
collection<string> col = ht.values(); now collection object has these values:
one, two. then called col.add(" three"); time got error:
exception in thread "main" java.lang.unsupportedoperationexception. i checked api:
if collection refuses add particular element reason other contains element, must throw exception (rather returning false) the value added("three") collection unique,not duplicate.but can other operations remove() , clear() operations on it.
not able call add().why not allowing add?
the collection returned values() method in hashtable not support adding new elements.
from javadocs:
returns collection view of values contained in hashtable. collection backed hashtable, changes hashtable reflected in collection, , vice-versa. collection supports element removal (which removes corresponding entry hashtable), but not element addition.
Comments
Post a Comment