简体   繁体   English

如何从地图特定数据中的文本文件读取?

[英]How to read from text file in Map specific data?

I need the read from the data file of the form: 我需要从表格的数据文件中读取:

1,14.23,1.71,2.43,15.6,127,2.8,3.06,.28,2.29,5.64,1.04,3.92,1065 2,12.72,1.81,2.2,18.8,86,2.2,2.53,.26,1.77,3.9,1.16,3.14,714 2,12.08,1.13,2.51,24,78,2,1.58,.4,1.4,2.2,1.31,2.72,630 .................................... 3,13.11,1.9,2.75,25.5,116,2.2,1.28,.26,1.56,7.1,.61,1.33,425 3,13.23,3.3,2.28,18.5,98,1.8,.83,.61,1.87,10.52,.56,1.51,675 ................and so on 1,14.23,1.71,2.43,15.6,127,2.8,3.06,.28,2.29,5.64,1.04,3.92,1065 2,12.72,1.81,2.2,18.8,86,2.2,2.53,.26,1.77,3.9 ,1.16,3.14,714 2,12.08,1.13,2.51,24,78,2,1.58,.4,1.4,2.2,1.31,2.72,630 ................ .................... 3,13.11,1.9,2.75,25.5,116,2.2,1.28,.26,1.56,7.1,.61,1.33,425 3 ,13.23,3.3,2.28,18.5,98,1.8,.83,.61,1.87,10.52,.56,1.51,675 .......依此类推

Where first symbol - the key, the rest are values. 其中第一个符号-键,其余为值。

I try to do it this way 我尝试这样做

 Map<String, String> map = new HashMap<String, String>();
            BufferedReader in = new BufferedReader(new FileReader("wine.data.txt"));
            String line = "";
            while ((line = in.readLine()) != null) {
                String parts[] = line.split(",");
                for(int i = 1; i < parts.length; i++)
                map.put(parts[0], parts[i]);
                System.out.println(map.toString());
            }
            in.close();

But I see only last number of each line. 但是我只看到每行的最后一个数字。 How to fix it? 如何解决?

Everytime put is called, the value for the speficied key is being replaced. 每次调用put ,都会替换专用密钥的值。

You want to store the result in a Map<String, List<String>> : 您要将结果存储在Map<String, List<String>>

String parts[] = line.split(",");
List<String> values = map.get(parts[0]);
if(values == null) {   // first time this key is found -> create new list
    values = new ArrayList<>();
    map.put(parts[0], values);
}
for(int i = 1; i < parts.length; i++) {
    values.add(parts[i]);
}

map associates each key with a string; map每个键与一个字符串相关联; each time you call put , you replace whatever string used to be associated with that key. 每次调用put ,都将替换以前与该键关联的任何字符串。

It isn't clear what you want the outcome to be, so I can't say how to fix it. 目前尚不清楚您希望结果是什么,因此我无法说出解决方法。

Your code will overwrite the value in each iteration and will give the last value as final one. 您的代码将在每次迭代中覆盖该值,并给出最后一个值作为最终值。 Instead use a list, add all the values there, and assign that list as map value. 而是使用列表,在其中添加所有值,然后将该列表分配为地图值。

Map<String, List<String>> map = new HashMap<String, List<String>>();
            BufferedReader in = new BufferedReader(new FileReader("wine.data.txt"));
            String line = "";
            while ((line = in.readLine()) != null) {
                String parts[] = line.split(",");
                ArrayList<String> arrList = new ArrayList<String>();
                for(int i = 1; i < parts.length; i++)
                    arrList.add(parts[i]);
                map.put(parts[0], arrList);
                System.out.println(map.toString());
            }
            in.close();

Try this. 尝试这个。 It should work fine. 它应该工作正常。

Issue is that you were using same key to store each record in row. 问题是您使用相同的键将每个记录存储在行中。 Its necessary to do a little fundamental change by using List instead of String as value in map. 通过使用List而不是String作为map中的值来做一些根本的改变是必要的。 By the end you will get all items in each row as itemList with its own key(1st record). 到最后,您将获得每一行中的所有项目作为具有其自己的键(第一条记录)的itemList。 Here is the implementation based on your code: 这是基于您的代码的实现:

Map<String, ArrayList<String>> map = new HashMap<>();
BufferedReader in = new BufferedReader(new FileReader("wine.data.txt"));
String line = "";
while ((line = in.readLine()) != null) {
      String parts[] = line.split(",");
      ArrayList<String> valueList = new ArrayList<>(Arrays.asList(parts));
      valueList.remove(0);// get rid of 1st element because it will be used as akey
      map.put(parts[0], valueList);
      System.out.println(map.toString());
}
in.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM