为什么我无法在 Apache Commons 中将 Transformer 设置为 LazyList?
在 apache.commons.collections 中有一个名为 MapUtils< /a> 有以下两种方法来定义 Map,可以为地图创建按需对象:
所以我可以使用工厂来实例化对象
Factory factory = new Factory() {
public Object create() {
return new Object();
}
}
或使用转换器来实例化新对象,具体取决于地图的键
Transformer factory = new Transformer() {
public Object transform(Object mapKey) {
return new Object(mapKey);
}
}
列表有一个类似的类: ListUtils,但是这个类只有一个带有工厂的方法:
我想像在地图情况下一样转换对象,但使用列表中对象的索引而不是地图中的键。
Transformer factory = new Transformer() {
public Object transform(int index) {
return new Object(index);
}
}
我的问题是为什么没有一个lazyList(列表列表,变压器变压器)? apache 是否提供任何其他列表来执行此操作,或者我是否必须构建自定义实现?
谢谢。
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:
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:
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)? Does apache provide any other List to do this or do I have to build my custom implementation?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我认为你应该使用 Guava 来做这种事情。它充分利用了泛型,并提供了一个经过深思熟虑、紧凑且合理的 API。它还提供了 Lists.transform 方法,根据每个位置的元素转换底层
List
在列表中。也就是说,我认为通过 index 转换
List
的转换方法没有多大意义。实际的底层List
将完全没有意义,因为转换会忽略它包含的元素......只有它的大小才重要。你能解释一下为什么你想做这样的事情吗?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. It also provides a Lists.transform method which transforms an underlying
List
based on the elements at each position in the list.That said, I don't think a transform method for transforming a
List
by index makes much sense. The actual underlyingList
would be completely meaningless given that the transformation would ignore the elements it contains... only its size would matter. Could you explain why you would want to do something like that?