A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始


package com.hui_words;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class Demo1 {
        public static void main(String[] args) {
                //原字符串
                String s = "China and Canada on Monday pledged to strengthen security cooperation at a high-level dialogue held in Beijing.It was the first

China-Canada High-Level National Security and Rule of Law Dialogue, a mechanism established during Canadian Prime minister Justin Trudeau's visit to China from

late August to early September.According to a joint statement released after the meeting, the two sides exchanged views on fighting terrorism, cyber crime,

organized crime, consular affairs and other issues.The two sides have confirmed their future cooperation framework. Some common goals are expected to be reached

under the framework, such as initiating a discussion on an extradition treaty and finalizing an agreement on sharing and returning recovered assets.The dialogue

was co-chaired by Wang Yongqing, secretary-general of the Commission for Political and Legal Affairs of the Communist Party of China (CPC) Central Committee,

and Daniel Jean, National Security Advisor to the Canadian Prime Minister.After the dialogue, Meng Jianzhu, head of the Commission for Political and Legal

Affairs of the CPC Central Committee, met with Jean. Meng said he hopes cooperation in security and legal affairs, which has become an integral part of the

China-Canada relationship, will become a new bright spot in the development of bilateral ties. ";
                System.out.println(s);
                //替换之后的字符串
                String s_new = s.replaceAll("[,.()-]"," ");
                System.out.println(s_new);
                //使用字符串的方法,分割字符串
                String[] split = s_new.split(" +");
                for (String string : split) {
                        System.out.println(string);
                }
                //把单词当做键,次数当做值存储在hashMap集合里面
                HashMap<String,Integer> hashMap = new HashMap<>();
                for (String string : split) {
                        if(hashMap.containsKey(string)){
                                Integer integer = hashMap.get(string);
                                integer++;
                                hashMap.put(string, integer);
                        }else {
                                hashMap.put(string, 1);
                        }
                }
                System.out.println(hashMap);
                //把出现次数相同的单词总结在一个集合里面,次数当做键,单词词组当做值
                HashMap<Integer,String> hashMap2 = new HashMap<>();
                Set<Entry<String,Integer>> entrySet = hashMap.entrySet();
                for (Entry<String, Integer> entry : entrySet) {
                        String key = entry.getKey();
                        Integer value = entry.getValue();
                       
                        if (hashMap2.containsKey(value)) {
                                String string = hashMap2.get(value);
                                String new_value = string +"  "+ key;
                                hashMap2.put(value, new_value);
                        }else {
                                hashMap2.put(value, key);
                        }
                }
                System.out.println(hashMap2);
                Set<Entry<Integer,String>> entrySet2 = hashMap2.entrySet();
                ArrayList<Integer> list = new ArrayList<>();
                for (Entry<Integer, String> entry : entrySet2) {
                        Integer key = entry.getKey();
                        String value = entry.getValue();
                        list.add(key);
                        System.out.println(key + "-->"+value);
                }
                System.out.println(list);
                Collections.sort(list);
                Collections.reverse(list);
                System.out.println(list);
                send_words(list,hashMap2);

               
               
               

        }

        private static void send_words(ArrayList<Integer> list, HashMap<Integer, String> hashMap2) {
                // TODO Auto-generated method stub
                System.out.println("请输入你要查找的次数最多的几个单词");
                for (int i = 0; i < 9; i++) {
                        Integer key = list.get(i);
                        String string = hashMap2.get(key);
                        System.out.println("单词的个数第"+key+"的单词是"+string);
                       
                       
                       
                }
        }
}

1 个回复

倒序浏览
666666牛掰!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马