I want to apply a function to a collection, map, etc, using Guava.
Basically, I need to resize the rows and columns of a Table separately so all rows and columns are of equal size, doing something like this:
Table<Integer, Integer, Cell> table = HashBasedTable.create();
Maps.transformValues(table.columnMap(), new ResizeFunction(BlockDimension.WIDTH));
Maps.transformValues(table.rowMap(), new ResizeFunction(BlockDimension.HEIGHT));
public interface Cell {
int getSize(BlockDimension dimension);
void setSize(BlockDimension dimension);
}
I already have an idea of what the ResizeFunction should be. However, I need to apply it, not just return a Collection.
In Guava, you don’t convert existing Lists, but instead create a new one using Iterables.transform:
Output:
Or, if you don’t need a
Listand aCollectionwill do, you can use a transformed live view:This
Collectionis a live view of the underlying one, so changes tolistwill be reflected in thisCollection.