簡體   English   中英

如何在ObservableList中使用LocalTime添加缺少的時間?

[英]How can I add the missing time using LocalTime in ObservableList?

我正在嘗試創建一個時間表,在這里我只能獲取輸入的時間和時間表名稱。

我想將時間表添加到List但是如果沒有其他時間表,則如果沒有其他早期時間,應該從6:00 AM開始為列表添加一個新的空時間表。

我有ObservableList<DataClass>它包含LocalTime和一些String

示例1:假設列表包含3個項目:

[04:00 AM] [Some String]
[06:30 AM] [Some String]
[05:00 PM] [Some String]

我想在列表中從4:00 AM5:00 PM添加缺少的時間,因此列表將是:

[04:00 AM] [Some String]
[05:00 AM] [Some String]
[06:30 AM] [Some String]
[07:00 AM] [Some String]
[08:00 AM] [Some String]
[09:00 AM] [Some String]
[10:00 AM] [Some String]
[11:00 AM] [Some String]
[12:00 PM] [Some String]
[01:00 PM] [Some String]
[02:00 PM] [Some String]
[03:00 PM] [Some String]
[04:00 PM] [Some String]
[05:00 PM] [Some String]

示例2:假設列表包含2個項目:

[08:30 AM] [Some String]
[02:00 PM] [Some String]

我想在列表中從6:00 AM5:00 PM添加缺少的時間,因此列表將是:

[06:00 AM] [Some String]
[07:00 AM] [Some String]
[08:00 AM] [Some String]
[09:00 AM] [Some String]
[10:00 AM] [Some String]
[11:00 AM] [Some String]
[12:00 PM] [Some String]
[01:00 PM] [Some String]
[02:00 PM] [Some String]
[03:00 PM] [Some String]
[04:00 PM] [Some String]
[05:00 PM] [Some String]

示例3:假設列表包含1個項目:

[08:00 PM] [Some String]

我想在列表中添加從6:00 AM8:00 PM的缺少時間,因此列表將是。

[06:00 AM] [Some String]
[07:00 AM] [Some String]
[08:00 AM] [Some String]
[09:00 AM] [Some String]
[10:00 AM] [Some String]
[11:00 AM] [Some String]
[12:00 PM] [Some String]
[01:00 PM] [Some String]
[02:00 PM] [Some String]
[03:00 PM] [Some String]
[04:00 PM] [Some String]
[05:00 PM] [Some String]
[06:00 PM] [Some String]
[07:00 PM] [Some String]
[08:00 PM] [Some String]

如果沒有其他較早的時間,則時間應從06:00 AM開始,否則時間將從該較早的時間開始。

如果沒有其他時間,則時間應在5:00 PM結束,否則時間將在該特定時間結束,我只想添加HOUR類的increment小時,因此不應為6:30 : 5:30 6:30 ,除非手動輸入。

我正在考慮以下邏輯,但由於想法idea鎖而無法進行。

  1. 根據從上午到下午的時間對列表進行排序,以獲取第一時間

  2. 檢查第一個數據的時間是否等於或小於6:00 AM
    如果為true,則從該時間開始並繼續添加缺少的時間,直到5:00 PM或最后一次到達為止。
    如果為假,則從6:00 AM開始並繼續添加缺少的時間,直到5:00 PM或最后一次到達為止。

下面的代碼是目前我所困惑的。

private void fillTheList(){
        ObservableList<DataClass> data = FXCollections.observableArrayList();
        Comparator<DataClass> comparator = Comparator.comparing(DataClass::getLocalTime);

        data.add(new DataClass(convertTime("05:00 PM"), "Sample Content"));
        data.add(new DataClass(convertTime("06:30 AM"), "Sample Content"));

        FXCollections.sort(data,comparator); //Sort the list from AM to PM

        for (DataClass list : data){
            if(list.getLocalTime().isBefore(LocalTime.of(6,0))){
                //The time is less than 6:00 AM then it should start here and Add the missing time but I don't know what to do next...
            }else{
                //the time is not less than 6:00 AM... I don't know what to do next..
            }
        }
        FXCollections.sort(data,comparator); //Sort the list from AM to PM again
}

private LocalTime convertTime(String timeString){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a");
        return LocalTime.parse(timeString, formatter);
}

PS:我實際上不確定我要問的是什么,因此如有必要,可以隨時提出修改建議。

