java一个单词在句子中出现的次数_Java练习题_Map 统计一段文章中每个(或指定)单词出现的次数,...

统计一段文章中每个单词出现的次数,

this is a book this is an elephont

也可以只统计指定单词

1 importjava.util.Collection;

2 importjava.util.HashMap;

3 importjava.util.Map;

4 importjava.util.Map.Entry;

5

6 public classTestDay19_2 {

7

8 public static voidmain(String[] args) {

9

10 String s = "As a student, we have study for many years. " + "During there years, we study Chinese, English, "

11 + "science and so on. In this period, with the improvement of our knowledge,"

12 + "we not only read the books in class but also read extracurricular after school. "

13 + "In my opinion Dream of the red chamber is the best book I have read. "

14 + "In this book, the great author Cao xueqin his view about love. "

15 + "He had firmly faith in what is the true love and try to tell us "

16 + "that there are different kinds of love, "

17 + "only one kind which should be considered as True Love. In a dream, "

18 + "and under mythical circumstances, the main character of the novel, "

19 + "Jia baoyu, met the Fairy Disenchantment in the Land of Illusions. "

20 + "She showed him three registers each containing the names "

21 + "and the happenings in life of 12 girls in his clan. "

22 + "Each girl represents a kind of love. From the stories " + "which are unfolding in the novel, "

23 + "the reader should know the characteristics of the different kinds of love, "

24 + "and should be able to distinguish True Love from the other kinds. "

25 + "True love is acceptance, committal, mutual and without any post conditions. "

26 + "The love between Lin daiyu and Jia baoyu is considered to be True Love. "

27 + "Just saying or hearing I love you is not good enough because talk is cheap. "

28 + "Action in mutual commitment is essential, as we can see in the novel. "

29 + "We always have dreams. What‘s ours dream? Do you know what you exactly want? "

30 + "And do you work hard for it. In this book, "

31 + "you can taste the feeling of the author about his strong will. In his words, "

32 + "you can find the charm of Chinese. It is not a book, but a precious deposits. "

33 + "You can get a lot from it. But unfortunately, "

34 + "the novel was never completed to such a state. On the other hand, maybe, "

35 + "it is another kind of beauty of the book. If you have not read this book yet, "

36 + "just go and read it. You will love it. ";

37 String[] ss = s.split(" ");

38

39 HashMap hm = new HashMap<>();

40 // 指定想要统计的单词

41 hm.put("this", 0);

42 hm.put("is", 0);

43 hm.put("a", 0);

44 hm.put("book", 0);

45 hm.put("an", 0);

46

47 for (int i = 0; i < ss.length; i++) {

48 String word =ss[i];

49 if(hm.containsKey(word)) {

50 hm.put(word, hm.get(word) + 1);

51 }

52 // 把下面这三行放开就是统计每个单词的次数

53 // else{

54 // hm.put(ss[i], 1);

55 // }

56 }

57

58 Collection> c =hm.entrySet();

59 for (Entryentry : c) {

60 System.out.println(entry.getKey() + "出现了" + entry.getValue() + "次");

61 }

62 }

63 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值