当我使用HashMap时,输出总是像{a,2},而不是我想要输出像(a,2).如何删除花括号或将其修改为")"这个.
wordCount.put("("+'"'+splitted[i].toLowerCase()+'"'+")",
wordCount.getOrDefault(splitted[i], 0) + 1);
Run Code Online (Sandbox Code Playgroud)
而且输出是
{("the")=1, ("be")=1, ("like")=1}
Run Code Online (Sandbox Code Playgroud)
,但我想要这样的输出,
( “the” , 132 )
Run Code Online (Sandbox Code Playgroud)
谢谢.
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(", "));
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以按照您想要的顺序制作格式化的输出,按键所需的顺序等.
| 归档时间: |
|
| 查看次数: |
459 次 |
| 最近记录: |