从arrayList中删除双括号到单个括号

时间:2017-04-23 18:43:32

标签: java arraylist stream hashmap

此代码的目的是使用HashMaps和Streams计算按顺序出现的字母实例。

代码打印出来

<[[is=3,imple=2,it=1]]>

但是我需要通过这个测试

assertEquals("is=3_imple=2_it=1_".replace("_", ls), out);

我尝试使用.toString()来修复此问题,但使用.replace()删除一组括号会导致预期结果更改为

<is=3[
imple=2
it=1
]>

和我打印的实际代码

<is=3[,imple=2,it=1]>

我也尝试在toString()中使用.replace删除逗号,但我觉得我当时正在硬编码答案。

有关如何进行的任何建议?非常感谢你!

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.Arrays;
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>>();

    results.addAll(counts.entrySet());
    Collections.sort(results,   Collections.reverseOrder(Map.Entry.comparingByValue()));

    String aList = results.toString();

    aList = results.toString().replace("[", "").replace("]", "");
    printstream.print(aList);
}





}



//Test Case

enter code here
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);
}

}

1 个答案:

答案 0 :(得分:0)

在test_WordCount_report()方法中运行以下行之后: String out = output.toString()。输出应如下所示:

is=3, imple=2, it=1

所以你应该能够直接断言或者如果你需要保留那个特定的断言字符串,那么改变断言检查:

String assertValue = "is=3_imple=2_it=1_";
String updAssert = assertValue.substring(0,assertValue.length() - 1).replace("_", ", ");
assertEquals(updAssert, out);

稍后在代码中,您可以将updAssert(如果已通过)附加到字符串构建器,然后附加一个新行。

StringBuilder sb = new StringBuilder();
sb.append(updAssert).append(System.lineSeparator());