Results 1 to 3 of 3

Thread: Collection Help

  1. #1
    spartan is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default Collection Help

    Hi All,

    I have a program Like this
    public class sample {


    public static void main(String[] args) {

    Collection<Map<String, String>> collectionMap = getCollectionMap();
    Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
    Map<String, String> insideMap = new HashMap<String, String>();
    insideMap.put("1", "A");
    insideMap.put("2", "B");

    map.put("Map1", insideMap);
    map.put("Map2", insideMap);

    collectionMap.addAll(map.values());
    System.out.println(collectionMap);
    }

    private static Collection<Map<String, String>> getCollectionMap() {
    TreeMap<String, Map<String, String>> treeMap = new TreeMap<String, Map<String, String>>();
    Map<String, String> insideMap = new HashMap<String, String>();
    insideMap.put("1", "A");
    insideMap.put("2", "B");
    treeMap.put("Map1", insideMap);
    treeMap.put("Map2", insideMap);

    return treeMap.values();

    }

    I get an error
    Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractCollection.add(Unknown Source)
    at java.util.AbstractCollection.addAll(Unknown Source)
    at sample.main(sample.java:209)

    in the Line collectionMap.addAll(map.values());

    could anybody help me in this?

    Thanks,
    Guru

  2. #2
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Do you want to add all values of map to collectionMap
    sanjeev,संजीव

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Straight from the API documentation for the values() method:

    Quote Originally Posted by API
    It does not support the add or addAll operations.
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help me on Collection
    By kathir0301 in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 11:08 AM
  2. collection
    By D.Calladine in forum New To Java
    Replies: 1
    Last Post: 12-02-2010, 02:36 PM
  3. java collection api
    By rajinder5 in forum Advanced Java
    Replies: 1
    Last Post: 10-07-2010, 01:51 PM
  4. help for collection api
    By javastuden in forum New To Java
    Replies: 3
    Last Post: 02-24-2010, 11:52 AM
  5. how to creat collection
    By STILET in forum Advanced Java
    Replies: 2
    Last Post: 07-29-2008, 07:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •