Svi*_*ish 7 java collections properties
在C#中,我可以这样做:
IEnumerable<long> ids = things.select(x => x.Id);
Run Code Online (Sandbox Code Playgroud)
在Java中我必须这样做:
Collection<Long> ids = new ArrayList<Long>(things.size());
for(Thing x : things)
ids.add(x.getId());
Run Code Online (Sandbox Code Playgroud)
现在必须做很多这样的事情并且想知道在Java中是否有更通用的方法来做这件事.可以创建一个方法来做到这一点,但后来我将不得不添加一个接口与getId方法或类似的...我不能...
public class ThingFunction implements Function<Thing, Long> {
@Override
public Long apply(Thing thing) {
return user.getId();
}
}
Run Code Online (Sandbox Code Playgroud)
并像这样调用(其中transform是从番石榴的Collections2静态导入:
Collection<Long> ids = transform(things, new ThingFunction());
Run Code Online (Sandbox Code Playgroud)
番石榴还有很多其他好处。
| 归档时间: |
|
| 查看次数: |
3711 次 |
| 最近记录: |