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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始


package com.hui_words;

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

public class Demo2 {
        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);
                                hashMap.put(string, ++integer);
                        }else {
                               
                                hashMap.put(string, 1);
                        }
                }
                System.out.println(hashMap);
                // 把出现次数相同的单词总结在一个集合里面,次数当做键,单词词组当做值
                Set<Entry<String,Integer>> entrySet = hashMap.entrySet();
                for (Entry<String, Integer> entry : entrySet) {
                       
                }
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马