For example, read a csv file, using first column as key and second column as value
Map<String, String> map=new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader(PATH_TO_YOUR_FILE));
String line="";
while((line=br.readLine())!=null){
String tmp[] = line.split(",");
map.put(tmp[0],tmp[1]);
... THINGS YOU WANT TO DO ...
}
br.close();
A fancy way: using Stream
Map<String, String> map = Files.lines(Paths.get(PATH_TO_YOUR_FILE))
.map(line -> line.split(","))
.collect(Collectors.toMap(
array -> array[0],
array -> array[1]
));
沒有留言:
張貼留言