Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to create an interface that provides the function(s) that you want to pass around. eg:</p> <pre><code>/** * A simple interface to wrap up a function of one argument. * * @author rcreswick * */ public interface Function1&lt;S, T&gt; { /** * Evaluates this function on it's arguments. * * @param a The first argument. * @return The result. */ public S eval(T a); } </code></pre> <p>Then, when you need to pass a function, you can implement that interface:</p> <pre><code>List&lt;Integer&gt; result = CollectionUtilities.map(list, new Function1&lt;Integer, Integer&gt;() { @Override public Integer eval(Integer a) { return a * a; } }); </code></pre> <p>Finally, the map function uses the passed in Function1 as follows:</p> <pre><code> public static &lt;K,R,S,T&gt; Map&lt;K, R&gt; zipWith(Function2&lt;R,S,T&gt; fn, Map&lt;K, S&gt; m1, Map&lt;K, T&gt; m2, Map&lt;K, R&gt; results){ Set&lt;K&gt; keySet = new HashSet&lt;K&gt;(); keySet.addAll(m1.keySet()); keySet.addAll(m2.keySet()); results.clear(); for (K key : keySet) { results.put(key, fn.eval(m1.get(key), m2.get(key))); } return results; } </code></pre> <p>You can often use Runnable instead of your own interface if you don't need to pass in parameters, or you can use various other techniques to make the param count less "fixed" but it's usually a trade-off with type safety. (Or you can override the constructor for your function object to pass in the params that way.. there are lots of approaches, and some work better in certain circumstances.)</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.
 

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