代码之家  ›  专栏  ›  技术社区  ›  Markus

如何在没有任何循环的情况下修改集合值以获得新集合?

  •  2
  • Markus  · 技术社区  · 17 年前

    例如,我有一个 Collection<String>

    通过循环,我可以这样做:

    Iterable<String> collection = getCollection();
    ArrayList<String> newCollection = new ArrayList<String>();    
    for(String str : collection) 
        newCollection.add("(" + str + ")");
    

    必须有一个更优雅的解决方案。

    编辑:允许使用第三方实用程序:)

    9 回复  |  直到 17 年前
        1
  •  7
  •   Community Mohan Dere    9 年前

    不,没有。仅使用JDK类是做不到更好的。 Check my second answer

    编辑

    试试谷歌收藏库 Lists.trasform :

    List<String> out = Lists.transform(in, new Function<String, String>() {
    
        public String apply(String s) {
            return String.format("(%s)", s);
        }
    });
    
        2
  •  2
  •   MarrLiss    17 年前
        3
  •  2
  •   Draemon    17 年前

    在我看来,您似乎在尝试将函数式习惯用法用于Java。虽然这可以通过使用第三方libs和functor实现(正如其他人所建议的),但我建议使用Java习惯用法或使用函数式语言编程Java。如果您想要一种与JVM兼容的函数式语言,请查看 Scala .

        4
  •  2
  •   dfa    17 年前

    使用 Apache Commons CollectionUtils :

    List l = someList;
    CollectionUtils.transform(l, new Transformer() {
        public Object transform(Object o)
        {
            String s = (String) o;
            return "(" + s + ")";
        }
    });
    
        5
  •  1
  •   yawn    17 年前

    你想要一杯吗 Map function . 这些文件(除其他外)在 functional java . 示例代码:

    import fj.F;
    import fj.data.List;
    import static fj.data.List.*;
    import static fj.pre.Show.listShow;
    import static fj.pre.Show.stringShow;
    
    import static java.lang.String.format;
    
    public class Test {
    
        public static void main(String[] args) {
    
            List<String> a = list("a", "b", "c");
            List<String> b = a.map(new F<String, String>() {
    
                public String f(String s) {
                    return format("(%s)", s);
                }
    
            });
    
            listShow(stringShow).println(a);
            listShow(stringShow).println(b);
    
        }
    
    }
    
        6
  •  1
  •   Peter Lawrey    17 年前

    在不使用任何循环的情况下,如何修改集合中的值以获得具有修改后的值的新集合?

    你为什么要这么做?由于Java没有闭包,因此闭包通常更加复杂、非标准且更难阅读。

    您将注意到,您的示例比其他解决方案更短、更简单,并且它使用标准java。

    顺便说一句:循环仍然被使用,只是不是你直接使用的,所以这种区别只是一种错觉。

        7
  •  1
  •   Mario Fusco    17 年前

    查看lambdaj库

    http://code.google.com/p/lambdaj/

    您将发现大量的方法以DSL风格操作集合,而无需编写单个显式循环

        8
  •  0
  •   user156178    17 年前

    这实际上是Java的一个不足,已经讨论了很多。在许多语言中,使用闭包(或代码块)只需一行代码就可以做到这一点。例如,在Groovy中,可以按如下方式编写:

    newCollection = collection.collect { "($it)" }
    

    我希望,我们可以在Java7中实现这一点。

        9
  •  0
  •   Community Mohan Dere    9 年前

    my previous answer was functional oriented ,现在,我在做“波莫多罗意大利面”,我在想一个更有趣的主意 面向对象

    对于一个匿名类实现,它用一个围绕()的元素来覆盖它的迭代器,您怎么看?

    public static <T> Collection<String> wrap(final Collection<T> collection) {
        final Iterator<T> iterator = collection.iterator();
        return new AbstractCollection<String>() {
    
            @Override
            public int size() {
                return collection.size();
            }
    
            @Override
            public Iterator<String> iterator() {
                return new Iterator<String>() {
    
                    public boolean hasNext() {
                        return iterator.hasNext();
                    }
    
                    public String next() {
                        return String.format("(%s)", iterator.next());
                    }
    
                    public void remove() {
                        iterator.remove();
                    }
                };
            }
        };
    }
    

    演示

    List<String> in = Arrays.asList("aaa","bbb");
    List<String> out = new ArrayList<String>(wrap(in));
    System.out.println(out);
    

    [(aaa), (bbb)]
    

    顺便说一句,这只使用JDK类,不使用循环!