Java 如何在 Map 中存储多个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3725703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store more than one string in a Map?
提问by user441978
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.
号码、姓名、地址、电话 - 所有这些都聚集在一起以显示值。每个值都与其他值相关联。
回答by Joachim Sauer
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.
你在拒绝对象。您应该使用一个包含号码、姓名、地址、电话(也许您可以称之为ContactInformation
)的对象并将其放入地图中。
回答by Bozho
No. a Map
has only one key. If you want your valueto contain more information, wrap the strings in a new class:
不,aMap
只有一把钥匙。如果您希望您的值包含更多信息,请将字符串包装在一个新类中:
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));
回答by Buhake Sindi
Nope, A Map can have only one key and mapped to one value.
不,一个 Map 只能有一个键并映射到一个值。
This is the Javadoc for Map:
这是 Map 的 Javadoc:
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
您可以做的是创建一个名为的实体User
,其中包含一些详细信息,例如
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);
回答by Synesso
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):
正如其他人所说,映射是键和值之间的映射。这就是为什么 map 的 javadoc 说(因为 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.
... 其中 K 是键的类型,V 是值的类型。只是 <K, V>。您必须准确定义两种类型。
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).
值得一提的是,在数学中,'map' 是一个术语,意思是将一种类型的值转换为另一种类型的值。Java Map 反映了这一点(就像 Scala 集合上可用的方法映射一样)。
回答by Ashish Patil
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.
您的问题表明您希望在 4 个字段中的每一个上都有键,而所有其他字段都将成为值。而一般来说,它是一个键和多个值打包在一个对象上。
Please confirm what is required.
请确认需要什么。
回答by Gareth Davis
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-collectionshas a series of classes that implement this structure right out of the box called ListMultimapand it's implementation ArrayListMultimap
请注意,google-collections有一系列开箱即用地实现此结构的类,称为ListMultimap及其实现ArrayListMultimap
回答by Ricardo Lima
Hmmm, You could create a class for Person with number, name, address and phoneno, then you create a Map
嗯,您可以为 Person 创建一个包含号码、姓名、地址和电话号码的类,然后创建一个 Map
回答by ThiamTeck
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:
以防万一您想维护一个映射:“号码,姓名”-->“地址,电话”并且您不希望创建一个类来封装这些属性。您可以在 Apache Commons Collections 中查看方便的 MultiKey:
http://commons.apache.org/collections/apidocs/org/apache/commons/collections/keyvalue/MultiKey.html
http://commons.apache.org/collections/apidocs/org/apache/commons/collections/keyvalue/MultiKey.html