简体   繁体   English

将文本文件中的值添加到 HashMap 中

[英]Adding values from a text file into a HashMap

I need to add values from a text file with lots of data into a HashMap.我需要将包含大量数据的文本文件中的值添加到 HashMap 中。 This is a tiny sample from the content of the text file:这是来自文本文件内容的一个小示例:

Outlook Temperature Humidity Windy GoOutside Outlook 温度 湿度 有风 GoOutside
sunny...........hot..............high..........false.....No阳光明媚.......热......高.......假......没有
overcast........hot...............high.........false......Yes阴……热…………高…………假……是的

the "....." are spaces in the text file. “.....”是文本文件中的空格。

I have stored the heading of each column in an array and I want them to be the keys in my HashMap, so for example我已经将每列的标题存储在一个数组中,并且我希望它们成为我的 HashMap 中的键,例如

   array[0] == "outlook" 

I have the following HashMap我有以下 HashMap

HashMap<String, String> map = new HashMap<String, String>();

Assuming that the program is reading the file correctly, how can I make sure that I am inserting each value into the correct key?假设程序正确读取文件,我如何确保将每个值插入到正确的键中?

Thank you for your time感谢您的时间

Use HashMap inbuit function put to insert keys into the HashMap and use get function to get the value of any particular key inserted into the HashMap.使用 HashMap inbuit 函数 put 将键插入到 HashMap 中,并使用 get 函数获取插入到 HashMap 中的任何特定键的值。

        HashMap<String,String[]> map = new HashMap<>();
        //write code for taking input from file into string arrays
        for(int i = 0; i < numOfRows; i++){
            for(int j = 0; j < 4; j++){
                map.put(weather[i], condition[i][j]);
            }
        }

Here i is number of rows of input for each weather and j is for the following string value ie, temperature, humidity, windy and go outside.这里 i 是每个天气的输入行数,j 是以下字符串值,即温度、湿度、有风和外出。

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

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