简体   繁体   中英

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. This is a tiny sample from the content of the text file:

Outlook Temperature Humidity Windy 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

   array[0] == "outlook" 

I have the following 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<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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