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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7447002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:00:36  来源:igfitidea点击:

Is it possible to apply a function to a collection using Java Guava?

javacollectionsguava

提问by Garrett Hall

I want to apply a function to a collection, map, etc, using Guava.

我想使用 Guava 将函数应用于集合、地图等。

Basically, I need to resize the rows and columns of a Tableseparately so all rows and columns are of equal size, doing something like this:

基本上,我需要分别调整 a 的行和列的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);
}

I already have an idea of what the ResizeFunctionshould be. However, I need to applyit, not just return a Collection.

我已经知道ResizeFunction应该是什么了。但是,我需要应用它,而不仅仅是返回一个Collection.

回答by Sean Patrick Floyd

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

在 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);

Output:

输出:

[racecar, boxcar]

[赛车、棚车]

Or, if you don't need a Listand a Collectionwill do, you can use a transformed live view:

或者,如果您不需要 a Listand a Collectionwill do,则可以使用转换后的实时视图:

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 Collectionis a live view of the underlying one, so changes to listwill be reflected in this Collection.

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

回答by Garrett Hall

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);
}

回答by Premraj

Sean has already mentioned that Guava don't change the original collection, so you can't really "apply" a function on your existing collection.

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

It's not clear what your ResizeFunctionfunction does, if you only changing the value of Cellin Table<Integer, Integer, Cell>then you can use Tables#transformValues()

不清楚你的ResizeFunction函数是做什么的,如果你只改变Cellin的值,Table<Integer, Integer, Cell>那么你可以使用Tables#transformValues()

Guava don't allow you to change the values of Rand Cin Table<R, C, V>(in standard Tablesclass) 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.

Guava 不允许您更改Rand Cin Table<R, C, V>(在标准Tables类中)的值,因为它们在返回行或列映射(Table#rowMap()and Table#columnMap())时用作键,并且您无法转换它们,因为所有 Guava 的转换和过滤方法都会产生惰性结果意味着函数/谓词仅在需要时应用,因为使用对象。他们不创建副本。正因为如此,转换很容易打破 Set 的要求。

If you still want to do it then you can wrap Tableobject in your own class and provide a required methods.

如果您仍然想这样做,那么您可以将Table对象包装在您自己的类中并提供所需的方法。

回答by John B

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.

这将确保表格中的每个位置都有一个单元格。只要您仅将单元格添加到 numRows、numColumns 内的行、列位置,您就会保留一个“方形”表格。