Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are basically two useful libraries that can help with this; <a href="http://guava-libraries.googlecode.com/" rel="nofollow noreferrer">Google Guava</a> and <a href="http://commons.apache.org/collections/" rel="nofollow noreferrer">Apache Commons Collections</a>.</p> <p>What you are trying to do is basically two operations, first mapping, then reduction. I've never used Commons Collections to any extent myself so I can't tell you more about that, but I know there is no support for reduction (or folding) in Google Guava at least (see <a href="http://code.google.com/p/guava-libraries/issues/detail?id=218" rel="nofollow noreferrer">Issue 218</a>). This is not too hard to add yourself though (not tested):</p> <pre><code>interface Function2&lt;A, B&gt; { B apply(B b, A a); } public class Iterables2 { public static &lt;A, B&gt; B reduce(Iterable&lt;A&gt; iterable, B initial, Function2&lt;A, B&gt; fun) { B b = initial; for (A item : iterable) b = fun.apply(b, item); return b; } } </code></pre> <p>That way you can combine it with Guavas Iterables.transform() like so:</p> <pre><code>class Summer implements Function2&lt;Integer, Integer&gt; { Integer apply(Integer b, Integer a) { return b + a; } } class MyMapper&lt;T&gt; implements Function&lt;T, Integer&gt; { Integer apply(T t) { // Do stuff } } </code></pre> <p>And then (provided you've import static'ed the relevant classes):</p> <pre><code>reduce(transform(iterable, new MyMapper()), 0, new Summer()); </code></pre> <p>Also see <a href="https://stackoverflow.com/questions/950751/how-to-implement-a-list-fold-in-java">this question</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload