-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashMap.java
99 lines (81 loc) · 3.17 KB
/
HashMap.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Write a Java Program to diplay the Student Details from a text file in a HashMap and then store this information in an ArrayList.
/*HashMap<String, String>
Keys = Roll,
Name,
Age,
Marks,
ArrayList<HashMap>
*/
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.FileReader;
public class StudentHashMap{
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new File Reader("FilePath.txt"));
StringBuffer sb = new StringBuffer();
List<Map> studentArrayList = new ArrayList<>();
String line;
while((line = br.readLine()) != null){
sb.append(line);
sb.append("\n");
String[] elements = line.split(", ");
String roll = elements[0].trim();
String name = elements[1].trim();
String age = elements[2].trim();
String marks = elements[3].trim();
Map<String, String> studentDetailsMap = new HashMap<>();
studentDetailsMap.put("Roll", roll);
studentDetailsMap.put("Name", name);
studentDetailsMap.put("Age", age);
studentDetailsMap.put("Marks", marks);
studentArrayList.add(studentDetailsMap);
}
br.close();
for(Map map : studentArrayList){
System.out.println(map);
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
/*RollNo Name Age Marks
101 Chandana 18 90
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
Create a text file with the above data and write a Java Program to read from the text file from each line and store it in a HashMap
Now use an ArrayList to get the student details from each Map.*/
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.FileReader;
public class StudentHashMap{
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new FileReader("FilePath.txt"));
String buffer sb = new StringBuffer();
List<Map> studentArrayList = new ArrayList<>();
String line;
while((line = br.readLine()) != null){
sb.append(line);
sb.append("\n");
String[] elements = line.split(", ");
String rollNo = elements[0];
String name = elements[1];
String age = elements[2];
String marks = elements[3];
Map<String, String> studentDetailsMap = new HashMap<>();
studentArrayList.add(studentDetailsMap);
}
br.close();
for(Map map : studentArrayList){
System.out.println(map);
}
}
}