目前,我需要添加整个地图,如最后一行所示。
PriorityQueue<Map.Entry<String, Integer>> sortedCells = new PriorityQueue<Map.Entry<String, Integer>>(3, new mem());
Map<String,Integer> pn = new HashMap<String,Integer>();
pn.put("hello", 1);
pn.put("bye", 3);
pn.put("goodbye", 8);
sortedCells.addAll(pn.entrySet());
如果我只想添加这个怎么办?
("word" 5)
如果我这样做
sortedCells.add("word",5)
我遇到了参数错误。
如何添加单个元素?
SimpleEntry
代替你自己的实现(我在写原始问题后找到了它)。SimpleEntry
实现了toString()
并且你将获得相同的结果。或者在你的MyEntry
中实现toString()
。如果这是一个重复出现的用例,并且你想要拥有自己的结构,我会使用MyEntry
(具有不同的描述性名称)。如果像你所提供的那样简单,我会使用SimpleEntry
。 - Avi