-
2021-03-06 14:52:25
我正在尝试在Java 8中实现单词计数程序,但无法使其工作。该方法必须将字符串作为参数并返回Map。
当我以旧的Java方式进行操作时,一切正常。但是,当我尝试在Java 8中执行此操作时,它将返回一个映射,其中键为空且具有正确的出现次数。
这是我的Java 8风格的代码:
public Map countJava8(String input){
return Pattern.compile("(\\w+)").splitAsStream(input).collect(Collectors.groupingBy(e -> e.toLowerCase(), Collectors.reducing(0, e -> 1, Integer::sum)));
}
这是我在正常情况下会使用的代码:
public Map count(String input){
Map wordcount = new HashMap<>();
Pattern compile = Pattern.compile("(\\w+)");
Matcher matcher = compile.matcher(input);
while(matcher.find()){
String word = matcher.group().toLowerCase();
if(wordcount.containsKey(word)){
Integer count = wordcount.get(word);
wordcount.put(word, ++count);
} else {
wordcount.put(word.toLowerCase(), 1);
}
}
return wordcount;
}
主程序:
public static void main(String[] args) {
WordCount wordCount = new WordCount();
Map phrase = wordCount.countJava8("one fish two fish red fish blue fish");
Map count = wordCount.count("one fish two fish red fish blue fish");
System.out.println(phrase);
System.out.println();
System.out.println(count);
}
当我运行该程序时,输出如下:
{ =7, =1}
{red=1, blue=1, one=1, fish=4, two=1}
我认为该方法splitAsStream会将正则表达式中的匹配元素流式传输为Stream。我该如何纠正?
更多相关内容 -
java统计字数
2021-03-04 05:38:46/*** 获取文章的字数或则字符数* @author montao*/public class StatWordCount {private final char[] CHS = {',',';','.','!','?',';','+','。','?','!'}; //符号数组private final char[] CHN = {'\n','\r'}; /.../**
* 获取文章的字数或则字符数
* @author montao
*/
public class StatWordCount {
private final char[] CHS = {',',';','.','!','?',';','+','。','?','!'}; //符号数组
private final char[] CHN = {'\n','\r'}; //转义符数组
//private final char[] CHN = {'\n','\r',''}; //转义符数组
private final char[] SPACE = {' ',' '}; //空格的数组(前半角,后全角)
/**
* 根据指定条件来筛选文章的字数
* @param wordContent 文章内容
* @param compriseInterpunction 是否包含指定字符
* @param compriseSpace 是否包含空格
* @return 返回文章经过指定筛选后的长度
*/
public int getWordCount(String wordContent,boolean compriseInterpunction,boolean compriseSpace)
{
if(wordContent==null){
return 0;
}else if(wordContent.length()==0){
return 0;
}else{
//既要包含符号又要包含空格
if(compriseInterpunction && compriseSpace){
//清除转义符
String regex = "["+new String(CHN)+"]";
wordContent = wordContent.replaceAll(regex," ");
return this.getWordCount(wordContent);
}
//不包含符号包含空格
else if(!compriseInterpunction && compriseSpace){
//使用正则表达式去掉指定的符号和转义符
String regex1 = "["+new String(CHN)+"]";
String regex2 = "["+new String(CHS)+"]";
wordContent = wordContent.replaceAll(regex1," ");
wordContent = wordContent.replaceAll(regex2," ");
return this.getWordCount(wordContent);
}
//包含指定符号不包含空格
else if(compriseInterpunction && !compriseSpace){
//使用正则表达式去掉空格和转义符
String regex1 = "["+new String(CHN)+"]";
String regex2 = "["+new String(SPACE)+"]";
wordContent = wordContent.replaceAll(regex1," ");
wordContent = wordContent.replaceAll(regex2," ");
return this.getWordCount(wordContent);
}
//空格和指定符号都不包含
else{
//使用正则表达式去掉空格,指定符号和转义符
String regex1 = "["+new String(CHN)+"]";
String regex3 = "["+new String(CHS)+"]";
String regex2 = "["+new String(SPACE)+"]";
wordContent = wordContent.replaceAll(regex1," ");
wordContent = wordContent.replaceAll(regex2," ");
wordContent = wordContent.replaceAll(regex3," ");
return this.getWordCount(wordContent);
}
}
}
/**
* 返回文章中的字数
* @param wordCount 文章内容
* @return
*/
@SuppressWarnings("unused")
private int getWordCount(String wordContent){
int count = 0;
if(wordContent==null){ //判断是否为null,如果为null直接返回0
count = 0;
}else if(wordContent.length()==0){ //判断是否为空,如果为空直接返回0
count = 0;
}else{ //判断获取字数
wordContent = wordContent.trim(); //清空空格
//临时变量
String s4 = "";
String s3 = "";
String s1 = "";
boolean bb = false;
if(wordContent.length()>0){
s4 = String.valueOf(wordContent.charAt(wordContent.length()-1));
}
for(int i=0;is3 = String.valueOf(wordContent.charAt(i));
int num = s3.getBytes().length;
if(s3.hashCode()==32||s3.getBytes().length==2){
bb=true;
}if(num==2){
count++;
}else{
if(i+11)){
s1 = String.valueOf(wordContent.charAt(i+1));
if((s1.hashCode()==32||s1.getBytes().length==2)&&(s3.hashCode()!=32)){
count++;
}
}
}
}
if(!bb){
count++;
}else{
if(s4.getBytes().length==1){
count++;
}
}
}
return count;
}
/**
* 根据条件来获取文章的字符数
* @param wordContent 文章内容
* @param compriseInterpunction 是否包含指定符号
* @param compriseSpace 是否包含空格
* @return 返回字符长度
*/
public int getWordCharacter(String wordContent,boolean compriseInterpunction,boolean compriseSpace)
{
//既要包含符号又要包含空格
if(compriseInterpunction && compriseSpace){
//清除转义符
String regex = "["+new String(CHN)+"]";
wordContent = wordContent.replaceAll(regex," ");
//首部的空格不算
wordContent = wordContent.replaceAll("^\\s+","");
return wordContent.length();
}//不包含符号包含空格
else if(!compriseInterpunction && compriseSpace){
//首部的空格不算
wordContent = wordContent.replaceAll("^\\s+","");
//使用正则表达式去掉指定的符号和转义符
String regex1 = "["+new String(CHN)+"]";
String regex2 = "["+new String(CHS)+"]";
wordContent = wordContent.replaceAll(regex1," ");
wordContent = wordContent.replaceAll(regex2," ");
return wordContent.length();
}//包含指定符号不包含空格
else if(compriseInterpunction && !compriseSpace){
//使用正则表达式去掉空格和转义符
return this.getNoSpaceCount(wordContent);
}//空格和指定符号都不包含
else{
//使用正则表达式去掉指定符号
String regex1 = "["+new String(CHS)+"]";
wordContent = wordContent.replaceAll(regex1," ");
return this.getNoSpaceCount(wordContent);
}
}
/**
* 获取文章中非空格的字符总数
* @param wordContent 文章内容
* @return
*/
private int getNoSpaceCount(String wordContent) {
int spaceCount = 0;
if(wordContent==null)
{
spaceCount = 0;
}else if(wordContent.length()==0)
{
spaceCount = 0;
}else
{
//替换首部的
wordContent = wordContent.replaceAll("^\\s+","");
wordContent = wordContent.replaceAll(" ","");
//使用正则替换转义符
String regex = "["+new String(CHN)+"]";
wordContent = wordContent.replaceAll(regex,"");
spaceCount = wordContent.length();
}
return spaceCount;
}
}
阅读(1947) | 评论(0) | 转发(0) |
-
java统计字数问题求教
2021-03-05 13:19:49展开全部importjava.util.Scanner;publicclasscountWords{publicstaticvoidmain(Stringargs[]){Stringnumber[]=newString[20];Stringtemp="N";//storeinput,ifit'snotcharacter,break;elsesto...展开全部
import java.util.Scanner;
public class countWords {
public static void main(String args[]) {
String number[] = new String[20];
String temp = "N"; // store input, if it's not character, break; else store
int i = 0;
Scanner sc = new Scanner(System.in);
System.out.println("please input your characters:\n");
while(true){
temp = sc.nextLine();
if(temp.matches("[0-9a-zA-Z]")){
number[i] = temp;
i++;
}
else break;
}
System.out.println("the count of character is "+i);
}
}
结果截图:
合法输入是单个的字母或者数字,如果有其他需求,另e5a48de588b63231313335323631343130323136353331333332623265加限定
-
java统计汉字字数的方法示例
2020-08-30 15:15:30主要介绍了java统计汉字字数的方法,结合实例形式分析了java正则判定、字符串遍历及统计相关操作技巧,需要的朋友可以参考下 -
java txt文件字数统计代码
2012-09-22 22:27:27用java写的小程序,用于统计txt文件中的字符数和中文字数,虽简单但是使用 -
Java学习笔记:案例统计字数
2022-01-25 13:47:542.程序扫描文件夹下所有的txt文件,统计总的“英文单词个数” 1.打开一个文件选择对话框 2.String[] files=获得这个文件夹下所有的TXT文件; 3.int sum=0;//总单词个数 3循环遍历files { String file=……; ...需求
1.让用户选择一个文件夹
2.程序扫描文件夹下所有的txt文件,统计总的“英文单词个数”
1.打开一个文件选择对话框
2.String[] files=获得这个文件夹下所有的TXT文件;
3.int sum=0;//总单词个数
3循环遍历files
{
String file=……;
String s=读取file文件的内容;
int count=……;//算file文件中单词的个数
sum=sum+count;
}
hello i am chenpanyu,how are you?
package part2; import com.yzk18.GUI.GUI; import com.yzk18.commons.IOHelpers; import java.util.Arrays; public class 计算单词总个数 { public static void main(String[] args) { /* String s="hello i am chenpanyu,how are you?"; String[] strs=s.split("\\s|\\.|\\?|\\!|,");//正则表达式\\转义|或者 System.out.println(Arrays.toString(strs));*/ String dir= GUI.dirOpenBox("选择一个包含英文txt文件的文件夹"); String[] files= IOHelpers.getFilesRecursively(dir,"txt"); int sum=0; for (String file:files) { String s=IOHelpers.readAllText(file); String[] strs=s.split("\\s|\\.|\\?|\\!|,");//分割出来的单词 sum=sum+strs.length;//数组strs.length=单词的个数 } System.out.println(sum); } }
-
Java实现字数统计(中文英文韩文日文混合),类似word效果
2021-01-08 10:44:04因为业务需求,需要一个字数统计的功能,其实这个功能也不难,但是因为这边涉及到中文,英文,日文和韩文的混合文本,所以不能用一般的统计方法,在网上搜索了一些,然后综合了一下自己写了个支持混合统计的工具类 ... -
Java简单统计文本字数方法
2016-11-23 01:04:47Java简单统计文本字数方法 -
java 字数统计
2021-03-07 09:13:17Java> appeared 4 times in the paragraph.package cn.CodeSnippet;import org.apache.commons.lang3.StringUtils;public class WordCounter {public static void main(String[] args) {// String wi... -
JAVA统计word字数
2020-01-02 14:43:51import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; ...import java.io.FileInputStre... -
Java 统计文本文件中字符数量
2021-11-17 19:47:53设有一个文本文件word01.txt,内容如下: after a minute or two and said to his friend he ...编写程序,统计文件中每个字母出现的次数。 输出示例如下 { =27, a=13, c=1, d=11, e=14, f=3, g=2, h=6, i=10, l=1, m= -
word怎么看不算标点的字数(word统计字数去掉符号)
2021-03-09 01:45:54简介:众所周知,Word具有自动统计字数和字数的功能。如何统计Word文档中的字数和字符数?下面我给大家简单介绍一下工具:Word20131.单击[查看]选项卡2.点击[字数]统计结果如下所示:单击状态栏中的字符统计图标1.在... -
总计字数,类似word统计
2018-06-11 21:49:07总计字数 :类似word文档一样。一个单词算一个字,一个数字算一个字 -
Java统计英文句子中出现次数最多的单词并计算出现次数的方法
2020-08-28 08:18:13主要介绍了Java统计英文句子中出现次数最多的单词并计算出现次数的方法,涉及java针对英文句子的字符串遍历、转换、正则替换、计算等相关操作技巧,需要的朋友可以参考下 -
JAVA 仿 MS word 字数统计
2021-03-09 01:46:12// TODO caihao 2016-11-06 字数统计 工具类/*** 统计字数,参照MS office word 2007规则* @param context 文本内容* @return 字数*/public int getMSWordsCount(String context){int words_count = 0;//中文单词... -
用java 8计算字数
2021-07-17 01:54:36I am trying to implement a word count program in java 8 but I am unable to make it work. The method must take a string as parameter and returns a Map.When I am doing it in old java way, everthing work... -
java字数统计程序
2013-01-21 19:32:47java 的文旦处理小程序 可以对输入的文字进行字数统计 -
hbase-spark-playground:spark的基本java字数统计
2021-07-02 13:08:23spark的基本java字数@see spark.examples.SparkWordCount 一个基本的java类,在HBase中使用spark进行读写@see spark.examples.SparkToHBase ####How-to(可选)您需要使用以下命令设置您的 hadoop 配置:export ... -
Java简单统计字符串中汉字,英文字母及数字数量的方法
2021-03-06 22:39:40本文实例讲述了java简单统计字符串中汉字,英文字母及数字数量的方法。分享给大家供大家参考,具体如下:package org.zhy.demo.algorithm;/*** 有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出... -
java统计excel并生成新的excel
2015-10-28 11:12:57使用java统计多个excel文档,分月份统计信息,并将结果生成新的excel文档 -
Java字数统计程序
2021-03-07 09:14:01我正在尝试制作一个关于字数的程序,我已经部分制作并且它给出了正确的结果但是当我输入空格或字符串中的多个空格时,字数的结果显示错误的结果,因为我在计算单词在使用的空间的基础上.如果有一个解决方案,无论有多少... -
JAVA使用POI获取Excel的列数与行数
2021-03-13 23:31:39前言报表输出是Java应用开发中经常涉及的内容,而一般的报表往往缺乏通用性,不方便用户进行个性化编辑。Java程序由于其跨平台特性,不能直接操纵Excel。因此,本文探讨一下POI视线Java程序进行Excel中列数和行数的... -
java使用正则表达式实现字数统计
2021-04-18 01:03:02@Overridepublic CalculateWordCountDTO calculateWordCount(String str) throws ServiceException {CalculateWordCountDTO rtn = new CalculateWordCountDTO();int wordCount = 0;int characterCount = 0;... -
行走目录时字数统计PDF文件
2021-04-18 00:40:41我正在尝试构建一个Python程序,它将遍历一个目录(和所有子目录)并对所有.html,.txt和.pdf文件执行累计字数统计 . 在读取.pdf文件时,需要一些额外的(PdfFileReader)来解析文件 . 解析.pdf文件时,我收到以下错误,... -
Java HashSet对txt文本内容去重(统计小说用过的字或字数)
2021-03-10 09:48:20Java HashSet对txt文本内容去重(统计小说用过的字或字数)基本思路:1、字节流读需要去重的txt文本。(展示demo为当前workspace下名为utf-8.txt的文本)2、对读取到的单个字节判断(1)如果为字母或特殊字符。操作(2)(2)... -
word源码java-wordcount:字数
2021-06-05 23:27:30word源码java wordcount Hadoop: Intellij结合Maven本地运行和调试MapReduce程序 (无需搭载Hadoop和HDFS环境) 2018年05月20日 10:33:18 阅读数:11 编辑 楼主花费了1天终于按照教程自己第一次成功运行了这个案例。 ... -
Flink Java编程:统计字数和读取本地文件
2021-03-09 01:45:58org.apache.flinkgroupId> flink-streaming-java_2.11artifactId> ${flink.version}version> dependency> org.apache.flinkgroupId> flink-clients_2.11artifactId> ${flink.version}version> dependency> ... -
MapReduce字数统计案例
2017-10-24 14:39:39MapReduce字数统计案例,希望大家交流,最好自己写完后在对比交流,欢迎前来交流