Java 我有一个字符串中的数字。如何将数字放入hashmap?

Java 我有一个字符串中的数字。如何将数字放入hashmap?,java,string,hashmap,Java,String,Hashmap,在这段代码中,我试图用hashmap生成kruskal算法代码的实现代码。因此,在第一段代码中,我将代码保存到一个字符串中,然后需要将字符串的值输入到hashmap中 private static final String FILENAME = "D:\\Kuliah\\Semester 7\\~USUL BULAN JANUARI\\PROBLEM\\sepuluh\\1.dat"; public static void main(String[] args) {

在这段代码中,我试图用hashmap生成kruskal算法代码的实现代码。因此,在第一段代码中,我将代码保存到一个字符串中,然后需要将字符串的值输入到hashmap中

private static final String FILENAME = "D:\\Kuliah\\Semester 7\\~USUL BULAN JANUARI\\PROBLEM\\sepuluh\\1.dat";

    public static void main(String[] args) {

                String content = "";

        BufferedReader br = null;
        FileReader fr = null;

        try {

            fr = new FileReader(FILENAME);
            br = new BufferedReader(fr);

            String sCurrentLine;

            br = new BufferedReader(new FileReader(FILENAME));

            while ((sCurrentLine = br.readLine()) != null) {
                            content = content + sCurrentLine + "\n";
    //          System.out.println(sCurrentLine);
            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                if (br != null)
                    br.close();

                if (fr != null)
                    fr.close();

            } catch (IOException ex) {

                ex.printStackTrace();

            }

        }

             System.out.print(content);

    }
         public static String extractNode(String content){
            String[] lines = content.split("\n");
            HashMap<Integer, Node> vertexs = new HashMap<Integer, Node>();

            for(int i=0;i<lines.length;i++){
             Node node = new Node();
             node.setId(0);
             vertexs.put(0, node);

                 //at this code how can i put it?

            }





            return "";
        }




}
private static final String FILENAME=“D:\\Kuliah\\7学期\\~USUL BULAN JANUARI\\PROBLEM\\sepuluh\\1.dat”;
公共静态void main(字符串[]args){
字符串内容=”;
BufferedReader br=null;
FileReader fr=null;
试一试{
fr=新文件读取器(文件名);
br=新的缓冲读取器(fr);
弦电流线;
br=新的BufferedReader(新的文件读取器(文件名));
而((sCurrentLine=br.readLine())!=null){
content=content+sCurrentLine+“\n”;
//System.out.println(sCurrentLine);
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
如果(br!=null)
br.close();
如果(fr!=null)
fr.close();
}捕获(IOEX异常){
例如printStackTrace();
}
}
系统输出打印(内容);
}
公共静态字符串提取节点(字符串内容){
字符串[]行=content.split(“\n”);
HashMap vertexs=新HashMap();

例如,对于(int i=0;i,您可以执行以下操作:

String x = "5";
int num = Integer.parseInt(x); //coverts String to Integer
希望这有帮助


`

您尝试过搜索“java字符串到整数”吗?有必要吗?因为我认为字符串的值可以直接输入到hashmap。在我的hashmap中,没有需要的整数值,但ID是。你有
hashmap
,所以是的,有必要遵循这些对象类型。如果你想让hashmap保存字符串,那么制作一个
hashmap
,我想字符串的值可以虽然字符串中的值是整数,但直接输入到hashmap。但在我的hashmap中,整数只是uniqe数,而不是值。