我正在寻找一种方法来存储包含任意数量的Sting-Double键值对的六个字符串的列表。例如,六个字符串列表将是:
["乐""愤怒""悲伤""惊喜""厌恶",& #34;恐惧&#34]。
列表项目" Happy"然后会有一对配对列表:
<" Joy",1.00>,<" love",0.98>等。
非常感谢任何帮助。我试过Map>但似乎无法做到正确。
这是我的详尽清单
MutualInformation::findRelatedWords - scanning for similarwords....
anger, angry
anger, irrate
anger, stupid
anger, annoy
anger, frustrate
disgust, disgust
disgust, dislike
disgust, hate
disgust, sick
disgust, ill
fear, fear
fear, afraid
fear, frighten
fear, scare
fear, terrify
joy, happiness
joy, glad
joy, joy
joy, good
joy, love
joy, like
sadness, sad
sadness, hurt
sadness, sorrow
sadness, cry
sadness, bad
surprise, surprise
surprise, amazing
surprise, astonish
surprise, incredible
surprise, wonder
这是Mapped对象的输出。如您所见,它只保存最后一项。
[{anger = [frustrate],disgust = [ill],fear = [terrify],joy = [like],sadness = [bad],surprise = [wonder]}]
部分代码如下:
Map<String, List<String>> relatedWords = new TreeMap<String, List<String>>();
while (rs.next())
{
// assign the results to variables, standardising case as we go
String rs_emotion = rs.getString("emotion").toLowerCase();
String rs_word = rs.getString("word").toLowerCase();
System.out.println("\t\t" + rs_emotion + ", " + rs_word);
relatedWords.put(rs_emotion, Collections.singletonList(rs_word));
}
答案 0 :(得分:0)
您正在寻找的键值对可以使用HashMap实现。如下:
Map<String, Double> map = new HashMap<>();
map.put("joy", 1.00);
map.put("love", 0.98);
然后可以将这些地图存储在另一个地图中,如下所示:
Map<String, Map<String, Double>> maps = new HashMap<>();
maps.put("Happy", happyMap);
迭代生成的happyMap:
for (Object o : happyMap.entrySet()) {
Map.Entry pair = (Map.Entry) o;
System.out.println(pair.getKey() + " " + pair.getValue());
}
答案 1 :(得分:0)
制作一个以字符串作为键并且配对为值的地图:
Map<String id, Pair>
或者,这就是我要做的,制作2张地图:
1. Map<String,String>
2. Map<String,Dobule>
获取该对,您只需在两个地图上访问键字符串“Happiness”的值。
答案 2 :(得分:0)
这是我的代码示例。这是一张包含地图的地图。
Map<String, Map<String, Integer>> mapOfMap = new HashMap<>();
Map<String, Integer> map1 = new HashMap<>();
mapOfMap.put("fear", map1);
map1.put("annoy", 1);
map1.put("frustrate", 2);
map1.put("stupid", 3);
Map<String, Integer> map2 = new HashMap<>();
mapOfMap.put("surprise", map2);
map2.put("surprise", 4);
map2.put("amazing", 5);
System.out.println(mapOfMap.get("fear").get("stupid")); // prints 3