嗨我有一个MAP,它在计算元素后给出所有记录。在这个记录集中,MAP的最后一个元素是我计算记录的最终输出..但是如何获取它并将其分配给String(将其用于以后的目的)?
Map<String, Integer> stringsWithCount = new TreeMap<String,Integer>();
for (Message msg : convInfo.messages) {
// where ever your input comes from: turn it into lower case,
// so that "ccc" and "CCC" go for the same counter
String item = msg.userName;
if (stringsWithCount.containsKey(item)) {
stringsWithCount.put(item, stringsWithCount.get(item) + 1);
} else {
stringsWithCount.put(item, 1);
}
}
我正在浏览地图,如下所示
for (Entry<String, Integer> entry : stringsWithCount.entrySet()) {
System.out.println(entry.getKey()+"("+entry.getValue()+")");
}
现在正在打印
G_LO(1)
FCHAN95(1)
G_LO(1)
FCHAN95(2)
G_LO(1)
FCHAN95(2)
G_LO(1)
WVU(1)
FCHAN95(2)
G_LO(1)
SWONG00(1)
WVU(1)
FCHAN95(3)
G_LO(1)
SWONG00(1)
WVU(1)
FCHAN95(4)
G_LO(1)
SWONG00(1)
WVU(1)
FCHAN95(5)
G_LO(1)
SWONG00(1)
WVU(1)
FCHAN95(6)
G_LO(1)
SWONG00(1)
WVU(1)
此处从我想访问的上一个唯一记录开始 - &gt; FCHAN95(6)G_LO(1)SWONG00(1)WVU(1)
答案 0 :(得分:2)
由于treemap内置了如下方法
String lastKey=((TreeMap<String, Integer>) stringsWithCount).lastKey(); // for last key
Integer lastRecord =((TreeMap<String, Integer>) stringsWithCount).get(lastKey) // for last record
// stringsWithCount是你的地图变量