簡體   English   中英

Java ArrayList根據動態對象字段排序嗎?

[英]Java ArrayList Sort on the basis of dynamic objects fields?

我遇到以下代碼問題

public void columnsList(List<TableRecord> records){

    for(TableRecord record : records){
        Table table = record.getTable();
        //Do sort here on stampDate
        Field[] fields = table.fields();
        for(Field field : fields){
            field.getName();
            record.getValue(field);

        }
    }

}

records對象包含不同類類型的對象

List<TableRecord> records = new List<TableRecord>();
records.add(new AddressRecord());
records.add(new CityRecord());
records.add(new UserRecord());

現在我需要如何根據每個類中的stampDate變量對它們進行排序,當列表中有不同的類時我們該怎么做

如果你上面的代碼是正確的,這意味着AddressRecordCityRecordUserRecord所有擴展TableRecord

class AddressRecord extends TableRecord {
    // other fields and methods here
}
class CityRecord extends TableRecord {
    // other fields and methods here
}
class UserRecord extends TableRecord {
    // other fields and methods here
}

您只需要為此類編寫一個Comparator 它看起來應該像這樣:

class TableRecord {
    private Date timeStamp;

    public Date getTimeStamp() {
        return timeStamp;
    }
// other fields and methods here
}

class RecordStampDateComparator implements Comparator<TableRecord>{

    public int compare(TableRecord tr1, TableRecord tr2) {
        Date tr1Date = tr1.getTimeStamp();
        Date tr2Date = tr2.getTimeStamp();   
        return tr1Date.compareTo(tr2Date);
    }
}

只需編寫帶有受保護字段stampDate的抽象類Record 即可 ,該類實現Comparable並重寫compareTo方法。

public abstract class Record implements Comparable<Record> {
    protected Date stampDate;

    @Override
    public int compareTo(Record anotherRecord){
        return this.stampDate.compareTo(anotherRecord.stampDate);
    }
}

然后用您的記錄類擴展該類:

public class AddressRecord extends Record{
...
}

public class CityRecord extends Record{
...
}

public class UserRecord extends Record{
...
}

如果您不能更改類,請編寫比較器( Comparator<Object> ),這將嘗試查找字段stampDate並對其進行比較。 比用它來排序列表。 比較器實現:

import java.util.Comparator;
import java.util.Date;


public class StampDateComparator implements Comparator<Object> {

@Override
public int compare(Object o1, Object o2) {

    try {
        Date d1 = (Date) o1.getClass().getDeclaredField("stampDate").get(o1);
        Date d2 = (Date) o2.getClass().getDeclaredField("stampDate").get(o2);
        return compare(d1, d2);
    } catch (SecurityException e) {
        throw new RuntimeException(e);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException("Missing variable stampDate");
    }catch (ClassCastException e) {
        throw new RuntimeException("stampDate is not a Date");
    } catch (IllegalArgumentException e) {
        //shoud not happen
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

}

在列表上使用以下Comparator類。

class TableRecordCompare implements Comparator<TableRecord>{
     if(TableRecord instanceof AddressRecord){
       // return compareTo for sample data of address.
     }
     else if(TableRecord instanceof CityRecord){
     // return compareTo for sample data of CityRecord.
      }
     else{
     // return compareTo for sample data of UserRecord.
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM