开发者

Does Google Collections API have an equivalent of the Ruby Enumerable#inject method?

开发者 https://www.devze.com 2022-12-24 08:57 出处:网络
I read through the javadoc and couldn\'t fin开发者_如何学运维d anything that resembles it.No, it does not.

I read through the javadoc and couldn't fin开发者_如何学运维d anything that resembles it.


No, it does not.

While it does have certain functional programming elements (Predicate, Function), those were to support specific needs and its core focus is not adding functional programming elements for Java (seeing as how it's terribly verbose currently). See this issue for a bit on that.


I think that you don't have a exact inject method.. but you can obtain a similar solution by using the transformValues methods supplied

Maps.transformValues(Map<K,V1> fromMap, Function<? super V1,V2> function)
List.transform(List<F> fromList, Function<? super F,? extends T> function)

Of course you'll need a Function class defined ad hoc to work with the passed parameter of the inject:

class MyFunction<Type, Type>
{
  static String variable;

  Type apply(Type t)
  {
     //do whatever you want with t
     // and storing intermediate result to variable

     // return same t to make this function work like identity
     return t;
  }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号