Package io.blt.util

Class Ctr

java.lang.Object
io.blt.util.Ctr

public final class Ctr extends Object
Static utility methods for operating on implementations of Collection and Map i.e. Containers.

For methods that return a modification of a passed container, the result will be of the same type if possible. This is accomplished using Obj.newInstanceOf(Object) and its limitations apply.

  • Method Details

    • transformValues

      public static <K, V, R, E extends Throwable> Map<K,R> transformValues(Map<K,V> source, ThrowingFunction<? super V,R,E> transform) throws E
      Returns a new Map containing the entries of source with transform applied to the values.

      If possible, the result is of the same type as the passed source map.

      e.g.
      
       var scores = Map.of("Louis", 95, "Greg", 92, "Mike", 71, "Phil", 86);
       var grades = Ctr.transformValues(scores, score -> {
           if (score >= 90) {
               return "A";
           } else if (score >= 80) {
               return "B";
           } else if (score >= 70) {
               return "C";
           } else if (score >= 60) {
               return "D";
           } else {
               return "F";
           }
       });
       // grades = Map.of("Louis", "A", "Greg", "A", "Mike", "C", "Phil", "B")
       
      Type Parameters:
      K - map key type
      V - map value type
      R - returned map value type
      E - type of transform throwable
      Parameters:
      source - Map whose values should be transformed
      transform - value transformation function
      Returns:
      a new Map containing the entries of source with transform applied to the values
      Throws:
      E extends Throwable
      See Also: