How to store more than one string in a Map?

2019-01-01 07:11发布

问题:

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.

回答1:

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.



回答2:

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 final String name;
   private final String address;
   private final String phone;

   // constructor and getters
}

map.put(number, new PersonalInfo(name, address, phone));


回答3:

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



回答4:

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);


回答5:

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



回答6:

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



回答7:

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).



回答8:

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.



标签: java map