Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Basically, you create a Function interface:</p> <pre><code>public interface Func&lt;In, Out&gt; { public Out apply(In in); } </code></pre> <p>and then pass in an anonymous subclass to your method.</p> <p>Your method could either apply the function to each element in-place:</p> <pre><code>public static &lt;T&gt; void applyToListInPlace(List&lt;T&gt; list, Func&lt;T, T&gt; f) { ListIterator&lt;T&gt; itr = list.listIterator(); while (itr.hasNext()) { T output = f.apply(itr.next()); itr.set(output); } } // ... List&lt;String&gt; myList = ...; applyToListInPlace(myList, new Func&lt;String, String&gt;() { public String apply(String in) { return in.toLowerCase(); } }); </code></pre> <p>or create a new <code>List</code> (basically creating a mapping from the input list to the output list):</p> <pre><code>public static &lt;In, Out&gt; List&lt;Out&gt; map(List&lt;In&gt; in, Func&lt;In, Out&gt; f) { List&lt;Out&gt; out = new ArrayList&lt;Out&gt;(in.size()); for (In inObj : in) { out.add(f.apply(inObj)); } return out; } // ... List&lt;String&gt; myList = ...; List&lt;String&gt; lowerCased = map(myList, new Func&lt;String, String&gt;() { public String apply(String in) { return in.toLowerCase(); } }); </code></pre> <p>Which one is preferable depends on your use case. If your list is extremely large, the in-place solution may be the only viable one; if you wish to apply many different functions to the same original list to make many derivative lists, you will want the <code>map</code> version.</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. 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