title: Java-How to implement a custom map(Function<? super T, ? extends R> mapper) that can transform values of a Map given a function
body: <p>I have a custom sorted map. I want to transform each value in the map using map function. I know I can achieve this using java 8 stream().map(function). But I want to understand how the above map(function) works and write a custom function. </p>
  
  <p>I have read several articles but I have not found one that is explaining clearing. </p>
  
  <p>How I am doing it:</p>
  
  <pre><code>SortedMap&lt;String, Integer&gt; items = new TreeMap();//TreeMap is a custom implementation
          items.put("item1", 2);
          items.put("item2", 1);
  
        SortedMap&lt;String, Integer&gt; transformedItems=items.entrySet().stream().collect(
               Collectors.toMap(
                     ( Map.Entry&lt;String, Integer&gt; entry)-&gt;entry.getKey(),//pick each key as it is
                       ( Map.Entry&lt;String, Integer&gt; entry)-&gt;entry.getValue()*2, //transform the value here.
                       (val1, val2) -&gt; { throw new RuntimeException("Not expecting duplicate keys"); },
                       ()-&gt;new TreeMap&lt;&gt;()
  </code></pre>
  
  <p>How I want to do it:</p>
  
  <pre><code>SortedMap&lt;String, Integer&gt; items = new TreeMap();
          items.put("item1", 2);
          items.put("item2", 1);
  
        SortedMap&lt;String, Integer&gt; transformedItems=items.customMap(value-&gt;value*2)
  </code></pre>
  
  <p>Challenge here is to write the customMap(function)</p>