文件转换为HashMap

4

我有一个存在内存中的文件,我想构建一个类型为HashMap的映射表:

HashMap<Date, List<String>>

超出了它的范围。文件的示例记录如下:

[1987-11-19=[Ashwin Anthony, 100000065750184, /sdcard/DirPrime/1389422388122.jpg]

我有的示例代码如下:

public void readSDMap(){
         try{
                File toRead=new File("/sdcard/DirPrime/filetwo.txt");
                FileInputStream fis=new FileInputStream(toRead);

                Scanner sc=new Scanner(fis);

                HashMap<Date,List<String>> mapInFile=new HashMap<Date,List<String>>();


                String currentLine;
                while(sc.hasNextLine()){
                    currentLine=sc.nextLine();

                    StringTokenizer st=new StringTokenizer(currentLine,"=",false);

                        **//error here**
                        The method put(Date, List<String>) in the type 
                        HashMap<Date,List<String>> is not applicable for the 
                        arguments (String, String)
                    mapInFile.put(st.nextToken(),st.nextToken());
                }
                fis.close();
                System.out.println(mapInFile);
                //print All data in MAP
                for(Map.Entry<Date,List<String>> m :mapInFile.entrySet()){
                 //   System.out.println(m.getKey()+" : "+m.getValue());
                }
            }catch(Exception e){} 
    }

如何重构地图,我无法理解如何制作地图的值部分。这是一个列表。

编辑 (这只是一个快速解决方案,请忽略我将字符串转换为日期的不规范方式) 解析日期后:

Date d = null; 
                try {
                    d = new SimpleDateFormat("yyyy-MM-dd"/*, current*/).parse(st.nextToken());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                Date    d1 = new java.sql.Date(d.getTime());






                mapInFile.put(d1,st.nextToken());

我对解决后半部分的方程感兴趣,即构建一个列表。


错误信息非常清楚... 您正在输入 (String,String),但是您已经定义了要存储(Date,String)的映射。 - Mitesh Pathak
是的,我的问题不是关于错误信息,而是关于如何构建一个列表,以便我可以将其放在里面。 - Skynet
抱歉,我的错。我只是评论了你的问题,没有回答它 :D - Mitesh Pathak
我的错,我应该明确说明。现在已经编辑过了。 - Skynet
3个回答

2
地图键是日期,你需要修复这行代码:
Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(st.nextToken());
mapInFile.put(date, Arrays.asList(st.nextToken().split(" ,")));

请注意,当您分割右侧字符串时,可以使用多个分隔符。

我将解析日期,但是如何构建列表,即 map 的后半部分? - Skynet
我之前尝试过,但是会出现以下错误:HashMap<Date,List<String>> 类型中的 put(Date, List<String>) 方法不适用于参数 (Date, String[])。 - Skynet
为什么不根据文件中的分隔符将字符串拆分?拆分后会得到一个String[]数组。 - mockinterface
只需使用Arrays.asList()进行转换即可。 - mockinterface
Arrays.asList 返回一个不可修改的列表,如果你想要一个可修改的列表,请使用 new ArrayList(Arrays.asList(array)) - PurkkaKoodari

1
可能尝试类似以下内容:

currentLine = sc.nextLine().replaceAll("\\[|\\]", "");

StringTokenizer st=new StringTokenizer(currentLine,"=",false);

.......

DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
mapInFile.put(df.parse(st.nextToken()), Arrays.asList(st.nextToken().split(",")));

删除额外的字符如'['。 - Mitesh Pathak
这种方法将 [] 留给了列表的值。 - PurkkaKoodari
使用substring函数删除[],或者直接使用str.replace("[", "");str.replace("]", ""); - Mitesh Pathak
我知道,所以我正在使用 Array.asList() - Mitesh Pathak

1
你试图将数据以 String 的形式放入地图中,而不是应该使用的格式(DateList <String>)。你需要将字符串转换为相应的格式。
String dateStr = "1987-11-19";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(dateStr);
// *********
String listStr = "[A,B,some text]";
String cut = listStr.substring(1, listStr.length - 2);
String[] array = cut.split(",");
List<String> list = new ArrayList<String>(Arrays.asList(array));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接