• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap Problem

 
Dave DiRito
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, figured it out. This works:
 
Dave DiRito
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid there is no real fast solution, but to look at every entry one at a time:
 
Dave DiRito
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 899
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any reason this wouldn't work?


I haven't tried it, but I believe the semantics of Map.Entry.equals() are adequate.
 
Dave DiRito
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just fixed Brian's code as follows and it works! Very nice. Unfortunately, I can't use it.


Dave
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, yes, it looks like that code would work.

Thanks,
Dave
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic