-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.java
More file actions
118 lines (104 loc) · 4.11 KB
/
Copy pathDataManager.java
File metadata and controls
118 lines (104 loc) · 4.11 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package heyingzhe;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class DataManager {
private List<Map<String, String>> records;
private Set<String> fieldNames;
public DataManager(List<Map<String, String>> records) {
this.records = records;
this.fieldNames = new HashSet<>();
// 获取所有字段名
for (Map<String, String> record : records) {
fieldNames.addAll(record.keySet());
fieldNames.add("id");
}
// 添加索引
for (int index=0; index<records.size(); index++) {
Map<String, String> record = records.get(index);
record.put("id", String.valueOf(index));
}
}
// 获取所有字段名
public Set<String> getFieldNames() {
return fieldNames;
}
// 查询某一字段的所有数据
public List<String> getColumnData(String fieldName) {
List<String> columnData = new ArrayList<>();
for (Map<String, String> record : records) {
columnData.add(record.getOrDefault(fieldName, ""));
}
return columnData;
}
// 添加一个新字段,并为每条记录填充值
public void addColumn(String fieldName, String defaultValue) {
fieldNames.add(fieldName);
for (Map<String, String> record : records) {
record.put(fieldName, defaultValue);
}
}
// 删除一个字段
public void removeColumn(String fieldName) {
fieldNames.remove(fieldName);
for (Map<String, String> record : records) {
record.remove(fieldName);
}
}
// 修改某一条记录的字段值
public void updateFieldValue(int rowIndex, String fieldName, String newValue) {
if (rowIndex >= 0 && rowIndex < records.size()) {
records.get(rowIndex).put(fieldName, newValue);
}
}
// 打印所有记录
public void printRecords() {
for (Map<String, String> record : records) {
System.out.println(record);
}
}
// 保存数据到文件
public void saveToFile(String filePath, String delimiter) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
// 写入标题行
writer.write(String.join(delimiter, fieldNames));
writer.newLine();
// 写入数据行
for (Map<String, String> record : records) {
List<String> values = new ArrayList<>();
for (String fieldName : fieldNames) {
values.add(record.getOrDefault(fieldName, ""));
}
writer.write(String.join(delimiter, values));
writer.newLine();
}
writer.close();
}
public List<Map<String, String>> getData () {
return records;
}
// 根据 Map 中某个键值查找对应位置,从而方便定位到
public Map<String, String> findMap(List<Map<String, String>> list, String targetField, String targetValue) {
// 遍历 List
for (Map<String, String> map : list) {
if (map.containsKey(targetField) && map.get(targetField).equals(targetValue)) {
return map;
}
}
System.out.println("No map found with the target that you input.");
return list.getFirst();
}
// 根据 Map 中两个键值查找对应位置,从而方便定位到
public Map<String, String> findMap(List<Map<String, String>> list, String targetField1, String targetValue1,
String targetField2, String targetValue2) {
// 遍历 List
for (Map<String, String> map : list) {
if (map.containsKey(targetField1) && map.get(targetField1).equals(targetValue1) && map.containsKey(targetField2) && map.get(targetField2).equals(targetValue2)) {
return map;
}
}
System.out.println("No map found with the target that you input.");
return list.getFirst();
}
}