Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何转换列表<;firstType>;列出<;第二种类型>;?(Apache CollectionUtils)_Java_Apache - Fatal编程技术网

Java 如何转换列表<;firstType>;列出<;第二种类型>;?(Apache CollectionUtils)

Java 如何转换列表<;firstType>;列出<;第二种类型>;?(Apache CollectionUtils),java,apache,Java,Apache,我需要创建方法transformer,它可以更改对象的类型。 例如:我有List sample以及对List sample的更改。 如何编写方法来做到这一点?我真的不明白,因为我知道apache已经有了rhis方法->CollectionUtils.transform();(full-way-org.apache.commons.collections4.CollectionUtils.transform();,但我不明白它是如何工作的,以及如何编写方法whjich include all( ,

我需要创建方法
transformer
,它可以更改对象的类型。 例如:我有
List sampl
e以及对
List sample的更改。
如何编写方法来做到这一点?我真的不明白,因为我知道apache已经有了rhis方法->
CollectionUtils.transform();(full-way-org.apache.commons.collections4.CollectionUtils.transform();
,但我不明白它是如何工作的,以及如何编写方法whjich include all( , )我看到了在主要方法中人们是如何覆盖rhis方法的,但如何编写方法以及如何使用它?

根据您的链接(),您需要使用
CollectionUtils.collect
方法,并将
Transformer
作为该方法的第二个参数。我猜
collect
函数迭代元素,并为每个元素调用
transform
方法,该方法从类型a获取元素并从类型B返回元素。 总的来说,它看起来是这样的:

List<String>listOfString = CollectionUtils.collect(listOfIntegers, new 
    Transformer<Integer, String>() {
    public String transform(Integer numbers) {
   //Do whatever you need to transform it to a String and return the String.
     return number.toString() // a suggestion
    }
});
listOfNum.stream().map(integer -> integer.toString()).collect(Collectors.toList());