Java ConcurrentMap实现英文词频统计:以hamlet为例

本文展示了如何使用Java并发Map在《哈姆雷特》文本中统计单词频率,通过线程池并行处理,优化了计数效率。重点在于并发数据结构的使用和字符串处理技术。

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

import java.io.Reader;
import java.io.FileReader;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.charset.*;
import java.util.List;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.concurrent.*;

class MyCounter implements Runnable {
	private ConcurrentMap<String, Integer> map = null;
	private String line = null;

	public MyCounter(String line, ConcurrentMap<String, Integer> map) {
		this.line = line;
		this.map = map;
	}

	@Override
	public void run() {
		for (String word : line.split(" ")) {
			// System.out.println(word);
			increase(word);
		}
	}

	public int increase(String word) {
		Integer oldValue;
		Integer newValue;
		while (true) {
			oldValue = map.get(word);
			if (oldValue == null) {
				newValue = 1;
				if (map.putIfAbsent(word, newValue) == null)
					break;
			} else {
				newValue = oldValue + 1;
				if (map.replace(word, oldValue, newValue))
					break;
			}
		}
		return newValue;
	}
}

public class ConcurrentTest {
	private static final ConcurrentMap<String, Integer> map = new ConcurrentHashMap<String, Integer>();

	public static void main(String[] args) throws IOException {
		Path p = Paths.get("F:\\hamlet.txt");
		List<String> lines = Files.readAllLines(p, StandardCharsets.UTF_8);
		ExecutorService service = Executors.newFixedThreadPool(10);
		for (String line : lines) {
			// new Thread(new MyCounter(line,map)).start();
			line = line.toLowerCase();
			// Split str around matches of empty string ""
			String[] substrings = "!'\"#$%&()*+,-./:;<=>?@[\\\\]^_`{|}~".split("");

			for (String ch : substrings) {
				line = line.replace(ch, " ");
			}

			service.execute(new MyCounter(line, map));
		}
		service.shutdown();
		while (!service.isTerminated())
			;
//		Iterator i = map.keySet().iterator();
//
//		while (i.hasNext()) {
//			String word = (String) i.next();
//			int count = map.get(word);
//			System.out.println(word + " " + count);
//		}

		List<String> values = new ArrayList<>(map.keySet());
		Collections.sort(values, new Comparator<String>() {
			public int compare(String a, String b) {
				// no need to worry about nulls as we know a and b are both in pl
				return map.get(b) - map.get(a);
			}
		});

		for (String val : values) {
			System.out.println(val + "," + map.get(val));
		}
	}
}

,4138
the,1143
and,966
to,762
of,669
i,631
you,554
a,546
my,514
hamlet,471
in,451

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值