当我使用 HashMap 时,输出总是像这样 {a,2},而不是我想要像 (a,2) 这样的输出。 我如何删除大括号或将其修改为“)”。
wordCount.put("("+'"'+splitted[i].toLowerCase()+'"'+")",
wordCount.getOrDefault(splitted[i], 0) + 1);
输出是
{("the")=1, ("be")=1, ("like")=1}
,但我想要这样的输出,
( “the” , 132 )
谢谢。
最佳答案
如果您想要自定义表示,而不是使用内置的 toString
方法,您应该实现自己的方法。
例如(使用 Java 8):
String toFormattedString(Map<String,Integer> map) {
return map.entrySet().stream()
.map(e -> String.format("(\"%s\", %d)", e.getKey(), e.getValue()))
.collect(Collectors.joining(", "));
}
通过这种方式,您可以按照您想要的方式制作格式化输出,按您想要的顺序使用键等。
关于java - 如何在 HashMap java 中删除 "{",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35958871/