26

What is the best implementation for this general-purpose library method?

public static <K, V> boolean containsEntry(
    Map<K, V> map, K key, V value) {}

Criteria for judging this puzzle, as with most coding puzzles, are in this order:

  1. Completeness
  2. Correctness
  3. Performance
  4. Beauty
  5. Receipt of PayPal contribution

EDIT:

Well, since it got closed, I might as well post the answer. I think this is probably optimal:

  V valueForKey = map.get(key);
  return (valueForKey == null)
      ? value == null && map.containsKey(key)
      : valueForKey.equals(value);

A clever simple solution would be:

  return map.entrySet().contains(
      new AbstractMap.SimpleImmutableEntry<K, V>(key, value));

It does allocate an instance, but it gives the map implementation a little more opportunity to do something optimal.

Kevin Bourrillion
  • 40,336
  • 12
  • 74
  • 87
  • "There is at least one best answer." - No, the is **one** 'best' answer by definition... – jjnguy Nov 06 '09 at 05:15
  • Kevin, if you are going to ask questions similar to this, you need to lay down some better rules. Otherwise, this really isn't a question that can be sufficiently answered. – jjnguy Nov 06 '09 at 05:19
  • 3
    Ok, I'm trying. See edit. I'm curious about your definition of best, though. If four cards of my poker hand are 2 of clubs, 3 of clubs, ace of spades, ace of hearts, what is the best fifth card I could have? – Kevin Bourrillion Nov 06 '09 at 05:36
  • Well, I see what you mean, but the best card in that case would be an Ace. – jjnguy Nov 06 '09 at 05:43
  • I'm curious about the completeness/correctness distinction. Is an incomplete solution really "correct"? – Laurence Gonsalves Nov 06 '09 at 05:49
  • Yeah, Laurence, somehow I don't think that's what is getting my puzzles all closed. :) – Kevin Bourrillion Nov 06 '09 at 06:07
  • FWIW I think these types of questions **do** have a place on the site. Some may disagree though. – jjnguy Nov 06 '09 at 06:17
  • 5
    I don't know about that. At least 2 more votes to close came in after I added the criteria. I think the real problem here is that I admitted that I already know the answer -- meaning, of course, that I already *think* I know the answer. I think most people seem to not want SO to be used this way. – Kevin Bourrillion Nov 06 '09 at 06:22
  • The site was not designed for this type of question. But I think it can be interesting to discuss a bit. – jjnguy Nov 06 '09 at 06:26
  • 4
    Well then, vote to reopen. :) I found a post that indicates most users do welcome this kind of post: http://meta.stackexchange.com/questions/25437/posting-programming-algorithmic-puzzles-on-so – Kevin Bourrillion Nov 06 '09 at 06:30
  • 4
    Wait a minute! Something is bugging me now about my puzzles being closed for not explicitly defining "best". Any SO post could use the phrasing "what's the best way to do X"? And the ones that don't say that? Well guess what, the asker is still going to use his own totally unspecified judgment on which answer he thinks was the best. And it gets worse: VOTERS are judging "bestness" too! And no one defines "best"! Unbelievable! It's like you were worried that I had one exact specific answer in mind and only that answer would be rewarded. But I just figured voting would happen as usual. – Kevin Bourrillion Nov 07 '09 at 18:22
  • @Kevin, This site can be quite inconsistent at times. Take a look at how this "best" question has done: http://stackoverflow.com/questions/1631414/what-is-the-best-battleship-ai – Jim Ferrans Nov 29 '09 at 14:44

3 Answers3

3
public static <K, V> boolean containsEntry(Map<K, V> map, K key, V value) {
    returns map.containsKey(key) && isEqual(map.get(key), value);
}
private static boolean isEqual(Object a, Object b) {
    return a == null ? a == b : a.equals(b);
}

Copied from deleted post.

Community
  • 1
  • 1
jjnguy
  • 136,852
  • 53
  • 295
  • 323
0
public static <K, V> boolean containsEntry(Map<K, V> map, K key, V value) {
    returns map.containsKey(key) & isEquals(map.get(key), value);
}
private static boolean isEqual(Object a, Object b) {
    return a == null ? a == b : a.equals(b);
}

You can also inline isEqual method.

notnoop
  • 58,763
  • 21
  • 123
  • 144
0

Presumably it's meant to return a boolean:

public static <K, V> boolean containsEntry(Map<K, V> map, K key, V value) {
    return map.containsKey(key) && map.get(key).equals(value);
}
harto
  • 89,823
  • 9
  • 47
  • 61