Iterator ların Nasıl ve Nerede Kullanılacağı
Iterator arabirimine ait bir nesne, bir koleksiyonun tüm elemanlarında dolaşmak için kullanılabilir. Iterator’lar bir koleksiyondaki verileri incelemek için kullanılabilir, ancak temel koleksiyonu değiştirmek için kullanılamazlar. Genel olarak, bir koleksiyonun içeriğinde döngü yapmak için şu adımlar izlenmelidir:
- Koleksiyonun iterator yöntemini çağırarak koleksiyonun başlangıcına bir iterator elde edin.
- hasNext yöntemini çağıran bir döngü kurun. Döngünün hasNext yöntemi true döndüğü sürece devam etmesini sağlayın.
- Döngü içinde next yöntemini çağırarak her bir elemanı alın.
Aspose.Cells API’leri birçok iterator sağlar, ancak bu makale ağırlıklı olarak aşağıda listelenen üç türü tartışmaktadır.
- Hücreler Iterator’ı
- Satırlar Iterator’ı
- Sütunlar Iterator’ı
Iterator’ların Nasıl Kullanılacağı
Hücreler Iterator’ı
Hücrelerin iterator’una erişmenin çeşitli yolları vardır ve uygulama gereksinimlerine bağlı olarak herhangi birini kullanabilirsiniz. İşte hücrelerin iterator’ını döndüren yöntemler:
- Cells.iterator
- Row.iterator
- Range.iterator
Yukarıda bahsedilen tüm yöntemler, başlatılmış hücreler koleksiyonunu dolaşmaya izin veren iterator’ı döndürür.
Aşağıdaki kod örneği, bir hücre koleksiyonu için Iterator sınıfının uygulanmasını göstermektedir.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getDataDir(CellsIterator.class); | |
// Load a file in an instance of Workbook | |
Workbook book = new Workbook(dataDir + "sample.xlsx"); | |
// Get the iterator from Cells collection | |
Iterator cellIterator = book.getWorksheets().get(0).getCells().iterator(); | |
// Traverse cells in the collection | |
while (cellIterator.hasNext()) { | |
Cell cell = (Cell) cellIterator.next(); | |
; | |
System.out.println(cell.getName() + " " + cell.getValue()); | |
} | |
// Get iterator from an object of Row | |
Iterator rowIterator = book.getWorksheets().get(0).getCells().getRows().get(0).iterator(); | |
// Traverse cells in the given row | |
while (rowIterator.hasNext()) { | |
Cell cell = (Cell) rowIterator.next(); | |
System.out.println(cell.getName() + " " + cell.getValue()); | |
} | |
// Get iterator from an object of Range | |
Iterator rangeIterator = book.getWorksheets().get(0).getCells().createRange("A1:B10").iterator(); | |
// Traverse cells in the range | |
while (rangeIterator.hasNext()) { | |
Cell cell = (Cell) rangeIterator.next(); | |
System.out.println(cell.getName() + " " + cell.getValue()); | |
} |
Satırlar Iterator’ı
Satırlar Iterator’ı, RowCollection.iterator yöntemi kullanılırken erişilebilir. Aşağıdaki kod örneği, RowCollection sınıfı için Iterator’ın uygulanmasını göstermektedir.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getDataDir(RowsIterator.class); | |
// Load a file in an instance of Workbook | |
Workbook book = new Workbook(dataDir + "sample.xlsx"); | |
// Get the iterator for RowCollection | |
Iterator rowsIterator = book.getWorksheets().get(0).getCells().getRows().iterator(); | |
// Traverse rows in the collection | |
while (rowsIterator.hasNext()) { | |
Row row = (Row) rowsIterator.next(); | |
System.out.println(row.getIndex()); | |
} |
Sütunlar Iterator’ı
Sütunlar Iterator’ı, ColumnCollection.iterator yöntemi kullanılırken erişilebilir. Aşağıdaki kod örneği, ColumnCollection sınıfı için Iterator’ın uygulanmasını göstermektedir.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getDataDir(ColumnsIterator.class); | |
// Load a file in an instance of Workbook | |
Workbook book = new Workbook(dataDir + "sample.xlsx"); | |
// Get the iterator for ColumnsCollection | |
Iterator colsIterator = book.getWorksheets().get(0).getCells().getColumns().iterator(); | |
// Traverse columns in the collection | |
while (colsIterator.hasNext()) { | |
Column col = (Column) colsIterator.next(); | |
System.out.println(col.getIndex()); | |
} |
Iterator’ların Nerede Kullanılacağı
Iterator’ların kullanılmasının avantajlarını tartışmak için gerçek bir örnek ele alalım.
Senaryo
Bir uygulama gereksinimi, belirli bir Çalışma Sayfasındaki tüm hücreleri gezinerek değerlerini okumaktır. Bu hedefi uygulamanın birkaç yolu olabilir. Bazıları aşağıda gösterilmiştir.
Görüntü Aralığı Kullanarak
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getDataDir(UsingDisplayRange.class); | |
// Load a file in an instance of Workbook | |
Workbook book = new Workbook(dataDir + "sample.xlsx"); | |
// Get Cells collection of first worksheet | |
Cells cells = book.getWorksheets().get(0).getCells(); | |
// Get the MaxDisplayRange | |
Range displayRange = cells.getMaxDisplayRange(); | |
// Loop over all cells in the MaxDisplayRange | |
for (int row = displayRange.getFirstRow(); row < displayRange.getRowCount(); row++) { | |
for (int col = displayRange.getFirstColumn(); col < displayRange.getColumnCount(); col++) { | |
// Read the Cell value | |
System.out.println(displayRange.get(row, col).getStringValue()); | |
} | |
} |
MaxDataRow & MaxDataColumn Kullanarak
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getDataDir(UsingMaxDataRowAndMaxDataColumn.class); | |
// Load a file in an instance of Workbook | |
Workbook book = new Workbook(dataDir + "sample.xlsx"); | |
// Get Cells collection of first worksheet | |
Cells cells = book.getWorksheets().get(0).getCells(); | |
// Loop over all cells | |
for (int row = 0; row < cells.getMaxDataRow(); row++) { | |
for (int col = 0; col < cells.getMaxDataColumn(); col++) { | |
// Read the Cell value | |
System.out.println(cells.get(row, col).getStringValue()); | |
} | |
} |
Yukarıda bahsedilen her iki yaklaşımın da oldukça benzer mantığı kullandığını görebilirsiniz; yani, koleksiyondaki tüm hücrelerin üzerinden dolaşmak. Bu, aşağıda tartışılan birkaç nedenle sorunlu olabilir.
- MaxRow, MaxDataRow, MaxColumn, MaxDataColumn & MaxDisplayRange gibi API’ler, ilgili istatistikleri toplamak için ekstra zaman gerektirir. Veri matrisi (satır x sütun) büyükse, bu API’leri kullanmak performans cezası getirebilir.
- Çoğu durumda, verilen bir aralıktaki tüm hücreler oluşturulmamıştır. Bu tür durumlarda, matristeki her hücreyi kontrol etmek, yalnızca başlatılmış hücreleri kontrol etmekten daha verimli değildir.
- Bir döngü içinde Cells.get(rowIndex, columnIndex) gibi bir hücreye erişmek, bir aralıktaki tüm hücre nesnelerinin oluşturulmasına neden olur, bu da sonunda OutOfMemoryError’a neden olabilir.
Sonuç
Yukarıda belirtilen gerçeklere dayanarak, yineleyicilerin kullanılması gereken olası senaryolar aşağıda belirtilmiştir.
- Hücre koleksiyonunun salt okunur erişimi gereklidir; yani, yalnızca hücreleri denetlemek gereklidir.
- Büyük sayıda hücre gezilmelidir.
- Yalnızca başlatılmış hücreler/satırlar/sütunlar gezilmelidir.