java,多层map应该怎么写?求个葫芦# Java - 爪哇娇娃
n*7
1 楼
最近看java,看了快200页还没扯到String,烦了
因为我的工作设计很多文件parsing的事情,就直接开写来学习
结果卡在map上了,求给个示范
经常要处理文件,比如input.txt:
Group,Sample,Gene,Variance
Patient,1,A,V22E
Patient,1,A,D54N
Patient,1,B,K17R
Control,2,C,A97G
Patient,3,A,V22E
Patient,3,C,I31V
Patient,3,D,G85A
Control,4,C,A97G
用python处理是这样的:
data = {}
with open('input.txt') as f:
f.next() #skip the title line
for line in f:
(group,sample,gene,variance) = line.rstrip().split(',')
if group not in data:
data[group] = {}
if sample not in data[group]:
data[group][sample] = {}
if gene not in data[group][sample]:
data[group][sample][gene] = []
data[group][sample][gene].append(variance)
用java写成了这样:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestInput {
public static void main(String[] args) throws IOException {
String file = "input.txt";
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine()) != null) {
if (line.contains("Group")) { //title line
continue;
}
String[] fields = line.trim().split(",");
???
}
br.close();
}
}
问号处不知道怎么写好了了。 搜了一些stackOF上的问题,觉得还是不明白
比如这个:
http://stackoverflow.com/questions/4120216/map-of-maps-how-to-k
提问者自己追加的solution是
Map
因为我的工作设计很多文件parsing的事情,就直接开写来学习
结果卡在map上了,求给个示范
经常要处理文件,比如input.txt:
Group,Sample,Gene,Variance
Patient,1,A,V22E
Patient,1,A,D54N
Patient,1,B,K17R
Control,2,C,A97G
Patient,3,A,V22E
Patient,3,C,I31V
Patient,3,D,G85A
Control,4,C,A97G
用python处理是这样的:
data = {}
with open('input.txt') as f:
f.next() #skip the title line
for line in f:
(group,sample,gene,variance) = line.rstrip().split(',')
if group not in data:
data[group] = {}
if sample not in data[group]:
data[group][sample] = {}
if gene not in data[group][sample]:
data[group][sample][gene] = []
data[group][sample][gene].append(variance)
用java写成了这样:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestInput {
public static void main(String[] args) throws IOException {
String file = "input.txt";
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine()) != null) {
if (line.contains("Group")) { //title line
continue;
}
String[] fields = line.trim().split(",");
???
}
br.close();
}
}
问号处不知道怎么写好了了。 搜了一些stackOF上的问题,觉得还是不明白
比如这个:
http://stackoverflow.com/questions/4120216/map-of-maps-how-to-k
提问者自己追加的solution是
Map