更新:DataClass.class

public class DataClass {

    private LocalTime localTime;
    private String content;

    public DataClass(LocalTime localTime, String content){
        this.localTime = localTime;
        this.content = content;
    }

    public LocalTime getLocalTime(){
        return localTime;
    }

    public String getContent(){
        return content;
    }
}

您自己的狀態很好。 此外,azro是正確的,您需要通過與數據中已有的最小和最大時間進行比較來找到要考慮插入的最小和最大小時。 Jai是正確的,您應該在插入列表之前檢查列表中是否已經有時間,並且為此目的方便使用流。 我的版本在一個小時內使用int進行迭代,但其他所有都正確的是LocalTime可以工作。

    int minHour = 6;
    int maxHour = 17;
    if (! data.isEmpty()) {
        // adjust min and max from list contents
        int minExisintgHour = data.get(0).getLocalTime().getHour();
        if (minExisintgHour < minHour) {
            // if the list already contained 4:00 or 4:30,
            // we only need to add hours from 5, so add 1
            minHour = minExisintgHour + 1;
        }
        int maxExisintgHour = data.get(data.size() - 1).getLocalTime().getHour();
        if (maxExisintgHour > maxHour) {
            maxHour = maxExisintgHour;
        }
    }
    for (int hour = minHour; hour <= maxHour; hour++) {
        LocalTime time = LocalTime.of(hour, 0);
        boolean alreadyInData = data.stream().anyMatch(d -> d.getLocalTime().equals(time));
        if (! alreadyInData) {
            data.add(new DataClass(time, "Added beacuse time was missing"));
        }
    }

我假設您正在像問題一樣在上述代碼之前和之后對列表進行排序。 如果對最小和最大進行線性遍歷,則可以省略之前的排序(一些流也可以這樣做)。

結果列表示例:

[06:00 AM] [Added beacuse time was missing]
[06:30 AM] [Sample Content]
[07:00 AM] [Added beacuse time was missing]
[08:00 AM] [Added beacuse time was missing]
[09:00 AM] [Added beacuse time was missing]
[10:00 AM] [Added beacuse time was missing]
[11:00 AM] [Added beacuse time was missing]
[12:00 PM] [Added beacuse time was missing]
[01:00 PM] [Added beacuse time was missing]
[02:00 PM] [Added beacuse time was missing]
[03:00 PM] [Added beacuse time was missing]
[04:00 PM] [Added beacuse time was missing]
[05:00 PM] [Sample Content]

您必須按照以下步驟

  1. 獲取開始的值,它將是06AM或列表的第一個值,這取決於哪個是第一個

  2. 得到end的值,它只是第二個值

  3. 清除列表以刪除開始和結束值
  4. 遍歷2之間的所有日期,每次遞增1h
  5. 結束條件是i.isBefore(end.plusHours(1))而不是i.isBefore(end)因為您需要包含end元素
  6. 排序並打印
ObservableList<DataClass> data = FXCollections.observableArrayList();
Comparator<DataClass> comparator = Comparator.comparing(DataClass::getLocalTime);

data.add(new DataClass(convertTime("05:00 PM"), "Sample Content"));
data.add(new DataClass(convertTime("06:30 AM"), "Sample Content"));

FXCollections.sort(data,comparator); //Sort the list from AM to PM
// 1. & 2.
LocalTime begin = LocalTime.of(6,0);
if(data.get(0).getLocalTime().isBefore(begin)){
   begin = data.get(0).getLocalTime();
}

LocalTime end = LocalTime.of(17,0);
if(end.isBefore(data.get(data.size()-1).getLocalTime())){
    end = data.get(data.size()-1).getLocalTime();
}

// 3.
data.clear();
// 4. & 5.
for(LocalTime i = begin.withMinute(0); !i.isAfter(end); i = i.plusHours(1)){
   data.add(new DataClass(i, "Sample Content"));        
}

// 6. 
FXCollections.sort(data,comparator); //Sort the list from AM to PM again
System.out.println(data);
//  [06:00, 07:00, 08:00, 09:00, 10:00, 11:00, 12:00, 13:00, 14:00, 15:00, 16:00, 17:00]

不能完全確定您需要什么,但這可能是您需要的:

ObservableList<DataClass> data = FXCollections.observableArrayList();

