此代码的目的是使用HashMaps和Streams计算按顺序出现的字母实例。
我遇到的问题是我的System.out.print(结果)正在打印[is = 3,imple = 2,it = 1]到控制台,但我的junit说“预期< [是= 3 imple = 2 it = 1]>但是< []>。
代码打印出[is = 3,imple = 2,it = 1],但实际上并没有将其更新到内存中。关于我应该做什么的任何提示或建议? 非常感谢你!
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class WordCount {
protected Map<String, Integer> counts;
static Scanner in= new Scanner(System.in);
public WordCount(){
counts = new HashMap<String,Integer>();
}
public Map getCounts(){
return counts;
}
public int parse(Scanner in, Pattern pattern){
int counter=0;
while (in.hasNext()) {
// get the next token
String token = in.next();
// match the pattern within the token
Matcher matcher = pattern.matcher(token);
// process each match found in token (could be more than one)
while (matcher.find()) {
// get the String that matched the pattern
String s = matcher.group().trim();
// now do something with s
counter=counts.containsKey(s) ? counts.get(s):0;
counts.put(s,counter+1);
}
}
return counter;
}
public void report(PrintStream printstream){
List<Map.Entry<String, Integer>> results = new ArrayList<Map.Entry<String, Integer>>();
for(Map.Entry<String, Integer> entry: counts.entrySet()){
results.add(entry);
Collections.sort(results,Collections.reverseOrder(Map.Entry.comparingByValue()));
results.toString();
}
System.out.println(results); // The main problem is this outputs [is=3,
imple=2, it=1] but the junit doesn't pass.
}
}
//Test Cases
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.regex.Pattern;
import junit.framework.TestCase;
public class TestWordCount extends TestCase {
public void test_WordCount_parse() {
WordCount wc = new WordCount();
Scanner in = new Scanner("this is a simple test, but it is not simple to pass");
Pattern pattern = Pattern.compile("[i][a-z]+");
wc.parse(in, pattern);
assertEquals((Integer)3, wc.getCounts().get("is"));
assertEquals((Integer)2, wc.getCounts().get("imple"));
assertEquals((Integer)1, wc.getCounts().get("it"));
}
public void test_WordCount_report() {
WordCount wc = new WordCount();
Scanner in = new Scanner("this is a simple test, but it is not simple to pass");
Pattern pattern = Pattern.compile("[i][a-z]+");
wc.parse(in, pattern);
ByteArrayOutputStream output = new ByteArrayOutputStream();
wc.report(new PrintStream(output));
String out = output.toString();
String ls = System.lineSeparator();
assertEquals("is=3_imple=2_it=1_".replace("_", ls), out);
}
答案 0 :(得分:0)
`public void report(PrintStream printstream)`
在此方法中,您不会向printstream
打印任何内容。尝试添加
printstream.print(results);
这种方法。
请注意,虽然System.out
本身就是PrintStream
,但它是绑定到控制台的不同流。