[英]Multimap guava values
I have this scenario: 我有这种情况:
ID={1,2,3}
Title ={T1,T2,T3}
Place ={P1,P2,P3}
I want to have this result: 我想要这样的结果:
ID: 1 , Title: T1, Place: P1
ID: 2 , Title: T2, Place: P2
ID: 3 , Title: T3, Place: P3
I used Multimap (Guava) to store the ID and title with their multiple values: 我使用Multimap(Guava)来存储ID和标题及其多个值:
Multimap<String, String> MultiMap = ArrayListMultimap.create();
However, I couldn't so far get the desired result as described above. 但是,到目前为止,我无法获得如上所述的预期结果。 I used this code as a try:
我使用此代码作为尝试:
for (Entry<String, String> key : MultiMap.entries())
{
System.out.println("name : " + key.getKey() + " value : " + key.getValue());
}
However, using this code the result I got is like this: 但是,使用此代码,我得到的结果是这样的:
name : ID value: 1
name : ID value: 2
name : ID value: 3
name : Title value: T1
name : Title value: T2
name : Title value: T3
name : Place value: P1
name : Place value: P2
name : Place value: P3
How to get the result as I described above using Multimap? 如何使用Multimap获得如上所述的结果?
I don't think you understand correctly what a Multimap is. 我认为您无法正确理解什么是Multimap。 A Multimap maps from one or more keys to multiple values, it's an n-to-m relation.
多重地图从一个或多个键映射到多个值,这是n对m的关系。 What you seem to have is a relation from an ID to a Title, which is an n-to-1 relation.
您似乎拥有的是从ID到标题的关系,这是n对1的关系。 The right data structure for this is a Map, not a Multimap.
正确的数据结构是Map,而不是Multimap。
Map<Integer, String> titlesById = new TreeMap<>(); // keep map ordered by ID
titlesById.put(1, "T1");
titlesById.put(2, "T2");
titlesById.put(3, "T3");
for(Map.Entry<Integer, String> entry : titlesById.entrySet()){
System.out.println("ID : " + entry.getKey() + " Title : " + entry.getValue());
}
This should produce the desired output. 这将产生所需的输出。
Note: if you are likely to look up titles by ID from the Map, then you probably want to switch from TreeMap to HashMap, as HashMap has constant time lookup. 注意:如果您有可能通过Map中的ID查找标题,则您可能希望从TreeMap切换到HashMap,因为HashMap具有固定的时间查找功能。
Update: apparently you do need a Multimap. 更新:显然您确实需要一个Multimap。 Then change the above code to something like this:
然后将上面的代码更改为如下所示:
Multimap<Integer, String> titlesById = TreeMultimap.create<>(); // keep map ordered by ID
titlesById.putAll(1, Arrays.asList("T1a", "T1b", "T1c"));
titlesById.putAll(2, Arrays.asList("T2a", "T2b"));
titlesById.put(3, "T3");
for(Map.Entry<Integer, String> entry : titlesById.entries()){
System.out.println("ID : " + entry.getKey() + " Title : " + entry.getValue());
}
Again: TreeMultimap
sorts the Map by the key, HashMultimap
has efficient lookup. 再次:
TreeMultimap
通过键对Map进行排序, HashMultimap
具有高效的查找功能。
It gets more and more interesting, now you want to have 3 values connected to each other. 它变得越来越有趣,现在您希望将3个值相互连接。 In that case, you should use a
Table
(3-dimensional Map). 在这种情况下,您应该使用
Table
(3维地图)。 Eg 例如
Table<Integer, String, String> titlesAndPlacesById = TreeBasedTable.create();
titlesAndPlacesById.put(1, "T1", "P1");
titlesAndPlacesById.put(2, "T2", "P2");
titlesAndPlacesById.put(3, "T3", "P3");
for(Table.Cell<Integer, String, String> cell : titlesAndPlacesById.cellSet()){
System.out.println("ID : " + cell.getColumnKey() + " Title : " + cell.getRowKey() + ", Place: " + cell.getValue());
}
But this is not a perfect match. 但这不是完美的匹配。 If you want to map an ID to a title and place, then you probably want to have a custom object encapsulating title and place, and use that as the value in a plain old Map from integer to custom type.
如果要将ID映射到标题和位置,则可能需要一个封装标题和位置的自定义对象,并将其用作从整数到自定义类型的普通旧Map中的值。
public class TitleAndPlace{
private final String title;
private final String place;
TitleAndPlace(String title, String place) {
this.title = title;
this.place = place;
}
public String getTitle() { return title; }
public String getPlace() { return place; }
@Override public boolean equals(Object o) {
if (this == o) return true;
else if (o instanceof TitleAndPlace) {
TitleAndPlace that = (TitleAndPlace) o;
return Objects.equals(title, that.title)
&& Objects.equals(place, that.place);
} else return false;
}
@Override public int hashCode() {
return Objects.hash(title, place);
}
}
Map<Integer, TitleAndPlace> map = new TreeMap<>();
map.put(1, new TitleAndPlace("T1", "P1"));
map.put(2, new TitleAndPlace("T2", "P2"));
for(Map.Entry<Integer, TitleAndPlace> entry : map.entrySet()){
System.out.println("ID : " + entry.getKey() + " Title : " + entry.getValue().getTitle() + ", Place: " + entry.getValue().getPlace());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.