简体   繁体   中英

How to join two arrays of parametrized types (in guava)

I was looking for a concise way to iterate over two arrays. Since the arrays are not expected to be large I figured I could just concat them.

Unfortunately the Guava invocation looks horrible:

        Class<? extends MyInterface>[] a2 = ...
        Class<? extends MyInterface>[] a1 = ...

        ObjectArrays.concat(a1, a2,
                (Class<Class<? extends MyInterface>>) MyInterface.class.getClass());

Is it possible to make it more readable?

Instead of using ObjectArrays you can combine Arrays.asList and Iterables.concat . This way you don't need to provide a class name.

Iterables.concat(Arrays.asList(a1), Arrays.asList(a2))

It will be a lot more readable if you use static imports:

import static com.google.common.collect.Iterables.concat;
import static java.util.Arrays.asList;

...

concat(asList(a1), asList(a2))

I ended up writing something of my own.

There's a main method which does all the work:

@SuppressWarnings("unchecked")
private static <T> T[] mergeInternal(@Nonnull T[] first,
                            @Nonnull T[] second,
                            @Nullable T[] third,
                            @Nullable T[] fourth,
                            @Nullable T[] fifth,
                            @Nullable T[] sixth) {
    int overallLength = first.length + second.length;
    if (third != null) {
        overallLength += third.length;
    }
    if (fourth != null) {
        overallLength += fourth.length;
    }
    if (fifth != null) {
        overallLength += fifth.length;
    }
    if (sixth != null) {
        overallLength += sixth.length;
    }

    Object[] joinedArray = (Object[]) Array.newInstance(first.getClass().getComponentType(), overallLength);
    System.arraycopy(first, 0, joinedArray, 0, first.length);
    System.arraycopy(second, 0, joinedArray, first.length, second.length);
    int copyTargetPosition = first.length + second.length;
    if (third != null) {
        System.arraycopy(third, 0, joinedArray, copyTargetPosition, third.length);
        copyTargetPosition += third.length;
    }
    if (fourth != null) {
        System.arraycopy(fourth, 0, joinedArray, copyTargetPosition, fourth.length);
        copyTargetPosition += fourth.length;
    }
    if (fifth != null) {
        System.arraycopy(fifth, 0, joinedArray, copyTargetPosition, fifth.length);
        copyTargetPosition += fifth.length;
    }
    if (sixth != null) {
        System.arraycopy(sixth, 0, joinedArray, copyTargetPosition, sixth.length);
    }
    return (T[]) joinedArray;
}

..and then there's an entry method for each combination of number of parameters (2..6), like so:

public static <T> T[] merge(@Nonnull T[] first, @Nonnull T[] second) {
    Preconditions.checkNotNull(first);
    Preconditions.checkNotNull(second);
    return mergeInternal(first, second, null, null, null, null);
}

public static <T> T[] merge(@Nonnull T[] first, @Nonnull T[] second, @Nonnull T[] third)
...
public static <T> T[] merge(@Nonnull T[] first, @Nonnull T[] second, @Nonnull T[] third, @Nonnull T[] fourth)

And so on.

I think one rarely needs to merge more than 6 arrays and if you need to, you can always easily extend the given idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM