是否可以使用 Java Guava 将函数应用于集合?
我想使用 Guava 将函数应用于集合、地图等。
基本上,我需要分别调整 Table
的行和列的大小,以便所有行和列的大小相等,执行如下操作:
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);
}
我已经了解了 ResizeFunction
代码> 应该是。但是,我需要应用它,而不仅仅是返回Collection
。
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Guava 中,您不转换现有列表,而是使用 Iterables.transform 创建一个新列表:
输出:
或者,如果您不需要
List
而需要Collection
,您可以使用转换后的实时视图:此
Collection
code> 是底层视图的实时视图,因此对list
的更改将反映在此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
List
and aCollection
will do, you can use a transformed live view:This
Collection
is a live view of the underlying one, so changes tolist
will be reflected in thisCollection
.创建一个这样的函数怎么样:
What about creating a function like this:
Sean 已经提到 Guava 不会更改原始集合,因此您无法真正在现有集合上“应用”功能。
如果您仅更改
Table
中Cell
的值,则不清楚您的ResizeFunction
函数的作用那么你可以使用 Tables#transformValues()Guava 不允许您更改
R
和Table
中的C
(在标准Tables
类中),因为它们在返回行或列映射时用作键(Table#rowMap()
和Table#columnMap()
),并且您无法转换它们,因为 Guava 的所有转换和过滤方法都会产生惰性结果,这意味着函数/谓词只是在使用对象时需要时应用。他们不创建副本。但正因为如此,转换很容易打破集合的要求。如果您仍然想这样做,那么您可以将
Table
对象包装在您自己的类中并提供所需的方法。Sean has already mentioned that Guava don't change the original collection, so you can't really "apply" a function on your existing collection.
It's not clear what your
ResizeFunction
function does, if you only changing the value ofCell
inTable<Integer, Integer, Cell>
then you can use Tables#transformValues()Guava don't allow you to change the values of
R
andC
inTable<R, C, V>
(in standardTables
class) because those are used as keys while returning row or column map (Table#rowMap()
andTable#columnMap()
) and you cannot transform those because all of Guava's methods for transforming and filtering produce lazy results means the function/predicate is only applied when needed as the object is used. They don't create copies. Because of that, though, a transformation can easily break the requirements of a Set.If you still want to do it then you can wrap
Table
object in your own class and provide a required methods.我认为最好在填充之前将表格设置为适当的大小/具有适当数量的单元格,即使它们是空的。类似于:
这将确保表中的每个位置都有一个单元格。只要您仅将单元格添加到 numRows、numColumns 内的行、列位置,您就会保留一个“方形”表格。
I think would would be better off setting up the table before population to be the appropriate size / have the appropriate number of cells even if they are empty. Something like:
This would ensure there is a cell for each position in your table. So long as you only add cells into row, column positions that are within numRows, numColumns you will keep a "square" table.