简体   繁体   English

在Java中转换通用Collection的类型

[英]Converting the type of a generic Collection in Java

In several spots in my code I have ArrayLists and TreeSets whose generic type I wish to convert. 在我的代码中的几个地方,我有ArrayLists和TreeSets我希望转换其通用类型。 So for example I have an ArrayList<Integer> which I wish to convert to an ArrayList<Long> . 因此,例如,我有一个ArrayList<Integer> ,我希望将其转换为ArrayList<Long> Or I have a TreeSet<BigInteger> which I wish to convert to a TreeSet<String> . 或者我有一个TreeSet<BigInteger> ,我希望将其转换为TreeSet<String>

All of these conversions can be made, but then I have to create for each type conversion a different function. 可以进行所有这些转换,但是随后我必须为每种类型转换创建一个不同的函数。 Therefore I want to create a generic function whose signature looks something like this: 因此,我想创建一个通用函数,其签名如下所示:

public static <Q,T> Collection<Q> convert(Collection<T> col, Class<Q> Q)

What I want is to get the class from col (eg ArrayList ), create a new collection of that class and type Q (called newCol ), and then iterate through col and convert each element which is of type T to type Q and add it to newCol and lastly return newCol . 我想要的是从col (例如ArrayList )获取类,创建该类和Q类型的新集合(称为newCol ),然后遍历col并将每个T类型的元素转换为Q类型并添加它到newCol ,最后返回newCol

How can I do this? 我怎样才能做到这一点?

There's no special mechanism like casting of incompatible classes in Java. 没有像Java中强制转换不兼容类这样的特殊机制。 You need to specify an explicit function which will perform a conversion. 您需要指定一个显式函数来执行转换。 Using Java 8 it's really easy: 使用Java 8确实很容易:

public static <Q,T,C extends Collection<Q>> C convert(Collection<T> col, Function<T, Q> fn, 
                   Supplier<C> supplier) {
    return col.stream().map(fn).collect(Collectors.toCollection(supplier));
}

Use it like this: 像这样使用它:

TreeSet<BigInteger> values = // fill them somehow
TreeSet<String> converted = convert(values, BigInteger::toString, TreeSet::new);

@Tagir Valeev is right. @Tagir Valeev是正确的。 You can do it easily in Java 8. But if you use Java 7, you can try to do something like this: 您可以在Java 8中轻松完成此操作。但是,如果您使用Java 7,则可以尝试执行以下操作:

    public static <F, T> Collection<T> transform(Collection<F> fromCollection, Function<? super F, T> function) {
        return new TransformedCollection<F, T>(fromCollection, function);
    }

    static class TransformedCollection<F, T> extends AbstractCollection<T> {
        final Collection<F> fromCollection;
        final Function<? super F, ? extends T> function;

        TransformedCollection(Collection<F> fromCollection, Function<? super F, ? extends T> function) {
            this.fromCollection = checkNotNull(fromCollection);
            this.function = checkNotNull(function);
        }

        @Override public void clear() {
            fromCollection.clear();
        }

        @Override public boolean isEmpty() {
            return fromCollection.isEmpty();
        }

        @Override public Iterator<T> iterator() {
            return Iterators.transform(fromCollection.iterator(), function);
        }

        @Override public int size() {
            return fromCollection.size();
        }
    }

It's code from Guava library. 它是Guava库中的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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