简体   繁体   English

为什么我不能在Apache Commons中将Transformer设置为LazyList?

[英]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.

相关问题 ProGuard:警告:org.apache.commons.beanutils.BeanMap $ 2:找不到超类或接口org.apache.commons.collections.Transformer - ProGuard: Warning: org.apache.commons.beanutils.BeanMap$2: can't find superclass or interface org.apache.commons.collections.Transformer 为 LazyList Apache commons 找到的装饰方法 - Decorate method on found for LazyList Apache commons 为什么我不能使用org.apache.commons.lang.StringEscapeUtils将此包含字符的字符串转换为'和è? - Why I can't use the org.apache.commons.lang.StringEscapeUtils to convert this String containing character as &apos and &egrave? Gradle找不到Apache Commons - Gradle can't find Apache commons 无法导入Apache Commons Jars - Can't import Apache Commons Jars 为什么我不能建立Spring Data Commons? - Why can't I build Spring Data Commons? 使用org.apache.commons.httpclient时,我可以在命令行上设置代理吗? - Can I set the proxy on the command line when using org.apache.commons.httpclient? 为什么我无法导入org.apache.commons.lang.StringUtils? - How come I can't import org.apache.commons.lang.StringUtils? 我无法使用apache-commons-net TelnetClient禁用echo选项 - I can't disable echo option using apache-commons-net TelnetClient 在apache commons-configurations2中格式化XML输出/修改Transformer - Format XML output/modify Transformer in apache commons-configurations2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM