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());