0

Scenario

I have some values of the type : (Name - Email)

From this couples of data, I have to set a ArrayList of "Name" values,something like: |Name1|Name2| --- |NameN| to set a Spinner. (Ordered alphabetically)

When I choose some Name in the spinner, I have to save the Email associated, to send it to other site.

Problem

My problem is that I don't know how to do this.

I mean, how I have to save all this tuplas, for once I selected the name, search it in the data? What kind of array/hashmap/hastable I have to use to save it?

Maybe an image would help a bit.

enter image description here

1
  • 2
    What spinner are you using? This sounds like a question for SO.
    – null
    Feb 19, 2016 at 10:02

1 Answer 1

1

An array is probably not a good option, since you don't want to have to iterate over it in order to find the right value. If you have many tuples, the time needed to determine the email associated to a name may become important. If you double the number of names, you can expect you need twice the time, too.

Hashtables and HashMap are similar, but tables offer a synchronized access to their values, and are therefore thread-safe. Consequently, you should privilege HashMap over Hashtable if you don't need such a tread safety.

Even better, you don't really care about HashMap. All you want is a structure that allows you to very quickly find the value associated to a key. That's what the Map interface is designed for. HashMap is an implementation of Map.

A straightforward solution seems to store the tuples as map entries, and to use this map as an association table.

Let's consider you have a Tuple class with two property name and email.

Map<String, String> associationTable = new HashMap<>();

for(tuple : myTuples){
   associationTable.put(tuple.name, tuple.email);
}

After that, if you want all your names (for instance, for populating a spinner), just ask it:

Set<String> myNames = associationTable.keySet();

And if you want the email associated to a given name:

String email = associationTable.get(name);

HashMap is based on a hashtable, a structure that offers constant-time performance for the insertion and retrieving operations (if your keys have a good hash function). So, basically, you will "instantaneously" get the email associated to the name, regardless the number of tuples in your structure.

Maps may have different names in other languages: dictionaries, association tables, hash tables, etc.

If thread safety is required, different alternatives exist. See for instance this other post for a comparison.

2
  • "you should privilege HashMap over Hashtable if you don't need such a tread safety." This seems to imply that the opposite is true - if you need thread safety, you should use Hashtable. Which isn't really correct - and the Javadoc states it - the right thing to do is to use ConcurrentHashMap or Collections.synchronizedMap since around 12 years ago. I wouldn't even mention Hashtable in any context apart from history or uber-legacy code.
    – Ordous
    Feb 19, 2016 at 16:07
  • "I wouldn't even mention Hashtable in any context apart from history or uber-legacy code." The OP mentioned Hashtable, so I discussed it. Plus, by no mean not(A) => not(B) implies A => B. Also, Hashtable ensures thread safety, but in a suboptimal way. So it is definitively not a bad solution. I added a link in my answer for a detailed comparison between Hashtable, ConcurrentHashMap, and Collections.synchronizedMap.
    – mgoeminne
    Feb 20, 2016 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.