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
    319
    Rep Power
    8

    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
    14,051
    Blog Entries
    7
    Rep Power
    23

    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
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Help me on Collection
    By kathir0301 in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 12:08 PM
  2. collection
    By D.Calladine in forum New To Java
    Replies: 1
    Last Post: 12-02-2010, 03: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, 12:52 PM
  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
  •