本文介绍了如何避免在番石榴中使用lalsily Lists.transform?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Map<String,Object> map = Maps.newHashMap();
map.put("test","123");
map.put("fuyou001","456");
map.put("id",145);
List<Map<String,Object>> list = Lists.newArrayList();
list.add(map);
Lists.transform(list, new Function<Map<String, Object>, Object>() {
  @Override
  public Object apply(@Nullable Map<String, Object> input) {
    System.out.println("test:" + input);
    return input;
  }
});
System.out.println(list);`

控制台未显示test。 ......

Console is not display "test...."

如何避免懒惰申请

我也试试

how to avoid applied lazily
I also try

List<Map<String,Object>> newList = new ArrayList<Map<String, Object>>(list.size());
Collections.copy(newList,list);

但不影响

推荐答案

功能一般不应有副作用; 。这是你真正的问题

Functions in general should not have side effects; that's your real problem.

这是说,如果你坚持立即应用改造,做一个副本: Lists.newArrayList(Lists.transform( list,function))

That said, if you insist on applying the transformation immediately, do a copy: Lists.newArrayList(Lists.transform(list, function)).

这篇关于如何避免在番石榴中使用lalsily Lists.transform?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:39