简体   繁体   中英

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:

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.

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 underlying List 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM