publicclass Sorting2 {
/**
* 1、分割字符串(按照空格来切割) 2、分拣存储 3、按要求查看 单词出现的次数 4、加入面向对象
*/publicstaticvoidmain(String[] args) {
// 1、分割字符串(按照空格来切割)
String str = "this is a cat and that is a mice and where is the food";
String[] arr = str.split(" ");
// 2、分拣存储
Map<String, Letter> map = new HashMap<String, Letter>();
for (String key : arr) {
if (!map.containsKey(key)) {
map.put(key, new Letter(key));
}
Letter value = map.get(key);
value.setCount(value.getCount() + 1);
//另一种存储方式// Letter value = map.get(key);// if (value == null) {// value = new Letter();// map.put(key, value);// }// value.setCount(value.getCount() + 1);
}
// 3、按要求查看 单词出现的次数for (String key : map.keySet()) {
Letter value = map.get(key);
System.out.println(key + "-->" + value.getCount());
}
}
}