我有一个文本文件和一个CSV文件。当我的应用程序启动时,我会阅读文本文件和CSV文件,并将其放在List
,Set
或Map
中。这就是我在做的事情:
public class App {
private static List<String> namesList = new ArrayList<>();
private static List<String> zipCodeList = new ArrayList<>();
private static Set<String> citySet = new HashSet<>();
private static Set<String> stateSet = new HashSet<>();
private static Map<String, String> zipCityMap = new HashMap<>();
private static Map<String, String> zipStateMap = new HashMap<>();
static {
String nameFile = "Names.txt";
String zipCodeFile = "ZipCodes.csv";
try {
Path nameFilePath = Paths.get(ClassLoader.getSystemResource(nameFile).toURI());
namesList = getNamesList(nameFilePath);
System.out.println(namesList.contains("Kristina"));
Path csvFilePath = Paths.get(ClassLoader.getSystemResource(zipCodeFile).toURI());
List<String> csvList = getCsvList(csvFilePath);
citySet = getCitySet(csvList);
System.out.println(citySet.contains("Reddick"));
zipCodeList = getZipCodeList(csvList);
System.out.println(zipCodeList.contains("00210"));
stateSet = getStateSet(csvList);
System.out.println(stateSet.contains("KY"));
zipCityMap = getZipCityMap(csvList);
zipStateMap = getZipStateMap(csvList);
String zipCity = zipCityMap.get(83214);
String zipState = zipStateMap.get(83214);
System.out.println(zipCity);
System.out.println(zipState);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
}
private static List<String> getNamesList(Path nameFilePath) throws IOException {
List<String> namesList = null;
try (BufferedReader reader = Files.newBufferedReader(nameFilePath)) {
namesList = reader
.lines()
.map(line -> line)
.collect(Collectors.toList());
}
return namesList;
}
private static List<String> getZipCodeList(List<String> csvList) throws IOException {
List<String> zipCodeList = csvList
.stream()
.map(line -> line.split(","))
.map(arr -> arr[0])
.collect(Collectors.toList());
return zipCodeList;
}
private static Set<String> getCitySet(List<String> csvList) throws IOException {
Set<String> citySet = getSet(csvList, 1);
return citySet;
}
private static Set<String> getStateSet(List<String> csvList) throws IOException {
Set<String> stateSet = getSet(csvList, 2);
return stateSet;
}
private static Set<String> getSet(List<String> csvList, int columnIndex) throws IOException {
Set<String> stateSet = csvList
.stream()
.map(line -> line.split(","))
.map(arr -> arr[columnIndex])
.collect(Collectors.toSet());
return stateSet;
}
private static Map<String, String> getZipCityMap(List<String> csvList) throws IOException {
Map<String, String> zipCityMap = getMap(csvList, 0, 1);
return zipCityMap;
}
private static Map<String, String> getZipStateMap(List<String> csvList) throws IOException {
Map<String, String> zipStateMap = getMap(csvList, 0, 2);
return zipStateMap;
}
private static Map<String, String> getMap(List<String> csvList, int zipColumnIndex, int columnIndex) throws IOException {
Map<String, String> zipStateMap = csvList
.stream()
.map(line -> line.split(","))
.map(arr -> new AbstractMap.SimpleEntry<>(arr[zipColumnIndex], arr[columnIndex]))
.collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue()));
return zipStateMap;
}
private static List<String> getCsvList(Path csvFilePath) throws IOException {
List<String> csvList = null;
try (BufferedReader reader = Files.newBufferedReader(csvFilePath)) {
csvList = reader
.lines()
.skip(1)
.filter(line -> line.indexOf(',') != -1)
.map(line -> line)
.collect(Collectors.toList());
}
return csvList;
}
}
问题是contains()
方法返回false
。当我尝试从zipCityMap
或zipStateMap
获取价值时,我得到null
。虽然在调试时我把光标放到任何一个集合或映射或列表中我看到了那里的值,但是它打印为false或null。为什么要印刷这个均匀值?我该如何解决这个问题?
names.txt中
Kristina
Paige
Sherri
Gretchen
Karen
Patrick
Elsie
Hazel
Malcolm
Dolores
Francis
Sandy
ZipCodes.csv
"zip","city","state","latitude","longitude","timezone","dst"
"00210","Portsmouth","NH","43.005895","-71.013202","-5","1"
"00211","Portsmouth","NH","43.005895","-71.013202","-5","1"
"00212","Portsmouth","NH","43.005895","-71.013202","-5","1"
"00213","Portsmouth","NH","43.005895","-71.013202","-5","1"
"00214","Portsmouth","NH","43.005895","-71.013202","-5","1"
"00215","Portsmouth","NH","43.005895","-71.013202","-5","1"
"00501","Holtsville","NY","40.922326","-72.637078","-5","1"
"00544","Holtsville","NY","40.922326","-72.637078","-5","1"
"00601","Adjuntas","PR","18.180103","-66.74947","-4","0"
"00602","Aguada","PR","18.363285","-67.18024","-4","0"
"00603","Aguadilla","PR","18.448619","-67.13422","-4","0"
"00604","Aguadilla","PR","18.498987","-67.13699","-4","0"
"00605","Aguadilla","PR","18.465162","-67.141486","-4","0"
"00606","Maricao","PR","18.182151","-66.9588","-4","0"
"00607","Aguas Buenas","PR","18.256995","-66.104657","-4","0"
"00609","Aibonito","PR","18.142002","-66.273278","-4","0"
"00610","Anasco","PR","18.288319","-67.13604","-4","0"
"00611","Angeles","PR","18.279531","-66.80217","-4","0"
"00612","Arecibo","PR","18.449732","-66.69879","-4","0"
"00613","Arecibo","PR","18.458093","-66.732732","-4","0"
"00614","Arecibo","PR","18.429675","-66.674506","-4","0"
"00615","Arroyo","PR","17.96977","-66.061459","-4","0"
"00616","Bajadero","PR","18.426748","-66.67669","-4","0"
"00617","Barceloneta","PR","18.455499","-66.55575","-4","0"
"00618","Barranquitas","PR","18.185463","-66.305827","-4","0"
"00622","Boqueron","PR","18.003125","-67.16745","-4","0"
"00623","Cabo Rojo","PR","18.08643","-67.15222","-4","0"
"00624","Penuelas","PR","18.055399","-66.72602","-4","0"
"00625","Caguas","PR","18.232109","-66.039087","-4","0"
"00626","Caguas","PR","18.235003","-66.037318","-4","0"
"00627","Camuy","PR","18.435246","-66.85644","-4","0"
答案 0 :(得分:0)
您需要从输入字符串中删除引号。您可以这样修改getSet
方法:
Set<String> stateSet = csvList
.stream()
.map(line -> line.split(","))
.map(arr -> arr[columnIndex].replace("\"", ""))
.collect(Collectors.toSet());
同样应该在getMap
方法中完成。请注意,有许多库可以正确读取CSV文件(例如,Apache Commons CSV)。使用库来阅读众所周知的文件格式会更好,因为可能会有一些你可能不知道的微妙的事情。