// Need to specify the period that you want the entries to be filled
private void fill(LocalTime from, LocalTime to) {
    // Keep adding new entries until we have reached the end
    while (!from.isAfter(to)) {
        // We need a final variable for stream()
        final LocalTime temp = from;

        // If data does not contain any time that is equivalent to this time
        if (data.stream().noneMatch(d -> temp.equals(d.getLocalTime()))) {
            data.add(new DataClass(temp, "Hello World"));
        }

        // Increment the time by an hour, and wait for next loop cycle
        from = from.plusHours(1);
    }
}

除非要以排序方式顯示數據,否則排序沒有任何意義。

更新資料

此實現不會阻止您傳入分鍾(或秒)值的fromto

如果您需要以小時為單位,則可以將其更改為:

ObservableList<DataClass> data = FXCollections.observableArrayList();

// Need to specify the period that you want the entries to be filled
private void fill(int fromHour, int toHour) {
    LocalTime from = LocalTime.of(fromHour, 0);
    LocalTime to = LocalTime.of(toHour, 0);

    // Keep adding new entries until we have reached the end
    while (!from.isAfter(to)) {
        // We need a final variable for stream()
        final LocalTime temp = from;

        // If data does not contain any time that is equivalent to this time
        if (data.stream().noneMatch(d -> temp.equals(d.getLocalTime()))) {
            data.add(new DataClass(temp, "Hello World"));
        }

        // Increment the time by an hour, and wait for next loop cycle
        from = from.plusHours(1);
    }
}

這將強制兩個邊界值僅具有小時值。

進行如下

  1. 排序清單
  2. 設置可能需要插入的時間(從插入范圍的下限開始)
  3. 查找時間大於等於要插入的時間的第一個元素的索引
  4. 如果元素的時間與要插入的時間不匹配,則在該索引處插入一個新元素
  5. 修改要插入的時間,然后繼續3.(如果尚未離開插入范圍)
ObservableList<DataClass> data = ...
LocalTime lowerBound = LocalTime.of(6, 0); // or some other input
LocalTime upperBound = LocalTime.of(12 + 5, 0); // or some other input


Comparator<DataClass> comparator = Comparator.comparing(DataClass::getLocalTime);
FXCollections.sort(data, comparator);

int size = data.size();
int index = 0;

while (!lowerBound.isAfter(upperBound)) {
    // find inserting index
    while (index < size && data.get(index).getLocalTime().isBefore(lowerBound)) {
        index++;
    }
    // insert, if not already in list
    if (index >= size || !data.get(index).getLocalTime().equals(lowerBound)) {
        data.add(index, new DataClass(lowerBound, "Inserted"));
        size++;
    }
    lowerBound = lowerBound.plusHours(1);
    index++;
}

請注意,對於列表中較小的步驟和較大數量的元素,代碼的性能不是最佳的,因為添加到列表的中間是O(n) ,因此結果復雜度為O(i + i*n + n*log(n)) ,其中i是插入次數, n是列表的初始大小。

為了改善這一點,您可以將所有新元素添加到列表的末尾,然后使用一次mergesort的merge步驟,這將導致運行時O(i+n*log(n))

final int size = data.size();
int index = 0;

while (!lowerBound.isAfter(upperBound)) {
    // find inserting index
    while (index < size && data.get(index).getLocalTime().isBefore(lowerBound)) {
        index++;
    }
    // insert, if not already in list
    if (index >= size || !data.get(index).getLocalTime().equals(lowerBound)) {
        data.add(new DataClass(lowerBound, "Inserted"));
    } else {
        index++;
    }
    lowerBound = lowerBound.plusHours(1);
}

// merge
DataClass[] merged = new DataClass[data.size()];
int insertionIndex = 0;
int index1 = 0;
int index2 = size;
while (index1 < size && index2 < merged.length) {
    DataClass v1 = data.get(index1);
    DataClass v2 = data.get(index2);
    if (v2.getLocalTime().isBefore(v1.getLocalTime())) {
        merged[insertionIndex] = v2;
        index2++;
    } else {
        merged[insertionIndex] = v1;
        index1++;
    }
    insertionIndex++;
}

// copy remaining
while (index1 < size) {
    merged[insertionIndex++] = data.get(index1++);
}
while (index2 < merged.length) {
    merged[insertionIndex++] = data.get(index2++);
}
data.setAll(merged);

暫無
暫無

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

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