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.
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.
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.
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));
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);
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.
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).
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.
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
Hmmm, You could create a class for Person with number, name, address and phoneno, then you create a Map
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