变换通用阵列有效地利用番石榴 [英] Transform a Generic Array Efficiently Using Guava

查看:117
本文介绍了变换通用阵列有效地利用番石榴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作的 Java 7中,然后我搜索番石榴API的方式到功能应用到阵列中,而无需先将其转换成一个Collection。我愿意创建自己的用于该目的的类,但我不希望推倒重来嘿嘿。

I'm working with Java 7, and I'm searching in the Guava API for a way to apply a function to an array without having to convert it to a Collection first. I'm willing to create my own class for such purpose, but I don't want to reinvent the wheel hehe.

因此​​,作为一个汇总(如果你不知道什么我说的),这就是我发现到目前为止,您可以用番石榴,以功能应用到一个数组和我一样说:

So as a summary (in case you don't know exactly what I'm talking about), this is what I've found so far that you can do with Guava in order to apply a function to an array as I said:

Integer[] someNumbers = new Integer[]{1, 2, 3};
Integer[] returnedNumbers = Collections2.transform(Arrays.asList(someNumbers), squareNumberFunction).toArray(new Integer[0]);

assertThat(returnedNumbers).isEqualTo(new Integer[]{1, 4, 9});//Using AssertJ here

不过,我想能够做这样的事情,而不是:

But I would like to be able to do something like this instead:

Integer[] someNumbers = new Integer[]{1, 2, 3};
Integer[] returnedNumbers = Arrays.transform(someNumbers, squareNumberFunction);

assertThat(returnedNumbers).isEqualTo(new Integer[]{1, 4, 9});

在理想情况下,我谈论的功能将类型安全

修改

有关进一步澄清的问题:

For even further clarification of the problem:


  • 我谈论的数组不是原始的阵列,它们引用复杂的对象(我只用整数轻松地举例说明我说的)。

  • 我在接受发送结构没有控制权,他们的阵列(想象一个传统code情况下这是可能的如果你认为可以帮助你理解这个问题更好)。

  • 效率是一个必须转化阵列和访问他们。

  • The arrays I'm talking about are not primitive arrays, they reference complex objects (I only used integers to easily exemplify what I was talking about).
  • I have no control over the received or send structures, they are arrays (imagine a legacy code situation where that's possible if you think that helps you understand the problem better).
  • Efficiency is a must when transforming the arrays and accessing them.

推荐答案

好吧,我结束了写我自己的code,以解决上述问题。所以,我希望别人发现它太有用了,不只是要诉诸使用集合(来吧,你为什么会得?)。

Okay, I ended up writing my own code in order to solve the aforementioned problem. So I hope someone else find it useful too, and don't just have to resort to using collections (come on, why would you have to?).

public static <A, B> B[] transform(Class<B> theReturnedValueType, Function<A, B> functionToApply, A... theValues) {

    B[] transformedValues = (B[]) Array.newInstance(theReturnedValueType, theValues.length);

    for (int i = 0; i < theValues.length; i++) {
        transformedValues[i] = functionToApply.apply(theValues[i]);
    }

    return transformedValues;
}

的如何使用它(实际上这是一个测试)实施例:

Example of how to use it (actually this is a test):

Integer[] returnedNumbers = ArrayTransformer.transform(Integer.class, squareNumberFunction, 1, 2, 3);

assertThat(returnedNumbers).isEqualTo(new Integer[]{1, 4, 9});

我要指出的是,例如code使用vargargs语法(这是一个考验,所以它的使用它一个有趣的方式),但你可以明显地传递一个数组对象来代替。

I want to point out that the example code is using vargargs syntax (it's a test, so it's a fun way to use it) but you can just obviously passed an array object instead.

另外,我要补充的是,你要小心,这种方法使用上反射性能命中(如Android)敏感的环境中时,它,因为在运行时创建新的使用反射阵列(java.lang中的。 reflect.newInstance(...))。在另一方面,如果你正在使用的toArray集合(T [])方法和传递的数组的大小不足以容纳所有的集合元素(在这种情况下,一个新的数组是在运行时分配的也是如此,就像我的code),所以也许如果反正你使用的是在我说的方式,方法(在任何情况下,你可以很容易地改变这个$ C此code不会重新present问题$ C,以满足您的需求)。

Also, I must add that you need to be careful with this approach when using it on an environment sensitive to reflection performance hits (such as Android), because of the runtime creation of the new array using reflection (java.lang.reflect.newInstance(...)). In the other hand, the same holds true if you are using toArray(T[]) method of collections and the passed array's size isn't enough to hold all the collection elements (in which case a new array is allocated at run time, just as with my code), so maybe this code doesn't represent a problem if anyway you are using that method in the way I said (in any case you could easily change this code to fit your needs).

这篇关于变换通用阵列有效地利用番石榴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