Results 1 to 9 of 9

Thread: maps

  1. #1
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default maps

    Hello,

    I want to write a method that will give me the union of 2 maps. So far i wrote some code that compiled and gave me no error, but when i run it it gives me an error which i couldnt find why it was giving it to me.

    The error : java.lang.UnsupportedOperationException: null (in java.util.AbstractCollection)

    and here is my code:

    Java Code:
    import java.util.Map;
    import java.util.HashMap;
    import java.util.Set;
    import java.util.Iterator;
    
    public class Ex9
    {
        public static void main (String[] args)
        {
            Map<Integer, Integer> map1 = new HashMap<Integer, Integer> ();
            
            map1.put(7, 1);
            map1.put(18, 5);
            map1.put(42, 3);
            map1.put(76, 10);
            map1.put(98, 2);
            map1.put(234, 50);
            
            Map<Integer, Integer> map2 = new HashMap<Integer, Integer> ();
            
            map2.put(7, 2);
            map2.put(11, 9);
            map2.put(42, -12);
            map2.put(98, 4);
            map2.put(234, 0);
            map2.put(999, 3);
            
            System.out.println("Map 1 : " + map1);
            System.out.println("Map 2 : " + map2);
            
            Map<Integer, Integer> result = union(map1, map2);
            System.out.println("The union of the above maps is : " + result);
        }
        
        /**
         * Returns a new map of integer keys and values which consists on the union of 2 maps.
         * 
         * @param map1 The first map of integer keys and values.
         * @param map2 The second map of integer keys and values.
         * @return A new map which is the union of both maps.
         */
        public static Map<Integer, Integer> union (Map<Integer, Integer> map1, Map<Integer, Integer> map2)
        {
            if (map1.isEmpty())
            {
                return map2;
            }else if (map2.isEmpty()) {
                return map1;
            }else{
                Map<Integer, Integer> result = new HashMap<Integer, Integer> ();
                
                Set<Integer> keys = map1.keySet();
                [B]keys.addAll(map2.keySet());[/B]
                
                Iterator<Integer> i = keys.iterator();
                
                while (i.hasNext())
                {
                    int key = i.next();
                    
                    if (map1.containsKey(key) && map2.containsKey(key))
                    {
                        int val1 = map1.get(key);
                        int val2 = map2.get(key);
                        result.put(key, (val1 + val2));
                    }else if (map1.containsKey(key) && !map2.containsKey(key)){
                        result.put(key, map1.get(key));
                    }else{
                        result.put(key, map2.get(key));
                    }
                }
                
                return result;
            }
        }
    }
    Iam getting an error at the bold line position

    Also, is this a correct way of doing the union of two maps?

    Regards,

    EDIT: I just tried this this line of code too but didnt work
    Java Code:
    Set<Integer> keys = map1.keySet();
    Set<Integer> keys2 = map2.keySet();
                
    Iterator<Integer> i2 = keys2.iterator();
    while (i2.hasNext())
    {
        keys.add(i2.next());
    }
    Last edited by aizen92; 05-23-2011 at 06:07 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Please post the FULL text of the error message and not just one part of it. The full message has useful info for understanding the cause of the error.

  3. #3
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    I use BlueJ and it gave me the error down anyway I'll post a screenshot of the code page and the console



    Hope this helps

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    This is straight from the API documentation for the keySet() method in the HashMap class:

    Quote Originally Posted by API docs
    Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.
    Pay special attention to the last sentence.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    The key set returned is some form of abtract collection with mostly dummied out operations (the ones that add things), because you are not supposed to edit either the key set or the entry set, which would bypass the Map code.

    Anyway, you want to know the union, so why not simply iterate over the key set from one of the maps. After all, you don't care about the ones in the other Map that aren't in the set you're iterating over...

  6. #6
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    So anything that returns a set view, I'm not supposed to modify it?

    @Tolls: But if i iterate over one key set, and i get the values with the common keys, i then still have the ones without the common keys ...

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Create a new clean object:

    Set<Integer> keys = new HashSet<Integer>(map1.keySet());

  8. #8
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    @Norm : Okay, thnx it worked with creating a hash set object.

    Thanks everybody for the help.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    So anything that returns a set view, I'm not supposed to modify it?

    @Tolls: But if i iterate over one key set, and i get the values with the common keys, i then still have the ones without the common keys ...
    No it doesn't.
    You still use the same loop above, but loop over map1.
    If map2 contains the key then add that into your result.
    Java Code:
    get map 1 keyset.
    iterate over keyset.
    if key exists in map 2 then add map1 and map2 values and add key and total value into result.

Similar Threads

  1. Help with Maps
    By snripa in forum New To Java
    Replies: 4
    Last Post: 12-14-2010, 09:27 AM
  2. Maps and Sets
    By darkblue24 in forum New To Java
    Replies: 19
    Last Post: 03-25-2010, 06:13 PM
  3. Maps and Sets
    By RedKMan in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 09:36 AM
  4. Maps
    By natep67 in forum New To Java
    Replies: 8
    Last Post: 05-06-2009, 03:59 AM
  5. Google Maps API
    By mew in forum New To Java
    Replies: 0
    Last Post: 12-26-2007, 10:28 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
  •