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

Java 从arrayList中删除双括号到单括号,java,arraylist,stream,hashmap,Java,Arraylist,Stream,Hashmap,此代码的目的是使用HashMaps和Streams统计按顺序出现的字母实例 代码打印出来 <[[is=3,imple=2,it=1]]> 我尝试使用.toString()来解决此问题,但使用.replace()删除一组括号会导致预期结果更改为 <is=3[ imple=2 it=1 ]> 还有我要打印的实际代码 <is=3[,imple=2,it=1]> 我也尝试过在toString()中使用.replace删除逗号,但我觉得我正在对答案进行硬编

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

代码打印出来

<[[is=3,imple=2,it=1]]>
我尝试使用.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);
}

}
import java.util.HashMap;
导入java.util.LinkedList;
导入java.util.Map;
导入java.util.Map.Entry;
导入java.util.Scanner;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
导入java.io.ByteArrayOutputStream;
导入java.io.PrintStream;
导入java.util.List;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
公共类字数{
受保护地图计数;
静态扫描仪输入=新扫描仪(系统输入);
公共字数(){
计数=新的HashMap();
}
公共映射getCounts(){
返回计数;
}
公共int解析(扫描程序输入,模式){
int计数器=0;
while(在.hasNext()中){
//获取下一个令牌
字符串标记=in.next();
//匹配令牌中的模式
Matcher Matcher=pattern.Matcher(令牌);
//处理令牌中找到的每个匹配项(可能不止一个)
while(matcher.find()){
//获取与模式匹配的字符串
字符串s=matcher.group().trim();
//现在做点什么吧
计数器=计数.containsKey?计数.get:0;
计数。放置(s,计数器+1);
}
}
返回计数器;
}
公共作废报告(打印流打印流){
列表结果=新建ArrayList();
results.addAll(counts.entrySet());
Collections.sort(results,Collections.reverseOrder(Map.Entry.comparingByValue());
字符串aList=results.toString();
aList=results.toString().replace(“[”,”).replace(“]”,”);
printstream.print(aList);
}
}
//测试用例
在这里输入代码
导入java.io.ByteArrayOutputStream;
导入java.io.PrintStream;
导入java.util.Scanner;
导入java.util.regex.Pattern;
导入junit.framework.TestCase;
公共类TestWordCount扩展了TestCase{
public void test\u WordCount\u parse(){
WordCount wc=新的WordCount();
Scanner in=新扫描仪(“这是一个简单的测试,但不容易通过”);
Pattern=Pattern.compile(“[i][a-z]+”);
parse(in,pattern);
assertEquals((整数)3,wc.getCounts().get(“is”);
assertEquals((整数)2,wc.getCounts().get(“imple”);
assertEquals((整数)1,wc.getCounts().get(“it”);
}
公共无效测试\字数\报告(){
WordCount wc=新的WordCount();
Scanner in=新扫描仪(“这是一个简单的测试,但不容易通过”);
Pattern=Pattern.compile(“[i][a-z]+”);
parse(in,pattern);
ByteArrayOutputStream输出=新建ByteArrayOutputStream();
wc.报告(新打印流(输出));
String out=output.toString();
字符串ls=System.lineSeparator();
assertEquals(“is=3_imple=2_it=1_Uls”)。替换(“Uls”,out);
}
}

在test\u WordCount\u 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());

在test\u WordCount\u 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());

您的断言中有“u”的特殊原因吗?你不能用逗号检查一下吗。为什么要使用行分隔符呢?因为我将把这段代码应用到一个.txt脚本文件中,以计算单个单词的出现次数,并希望在每个单词之间用新行对它们进行解析,以确保其易读性。对,但您在替换“\ux”后进行断言。为了便于阅读,您可以在测试后设置格式。您的断言中有“u”的特殊原因吗?你不能用逗号检查一下吗。为什么要使用行分隔符呢?因为我将把这段代码应用到一个.txt脚本文件中,以计算单个单词的出现次数,并希望在每个单词之间用新行对它们进行解析,以确保其易读性。对,但您在替换“\ux”后进行断言。为了便于阅读,您可以在测试后设置格式。为了解决我的问题,我最后做的是删除.toString(),将其替换为.toArray(),并使用For循环来迭代printstream.print非常感谢您的帮助!为了解决我的问题,我最后做的是删除.toString()并将其替换为.toArray(),然后使用for循环来迭代printstream.print非常感谢您的帮助!