@@ -199,6 +199,75 @@ public void onCompleted()
199199 };
200200 }
201201
202+ /**
203+ * A memory-efficient, read-only view that represents the transposed version of a list of objects.
204+ * This class implements the List interface but does not store the transposed data explicitly.
205+ * Instead, it computes the transposed data on-the-fly when accessed.
206+ */
207+ private static class TransposedIndexKeyView <T > extends AbstractList <List <IndexProto .IndexKey >>
208+ {
209+ private final List <T > originalData ;
210+ private final Function <T , List <IndexProto .IndexKey >> indexExtractor ;
211+ private final int columnCount ;
212+
213+ public TransposedIndexKeyView (List <T > originalData ,
214+ Function <T , List <IndexProto .IndexKey >> indexExtractor )
215+ {
216+ this .originalData = originalData ;
217+ this .indexExtractor = indexExtractor ;
218+ if (originalData == null || originalData .isEmpty ())
219+ {
220+ this .columnCount = 0 ;
221+ } else
222+ {
223+ this .columnCount = indexExtractor .apply (originalData .get (0 )).size ();
224+ }
225+ }
226+
227+ @ Override
228+ public List <IndexProto .IndexKey > get (int columnIndex )
229+ {
230+ if (columnIndex < 0 || columnIndex >= columnCount )
231+ {
232+ throw new IndexOutOfBoundsException ("Column index out of bounds: " + columnIndex );
233+ }
234+ return new ColumnView (columnIndex );
235+ }
236+
237+ @ Override
238+ public int size ()
239+ {
240+ return columnCount ;
241+ }
242+
243+ private class ColumnView extends AbstractList <IndexProto .IndexKey >
244+ {
245+ private final int columnIndex ;
246+
247+ public ColumnView (int columnIndex )
248+ {
249+ this .columnIndex = columnIndex ;
250+ }
251+
252+ @ Override
253+ public IndexProto .IndexKey get (int rowIndex )
254+ {
255+ if (rowIndex < 0 || rowIndex >= originalData .size ())
256+ {
257+ throw new IndexOutOfBoundsException ("Row index out of bounds: " + rowIndex );
258+ }
259+ return indexExtractor .apply (originalData .get (rowIndex )).get (columnIndex );
260+ }
261+
262+ @ Override
263+ public int size ()
264+ {
265+ return originalData .size ();
266+ }
267+ }
268+ }
269+
270+
202271 /**
203272 * Transpose the index keys from a row set to a column set.
204273 * @param dataList
@@ -214,17 +283,7 @@ private <T> List<List<IndexProto.IndexKey>> transposeIndexKeys(List<T> dataList,
214283 return Collections .emptyList ();
215284 }
216285
217- int indexNum = indexExtractor .apply (dataList .get (0 )).size ();
218- if (indexNum == 0 )
219- {
220- return Collections .emptyList ();
221- }
222-
223- return IntStream .range (0 , indexNum )
224- .mapToObj (i -> dataList .stream ()
225- .map (data -> indexExtractor .apply (data ).get (i ))
226- .collect (Collectors .toList ()))
227- .collect (Collectors .toList ());
286+ return new TransposedIndexKeyView <>(dataList , indexExtractor );
228287 }
229288
230289 /**
0 commit comments