我想以这种格式(例如国家,城市和邮政编码)从csv文件中设置父ID
Greece
Athens
13561
10434
Cyprus
Larnaka
55477
答案 0 :(得分:-1)
如果其他人遇到此类问题,我只是想帮助他们。随机ID实际上是从数据库返回的ID。 如果我有一个错误,请修复它。
public class Record {
private String value;
private int level;
private int parent;
private int id;
public Record(String value, int level) {
super();
this.value = value;
this.level = level;
}
Random rand = new Random();
ArrayList<Record> records = new ArrayList<Record>();
records.add(new Record("ελλαδα", 0));
records.add(new Record("αθηνα", 1));
records.add(new Record("13561", 2));
records.add(new Record("1044", 2));
records.add(new Record("τριπολη", 1));
records.add(new Record("801456", 2));
records.add(new Record("501457", 2));
records.add(new Record("κυπρος", 0));
records.add(new Record("λαρνακα", 1));
records.add(new Record("987654", 2));
records.add(new Record("654321", 2));
for(int i =0; i<records.size();i++) {
if(i==0){
records.get(i).setId(rand.nextInt(50000));
records.get(i).setParent(-1);
}
if(i > 0){
int prev_depth = records.get(i-1).getLevel();
if(records.get(i).getLevel()>prev_depth){
records.get(i).setParent(records.get(i-1).getId());
records.get(i).setId(rand.nextInt(50000));
}else if(records.get(i).getLevel()==prev_depth){
records.get(i).setId(rand.nextInt(50000));
records.get(i).setParent(records.get(i-1).getParent());
}else{
int ridx = i - 1;
while ((prev_depth != records.get(i).getLevel()) && (ridx >= 0)){
prev_depth = records.get( ridx).getLevel();
ridx -= 1;
}
records.get(i).setId(rand.nextInt(50000));
if(ridx==-1){
records.get(i).setParent(-1);
}else{
records.get(i).setParent(records.get(ridx).getId());
}
}
}
}
records.stream().forEach(System.out::println);