Here's a simple thing that I can't find a simple solution for - so any suggestions will be very welcome...

I have a Map<String, MyClass> and if it has more than 1 entry I let the user pick a MyClass instance by displaying the keys. No problem there.

If it has exactly one entry I just want to use that single MyClass instance.
The best code I have come up with so far is the horribly tacky
(MyClass) myMap.values().toArray()[0];

Surely there's a better way? Someone...?

I would prefer myMap.values().iterator().next(). It is also a bit tricky and potentially confusing, but it has the benefit of being type-safe. With a comment to explain what I'm doing, I wouldn't feel too bad about it.

On the other hand, it seems that most of the awkwardness comes from you using a Map for something other than looking up values by keys, which is all that Map is really intended for. It seems that what you really want is a List, either of keys or key-value pairs. A list would make extracting a lone element simple, and it would make it easier to present the user with a menu. So why not convert the map into a list before you begin and save yourself this trouble?

commented: Was the first I thought about when reading the above post. +13

@JamesCherill
Hey James, I was shocked to see you asking the question. I would love to help you but unfortunately i never worked on Collections so far. :) .(As I completed my graudation recently and searching for a job). I will be watching this thread. Hope you get the better solution.:) I will search answer meanwhile

I would prefer myMap.values().iterator().next().

Yes, I like that better as well, especially because it eliminates that horrible cast. Somehow I never thought of using an iterator without a loop! Thank you very much.
OK GUYS - BGUILD HAS THROWN DOWN THE CHALLENGE!

Hey James, I was shocked to see you asking the question

Why? Do you think anyone ever gets to know the best way to do everything in Java? Is anyone so smart they can't learn from someone else? I'm flattered by your shock, but it's completely unjustified :)

Why? Do you think anyone ever gets to know the best way to do everything in Java? Is anyone so smart they can't learn from someone else? I'm flattered by your shock, but it's completely unjustified :)

You took me in a wrong way. I see your posts regularly helping other OP's . But i suddenly saw you posting a question. So I saw your post as little bit of suprise..(Dont misunderstand me :))Yes ofcourse, no one is perfect .

I was faced with a similar situation in the past and decided to adopt a completely different approach. Since you really don't care about the key, I'm assuming this is some sort of a default case and no custom mapping has been added. In that case, something like this worked better for me:

// map => LinkedHashMap to maintain the ordering
if (map.isEmpty()) { // no custom mapping
   return config.getDefaultMyClassInstance();
} else {
   return map.get(yourKey);
}

Of course, might not fit your use case here; just throwing out some random food for thought.

EDIT: If using something like Python, the solution can boil down to a single line:

val = mapping.get( your_key, default_object )

Harinath: No, don't worry, my reply was toung-in-cheek anyway.

Sanjay: The actual situation is a client that can connect to one of a (small-ish) number of part-time servers. If only one server is available the client connects to that one. If more than one is available the user is given the list to chose from (the keys are server names, and the values are InetSocketAddress instances, the map is created dynamically by mDNS). The problem is that with only 1 entry I have no idea what the key will be. There is no default.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.