tags:

views:

326

answers:

9

Is it possible to set more than two pair value?

For example:

Map<String,String,String,String>

number,name,address,phone - All come together for display the values. Each value associated with others.

+32  A: 

You're in object denial. You should use an object that holds the number, name, address, phone (maybe you could call it ContactInformation) and put that into the map.

Joachim Sauer
+1 for the "Object denial" diagnosis
helios
yup. "You're in Object denial." Gotta get a T-Shirt with that slogan (+1)
seanizer
Example Please..
@user: Bozho provided a good example, look at his answer.
Joachim Sauer
@ once u use object please implement hashcode and equals.
Suresh S
+20  A: 

No. a Map has only one key. If you want your value to contain more information, wrap the strings in a new class:

public class PersonalInfo {
   private String name;
   private String address;
   private String phone;

   // constructor, getters and setters
}

map.put(number, new PersonalInfo(name, address, phone));
Bozho
+1 for beating me....
The Elite Gentleman
PersonalInfo info = new PersonalInfo(name, address, phone); map.put(info.getPhone(), info);
Ashish Patil
Put some `final`s in there!
Tom Hawtin - tackline
+4  A: 

Nope, A Map can have only one key and mapped to one value.

This is the Javadoc for Map:

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

What you can do is to create an entity called User with some details e.g

public class User implements Serializable {

    private String firstName;
    private String lastNumber;
    private String address;
    private String phoneNumber;

    //Generated getters and setters here....
}

then add it to a map like so....

Map<String, User> userMap = new HashMap<String, User>();
User user = new User();
//populate user
userMap.put(uniqueUserID, user);
The Elite Gentleman
A: 

What is your requirement, so that you would be answered specifically? You might get a good solution here which you might never had thought of.

Konnect
I am having list of values:number,name,address,phoneno.
in my dropdown displaying number only, if change the number in dropdown below fields are should change.
You need Map<Int, ObjectContaining(Name, Address, PhoneNo)> (the latter three you may model as Strings if you wish).
Synesso
A: 

As others have said, a map is a mapping between a key and a value. That is why the javadoc for map says (since generics in v5):

Interface Map<K,V>

... where K is the type of the key, and V is the type of the value. Just <K, V>. You must define exactly two types.

It is worth mentioning that in mathematics 'map' is a term meaning to convert a value of one type into a value of another type. The Java Map is a reflection of this, (as is the method map available on collections in Scala).

Synesso
"It is worth mentioning that in mathematics 'map' is a term meaning to convert a value of one type into a value of another type."Not sure what "type" means in this context, but still I would say: No, "mathematical" maps are not necessarily what you say they are (think of endomorphisms/endofunctions).
Novox
Specifically, set theory. Where 'type' = a set. And 'map' in the context of a function that maps A -> B. That is a similar effect to what Java's strongly typed Maps provide.
Synesso
A: 

Your question suggests you want key on each of the 4 fields and all others become values. While in general, it is one key and multiple values packed on a object.

Please confirm what is required.

Ashish Patil
A: 

The 'correct' solution is to use an object that holds the values in named fields, but in the spirit of answering the question asked, a simple (if unclean) solution would be to use:

Map<String,List<String>> yourMap = new HashMap<String,List<String>>();

List<String> info = new ArrayList<String>();
info.add(number);
info.add(name);
info.add(address);
info.add(phone);

yourMap.put(key, info);

Note google-collections has a series of classes that implement this structure right out of the box called ListMultimap and it's implementation ArrayListMultimap

Gareth Davis
I would refrain from providing sample code for a bad solution. I see the reasoning for wanting to explain that, but sample code makes much too likely that it will simply be copied without learning why it's not a good idea.
Joachim Sauer
all in the service of giving people enough information to blow their own foot off. 'Yes sir, if you point that at your foot and pull the little thing in front of the handle it will go bang'
Gareth Davis
A: 

Hmmm, You could create a class for Person with number, name, address and phoneno, then you create a Map

rslima
A: 

Just in case you want to maintain a map of: "number, name" --> "address, phone" and you do not wish to create a class to encapsulate these attributes. You may have a look in the handy MultiKey in Apache Commons Collections:

http://commons.apache.org/collections/apidocs/org/apache/commons/collections/keyvalue/MultiKey.html

ThiamTeck