Java 如何从多重映射中删除第一个和最后一个字符';s字符串表示法?

Java 如何从多重映射中删除第一个和最后一个字符';s字符串表示法?,java,syntax,guava,Java,Syntax,Guava,我正在尝试将Multimap.get()的结果输出到一个文件中。但是,我得到的[和]字符分别显示为第一个和最后一个字符 我试图使用这个程序,但它不打印整数之间的任何分隔符。我怎样才能解决这个问题 package main; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; impor

我正在尝试将
Multimap.get()的结果输出到一个文件中。但是,我得到的
[
]
字符分别显示为第一个和最后一个字符

我试图使用这个程序,但它不打印整数之间的任何分隔符。我怎样才能解决这个问题

package main;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

public class App {

public static void main(String[] args) {

    File file = new File("test.txt");
    ArrayList<String> list = new ArrayList<String>();
    Multimap<Integer, String> newSortedMap = ArrayListMultimap.create();

    try {
        Scanner s = new Scanner(file);
        while (s.hasNext()) {
            list.add(s.next());
        }
        s.close();
    } catch (FileNotFoundException e) {
        System.out.println("File cannot be found in root folder");
        ;
    }

    for (String word : list) {
        int key = findKey.convertKey(word);
        newSortedMap.put(key, word);
    }

    // Overwrites old output.txt
    try {
        PrintWriter writer = new PrintWriter("output.txt", "UTF-8");
        for (Integer key: newSortedMap.keySet()) {
            writer.println(newSortedMap.get(key));
        }
        writer.close(); 
    } catch (FileNotFoundException e) {
        System.out.println("FileNotFoundException e should not occur");
    } catch (UnsupportedEncodingException e) {
        System.out.println("UnsupportedEncodingException has occured");
    }
}
packagemain;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.PrintWriter;
导入java.io.UnsupportedEncodingException;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入java.util.Scanner;
导入com.google.common.collect.ArrayListMultimap;
导入com.google.common.collect.Maps;
导入com.google.common.collect.Multimap;
公共类应用程序{
公共静态void main(字符串[]args){
File File=新文件(“test.txt”);
ArrayList=新建ArrayList();
Multimap newSortedMap=ArrayListMultimap.create();
试一试{
扫描仪s=新扫描仪(文件);
而(s.hasNext()){
添加(s.next());
}
s、 close();
}catch(filenotfounde异常){
System.out.println(“在根文件夹中找不到文件”);
;
}
for(字符串字:列表){
int key=findKey.convertKey(word);
newSortedMap.put(关键字、单词);
}
//覆盖旧的output.txt
试一试{
PrintWriter=新的PrintWriter(“output.txt”、“UTF-8”);
for(整数键:newSortedMap.keySet()){
writer.println(newSortedMap.get(key));
}
writer.close();
}catch(filenotfounde异常){
System.out.println(“不应发生FileNotFounde异常”);
}捕获(不支持的编码异常e){
System.out.println(“发生了不支持的编码异常”);
}
}

您可以将
newSortedMap.get(key).toString()赋值给一个变量,比如
stringList
。现在调用
writer.println(stringList.substring(1,stringList.length()-1));

请理解,当您将列表传递到
writer.println
方法中时,它将调用对象的
toString()
方法并写入输出。
list.toString()
方法返回一个字符串,其中所有值由
分隔,并添加
[
]
到字符串的开头和结尾。

只需使用修改
字符串本身即可。
子字符串(int,int)

返回作为此字符串的子字符串的新字符串。子字符串从指定的
beginIndex
开始,并扩展到索引
endIndex-1
处的字符。因此子字符串的长度为
endIndex beginIndex

因此,将
映射
转换为
字符串
。然后,
1
字符串
表示的第二个字符,其余部分使用
mapString.length()-1

下面是一些工作代码:

PrintWriter writer = new PrintWriter("output.txt", "UTF-8");
String mapString = newSortedMap.toString();
writer.println(mapString.substring(1, mapString.length() - 1);

您可以将
newSortedMap.get(key).toString()赋值给变量,比如
stringList
。现在调用
writer.println(stringList.substring(1,stringList.length()-1))
很抱歉,在整个堆栈交换过程中,我得到了很多无法解释的反对票——我认为有必要做出解释,但这是我的帖子Meta@HarshPoddar我要试试——把它作为一个答案写下来,如果它奏效,我会接受的。