【发布时间】:2015-07-24 04:55:37
【问题描述】:
我只想显示在一个字符串中出现多次的单词,不应该打印单个出现的字符串。我还想打印长度大于 2 的字符串(以消除 is,was,the 等)..
我尝试的代码..打印所有字符串并显示是出现次数..
代码:
public static void main(String args[])
{
Map<String, Integer> wordcheck = new TreeMap<String, Integer>();
String string1="world world is new world of kingdom of palace of kings palace";
String string2[]=string1.split(" ");
for (int i=0; i<string2.length; i++)
{
String string=string2[i];
wordcheck.put(string,(wordcheck.get(string) == null?1: (wordcheck.get(string)+1)));
}
System.out.println(wordcheck);
}
输出:
{is=1, kingdom=1, kings=1, new=1, of=3, palace=2, world=3}
不应打印单一外观的字符串... 我也想打印长度大于 2 的字符串(以消除 is,was,the 等)..
【问题讨论】:
-
TreeMap的toString方法是这样设计的。最好编写自己的方法来打印 TreeMap。
标签: java arrays string treemap