是否可以使用 Java Guava 将函数应用于集合?

发布于 2024-12-05 06:09:04 字数 596 浏览 2 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淑女气质 2024-12-12 06:09:04

在 Guava 中,您不转换现有列表,而是使用 Iterables.transform 创建一个新列表:

final List<String> list = Arrays.asList("race", "box");
final List<String> transformed =
    Lists.newArrayList(Iterables.transform(list, new Function<String, String>() {

        @Override
        public String apply(final String input) {
            return new StringBuilder().append(input).append("car").toString();
        }
    }));
System.out.println(transformed);

输出:

[赛车、棚车]

或者,如果您不需要 List 而需要 Collection,您可以使用转换后的实时视图:

final Collection<String> transformed =
    Collections2.transform(list, new Function<String, String>() {

        @Override
        public String apply(final String input) {
            return new StringBuilder().append(input).append("car").toString();
        }
    });

Collection code> 是底层视图的实时视图,因此对 list 的更改将反映在此 Collection 中。

In Guava, you don't convert existing Lists, but instead create a new one using Iterables.transform:

final List<String> list = Arrays.asList("race", "box");
final List<String> transformed =
    Lists.newArrayList(Iterables.transform(list, new Function<String, String>() {

        @Override
        public String apply(final String input) {
            return new StringBuilder().append(input).append("car").toString();
        }
    }));
System.out.println(transformed);

Output:

[racecar, boxcar]

Or, if you don't need a List and a Collection will do, you can use a transformed live view:

final Collection<String> transformed =
    Collections2.transform(list, new Function<String, String>() {

        @Override
        public String apply(final String input) {
            return new StringBuilder().append(input).append("car").toString();
        }
    });

This Collection is a live view of the underlying one, so changes to list will be reflected in this Collection.

一杯敬自由 2024-12-12 06:09:04

创建一个这样的函数怎么样:

public static <T> void apply(Iterable<T> iterable, Function<T, Void> function) {
    for (T input : iterable)
        function.apply(input);
}

What about creating a function like this:

public static <T> void apply(Iterable<T> iterable, Function<T, Void> function) {
    for (T input : iterable)
        function.apply(input);
}
手长情犹 2024-12-12 06:09:04

Sean 已经提到 Guava 不会更改原始集合,因此您无法真正在现有集合上“应用”功能。

如果您仅更改 TableCell 的值,则不清楚您的 ResizeFunction 函数的作用那么你可以使用 Tables#transformValues()

Guava 不允许您更改 RTable 中的 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 of Cell in Table<Integer, Integer, Cell> then you can use Tables#transformValues()

Guava don't allow you to change the values of R and C in Table<R, C, V> (in standard Tables class) because those are used as keys while returning row or column map (Table#rowMap() and Table#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.

御守 2024-12-12 06:09:04

我认为最好在填充之前将表格设置为适当的大小/具有适当数量的单元格,即使它们是空的。类似于:

for (int i=0; i<numRows; i++)
   for (int j=0; j<numColumns; j++)
       table.put(i,j,null);

这将确保表中的每个位置都有一个单元格。只要您仅将单元格添加到 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:

for (int i=0; i<numRows; i++)
   for (int j=0; j<numColumns; j++)
       table.put(i,j,null);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文