Java collection/map apply method equivalent?

后端 未结 8 555
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 19:27

I would like to apply a function to a Java collection, in this particular case a map. Is there a nice way to do this? I have a map and would like to just run trim() on all t

8条回答
  •  一生所求
    2020-12-13 20:02

    Might be overkill for something like this, but there are a number of really good utilities for these types of problems in the Apache Commons Collections library.

    Map map = new HashMap(); 
    map.put("key1", "a  ");
    map.put("key2", " b ");
    map.put("key3", "  c");
    
    TransformedMap.decorateTransform(map, 
      TransformerUtils.nopTransformer(), 
      TransformerUtils.invokerTransformer("trim"));
    

    I highly recommend the Jakarta Commons Cookbook from O'Reilly.

提交回复
热议问题