java统计字符串单词的个数

本文介绍了一个简单的英文单词统计器的实现方法。通过替换特定字符并使用哈希映射记录每个单词出现的次数,该统计器能够高效地统计给定文本中各单词的频率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在一些项目中可能需要对一段字符串中的单词进行统计,我在这里写了一个简单的demo,有需要的同学可以拿去看一下。

本人没怎么写个播客,如果有啥说的不对的地方,你来打我啊大笑大笑

不说废话了直接贴代码:

实现代码:

/**
	 * 统计各个单词出现的次数
	 * @param text
	 */
	public static void findEnglishNum(String text){
		//找出所有的单词
		String[] array = {".", " ", "?", "!"};
		for (int i = 0; i < array.length; i++) {
			text = text.replace(array[i],",");
		}
		String[] textArray = text.split(",");
		
		//遍历 记录
		Map<String, Integer> map = new HashMap<String, Integer>();
		for (int i = 0; i < textArray.length; i++) {
			String key = textArray[i];
			//转为小写
			String key_l = key.toLowerCase();
			if(!"".equals(key_l)){
				Integer num = map.get(key_l);
				if(num == null || num == 0){
					map.put(key_l, 1);
				}else if(num > 0){
					map.put(key_l, num+1);
				}
			}
		}
		//输出到控制台
		System.out.println("各个单词出现的频率为:");
		Iterator<String> iter = map.keySet().iterator();
		while(iter.hasNext()){
			String key = iter.next();
			Integer num = map.get(key);
			System.out.println(key + "\n\t\t" + num + "次\n-------------------");
		}
	}


测试代码:

public static void main(String[] args) {
		String text = "Welcome welcome to ADempiere, a commons-based peer-production of Open Source ERP Applications. This Wiki is for the global community to contribute and share know-how and domain expertise. We hope you can find as much open information and participate in making it most usable for everyone. This project has a bazaar of Citizens with a Community Council Team which work in theFunctional Team and Technical Team along the Software Development Procedure supported and funded by the foundation ADempiere";
		
		findEnglishNum(text);
	}

运行结果:

后面还有一些没有全部截下来


本人代码功力不够深厚,如果代码中有什么不对或不好的地方,欢迎各位大神指点!可怜

You and your team have worked tirelessly until you have a sequence a1,a2,…,a2n+1 of positive integers satisfying these properties. 1≤ai≤1018 for all 1≤i≤2n+1 . a1,a2,…,a2n+1 are pairwise distinct. a1=a2&minus;a3+a4&minus;a5+…+a2n&minus;a2n+1 . However, the people you worked with sabotaged you because they wanted to publish this sequence first. They deleted one number from this sequence and shuffled the rest, leaving you with a sequence b1,b2,…,b2n . You have forgotten the sequence a and want to find a way to recover it. If there are many possible sequences, you can output any of them. It can be proven under the constraints of the problem that at least one sequence a exists.    Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104 ). The description of the test cases follows. The first line of each test case contains one integer n (1≤n≤2⋅105 ). The second line of each test case contains 2n distinct integers b1,b2,…,b2n (1≤bi≤109 ), denoting the sequence b . It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .    Output For each test case, output 2n+1 distinct integers, denoting the sequence a (1≤ai≤1018 ). If there are multiple possible sequences, you can output any of them. The sequence a should satisfy the given conditions, and it should be possible to obtain b after deleting one element from a and shuffling the remaining elements. Example InputCopy 4 1 9 2 2 8 6 1 4 3 99 2 86 33 14 77 2 1 6 3 2 OutputCopy 7 9 2 1 8 4 6 9 86 99 2 77 69 14 33 4 6 1 2 3
最新发布
03-11
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值