This example shows how to read a text file into HashMap in Java. This example uses a text file containing keys and values and converts them to the HashMap object.
How to read a text file to HashMap in Java?
I am going to use the below given text file contents for the purpose of this example.
1 2 3 4 5 |
Jack:26 Ryan:32 Mary:22 Emily:24 John:45 |
The text file contains a person’s name and age separated by a colon “:” in each line. The name will be used as a key of the HashMap object and age will be the value.
I have saved the text file at “E:\text-values.txt” location. Please change the code as per the location of your text file.
I am going to use the BufferedReader to read the text file line by line, split the line by colon and put the parts in the HashMap as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.HashMap; import java.util.Map; public class ReadTextFileToHashMapExample { /*** Change this - replace file path ****/ final static String filePath = "E:/text-values.txt"; public static void main(String[] args) { //read text file to HashMap Map<String, Integer> mapFromFile = getHashMapFromTextFile(); //iterate over HashMap entries for(Map.Entry<String, Integer> entry : mapFromFile.entrySet()){ System.out.println( entry.getKey() + " => " + entry.getValue() ); } } public static Map<String, Integer> getHashMapFromTextFile(){ Map<String, Integer> mapFileContents = new HashMap<String, Integer>(); BufferedReader br = null; try{ //create file object File file = new File(filePath); //create BufferedReader object from the File br = new BufferedReader( new FileReader(file) ); String line = null; //read file line by line while ( (line = br.readLine()) != null ){ //split the line by : String[] parts = line.split(":"); //first part is name, second is age String name = parts[0].trim(); Integer age = Integer.parseInt( parts[1].trim() ); //put name, age in HashMap if they are not empty if( !name.equals("") && !age.equals("") ) mapFileContents.put(name, age); } }catch(Exception e){ e.printStackTrace(); }finally{ //Always close the BufferedReader if(br != null){ try { br.close(); }catch(Exception e){}; } } return mapFileContents; } } |
Output
1 2 3 4 5 |
Emily => 24 Ryan => 32 John => 45 Jack => 26 Mary => 22 |
Important Note:
As you can see from the output, the order of the HashMap entries is not the same as file lines. That is because the HashMap class does not guarantee to maintain the insertion order of the mappings.
If the order of lines is important to you, use the LinkedHashMap class instead of the HashMap class. Change the below line in the code.
1 |
Map<String, Integer> mapFileContents = new HashMap<String, Integer>(); |
Replace it with the line given below.
1 |
Map<String, Integer> mapFileContents = new LinkedHashMap<String, Integer>(); |
Also, do not forget to import the LinkedHashMap class. After these changes, the output will have the map entries in the same order as the file lines.
Output
1 2 3 4 5 |
Jack => 26 Ryan => 32 Mary => 22 Emily => 24 John => 45 |
If you want to write HashMap to file, please refer to how to write HashMap to a text file example.
This example is a part of the Java HashMap tutorial with examples.
Please let me know your views in the comments section below.