我有一个带有制表符分隔数据的文本文件,如下所示:
0 fashioned 0.01
0 hard 0.01
0 taking 0.01
0 cool 0.01
0 conversation 0.01
0 biz 0.01
0 jobs 0.01
0 invest 0.01
1 loving 48.01
1 networks 0.01
1 campaigns 0.01
1 raise 0.01
1 competition 0.01
1 kitten 0.01
1 slashed 0.01
1 planned 0.01
我需要以格式存储: 0,< 0.01,0.01,0.01,0.01> 1,LT; 48.01,0.01,0.01,0.01,0.01> 如何才能做到这一点?我尝试过这段代码,但没有提供适当的输出。
public void wordArray() throws FileNotFoundException, IOException {
ArrayList<Integer> topic = new ArrayList<Integer>();
ArrayList<String> word = new ArrayList<String>();
ArrayList<Double> val = new ArrayList<Double>();
String s,tweet[] = null, d[];
int us = -1;
double max = 0;
FileReader fr = new FileReader("/home/lenovo/abc/words.txt");
BufferedReader br = new BufferedReader(fr);
list1=new ArrayList<Double>();
while ((s = br.readLine()) != null) {
us++;
d = s.split("\t");
topic.add(Integer.parseInt(d[0]));
if(us==0){
list1.add(Double.parseDouble(d[2]));
maxp.put(topic.get(us),list1);
}
else{
maxp.put(Integer.parseInt(d[0]),list1=new ArrayList<Double>());
list1.add(Double.parseDouble(d[2]));
}
我想为第一列的不同值创建一个新列表并添加到map。
答案 0 :(得分:0)
目前,它看起来过于复杂和难以理解。
但你可以通过创建一个对象并使用地图来转过它。
e.g。您可以创建一个对象来表示记录 -
class Record {
private final Integer topic;
private final String word;
private final Double value;
public Record(Integer topic, String word, Double value) {
this.topic = topic;
this.word = word;
this.value = value;
}
public static Record valueOf(String recordLine) {
String[] fields = recordLine.split("\\s+");
return new Record(
Integer.valueOf(fields[0]),
fields[1],
Double.valueOf(fields[2])
);
}
public Integer getTopic() {
return topic;
}
public String getWord() {
return word;
}
public Double getValue() {
return value;
}
}
然后按如下方式声明地图 -
Map<Integer, List<Record>> map = new HashMap<Integer, List<Record>>();
然后填充地图 -
while ((line = br.readLine()) != null) {
Record currRec = Record.valueOf(line);
List<Record> currList = map.get(currRec.getTopic());
if(currList == null) {
currList = new ArrayList<Record>();
map.put(currRec.getTopic(), currList);
}
currList.add(currRec);
现在您有按主题分组的记录。
答案 1 :(得分:0)
我喜欢libs。试试jackson,commons.collection - 你得到真正的POJOS
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import java.io.IOException;
import java.io.Serializable;
import java.util.Iterator;
import org.apache.commons.collections.map.MultiValueMap;
public class Jackson {
private static final String data = "0\tFirst name\t10\t1.23\n"
+ "1\tSecond name\t23\t\n"
+ "0\tThird name\t30\t1.87";
public static void main(String[] args) throws IOException {
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(Data.class)
.withoutHeader()
.withColumnSeparator('\t')
.withLineSeparator("\n");
Iterator<Data> result = mapper.reader(Data.class).withSchema(schema).readValues(data);
MultiValueMap map = new MultiValueMap();
while (result.hasNext()) {
Data d = result.next();
map.put(d.getIndex(), d);
System.out.println(d);
}
//
// Here is your map
//
System.out.println(map);
}
public static class Data implements Serializable {
private int index;
private String name;
private int age;
private Double size;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Double getSize() {
return size;
}
public void setSize(Double size) {
this.size = size;
}
@Override
public String toString() {
return "Data{" + "name=" + name + ", age=" + age + ", size=" + size + '}';
}
}
}