aspose file tools*
The moose likes Beginning Java and the fly likes HashMap Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashMap Problem" Watch "HashMap Problem" New topic
Author

HashMap Problem

Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
I'm trying to create a method that tests whether one HashMap is a subset of the other. Here's the method with the error I'm getting:


C:\Java Programs\Java 271\Ch11HW>javac Ch11Ex8.java
HashMap.java:28: cannot find symbol
symbol : method containsAll(java.util.Map<java.lang.String,java.lang.String>
location: interface java.util.Set<java.lang.String>
if(map2.keySet().containsAll(map1)){
^
1 error
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Ok, figured it out. This works:
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
I think I'm still missing one more part to this. The problem requires that the method test not only if every key in the first map is contained in the second map, but also that they map to the same value that's in the second map. So I need to come up with another piece of code for that.
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Altered the code as follows, but this does not work to catch a value that is different from the first map in the second:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19794
    
  20

I'm afraid there is no real fast solution, but to look at every entry one at a time:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Thank you, Rob. I didn't quite understand your syntax although it did give me some ideas. I came up with the following. Is my code basically the same logic as yours?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19794
    
  20

Just about yes. There are some improvements to be made though:


Now my code with some more explanations:

Now the containsKey is necessary to differentiate between not having the value (get returns null) or having the value null.

My code will perform just a bit better than yours because you iterate over the key set twice: once during containsAll and once in the for-loop. That leaves your two gets versus my get and containsKey, but in most implementations those are equally fast.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 863
    
    1
Is there any reason this wouldn't work?


I haven't tried it, but I believe the semantics of Map.Entry.equals() are adequate.


bitguru blog
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Rob,
I tried my code with your suggested improvements. It compiled but did not return the correct results. I very much appreciate your comments, though. They helped me understand the code, especially "if a then b else c". I've been trying to understand that syntax and making it way too hard.

Our textbook mentions entrySet just to say that there is such a thing but they're not going to cover it. So we can't use it in our solutions. I did try your code though and got the following compile error. Note that I changed the map names map1 and map2 to aMap and bMap respectively for reasons unrelated to this method:

HashMaptest.java:82: cannot find symbol
symbol : method getValue(java.lang.String)
location: interface java.util.Map<java.lang.String,java.lang.String>
String value2 = bMap.getValue(key);
^
1 error

I tried Brian's code too and got this compile error:
HashMaptest.java:56: unexpected type
required: variable
found : value
bMap.entrySet().containsAll(aMap.entrySet() ) = subMap(aMap, bMap);
^
1 error
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Just fixed Brian's code as follows and it works! Very nice. Unfortunately, I can't use it.


Dave
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19794
    
  20

Originally posted by Brian Cole:
Is there any reason this wouldn't work?

It should yes. The simplest solution and it didn't even cross my mind...

Originally posted by Dave DiRito:
Rob,
I tried my code with your suggested improvements. It compiled but did not return the correct results.

That's because the body of my loop is completely wrong; there isn't even a call to aMmap.get to get the value! As a result, I was comparing the keys to the values. D'OH!

So this should be better:
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Rob, yes, it looks like that code would work.

Thanks,
Dave
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HashMap Problem