A new helper method Arrays.asArray

Louis Wasserman lowasser at google.com
Tue Jun 14 17:47:23 UTC 2016


Why would you not just write new Object[] {sort0, sort1, sort2, sort3}?

On Tue, Jun 14, 2016, 10:35 AM <timo.kinnunen at gmail.com> wrote:

>
> Hi,
>
> I have found that many times I need to write this simple helper method:
>
>         public static @SafeVarargs <T> T[] asArray(T… ts) { return ts; }
>
>
> I usually need this when I have several implementations I’m comparing and
> I want to change the code for observing one of them to observing two or
> more of them in sequence. I feel that in this case switching from operating
> on one object to operating on an unknown List implementation (from
> Arrays.asList) is a too drastic change when all I need is put a for-loop
> around some code and iterate.
>
> The code for which I have to write this method is often some variation of
> something like this:
>
>        Comparator<Object> sort1 = (x, y) -> (Integer) x - (Integer) y;
>        Comparator<Object> sort0 = (x, y) -> (int) (Math.pow((Integer) x,
> 2.0) - Math.pow((Integer) y, 2.0));
>
>        // Have to use a helper method here
>        Comparator<Object>[] sorts = asArray(sort0, sort1, sort2, sort3);
>
>
> Please consider and add this simple method to Arrays.
>
>
>
> --
> Have a nice day,
> Timo
>
> Sent from Mail for Windows 10
>
>


More information about the core-libs-dev mailing list