[英]Why can't I set a Transformer to a LazyList in Apache Commons?
In apache.commons.collections there is a class called MapUtils which have these two methods to define a Map which can create on demand objects for the map: 在apache.commons.collections中,有一个名为MapUtils的类,它具有以下两种方法来定义Map,该Map可以为该地图创建按需对象:
So I can use a factory to instantiate the object 所以我可以使用工厂实例化对象
Factory factory = new Factory() {
public Object create() {
return new Object();
}
}
or a transformer to instantiate the new object depending on the key of the map 或转换器根据地图的键实例化新对象
Transformer factory = new Transformer() {
public Object transform(Object mapKey) {
return new Object(mapKey);
}
}
There's a similar class for Lists: ListUtils , but this class only has a method with a Factory: Lists有一个类似的类: ListUtils ,但是该类只有一个带有Factory的方法:
I'd like to transform the object like in the map situation but using the index of the object in the list instead of the key in the map. 我想像在地图情况下那样转换对象,但使用列表中对象的索引而不是地图中的键。
Transformer factory = new Transformer() {
public Object transform(int index) {
return new Object(index);
}
}
My question is why there is not a lazyList(List list, Transformer transformer)? 我的问题是为什么没有lazyList(列表列表,Transformer转换器)? Does apache provide any other List to do this or do I have to build my custom implementation?
apache是否提供其他列表来执行此操作,还是我必须构建自定义实现?
Thanks. 谢谢。
First of all, in my opinion you should use Guava for this sort of thing... it makes full use of generics and provides a much more well thought-out, compact and sensible API. 首先,在我看来,您应该将Guava用于此类事情……它充分利用了泛型,并提供了经过深思熟虑,紧凑且明智的API。 It also provides a Lists.transform method which transforms an underlying
List
based on the elements at each position in the list. 它还提供了Lists.transform方法,该方法根据
List
中每个位置的元素来转换基础List
。
That said, I don't think a transform method for transforming a List
by index makes much sense. 就是说,我认为通过索引转换
List
的转换方法没有多大意义。 The actual underlying List
would be completely meaningless given that the transformation would ignore the elements it contains... only its size would matter. 考虑到转换将忽略其包含的元素,因此实际的基础
List
将完全没有意义……只有其大小才重要。 Could you explain why you would want to do something like that? 您能解释一下为什么要这样做吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.