2017-02-03 37 views
1

我剛開始使用Spring Batch,想知道維護在ItemReader中讀取的項目列表的最佳做法是什麼。我看到的例子有這樣的:Spring批處理ItemReader - 維護指針或從列表中刪除項目?

private static List<Resource> items = new LinkedList<Resource>(); 
private static int index = 0; 

private void initialize(){ 
    items.add(new Resource()); 
} 

public Model read(){ 
    if(index < items.size()) 
     return new Model(items.get(index++)); 
    return null; 
} 

什麼是這種方法相較於未使用的指標,簡單地縮短列表的好處是什麼?例如: -

public Model read(){ 
    if(items.size() > 0) 
     return new Model(items.remove(0)); 
    return null; 
} 

回答

2

我能想到之後在第2個方法缺點:

  • items上一次迭代後不保持可用。
  • 它可能會變慢,具體取決於List的具體類型。如果它是一個LinkedList,事情應該沒問題。但是對於ArrayList,很多元素都會在每個remove上移動。