用于列表清理的Java一行代码

时间:2010-09-21 12:10:25

标签: java python collections closures

在java中是否有一个类似这样的构造(这里用python实现):

[] = [item for item in oldList if item.getInt() > 5]

今天我使用的是:

ItemType newList = new ArrayList();
for( ItemType item : oldList ) {
    if( item.getInt > 5) {
     newList.add(item);
    }
}

对我而言,第一种方式看起来更聪明。

5 个答案:

答案 0 :(得分:5)

Java 7 mightmight not实现闭包,因此支持这样的功能,但目前它没有,所以在Java VM上你可以选择在Groovy中执行它,ScalaClojure(也可能是其他人),但在java中,你只能使用像Guava Collections2.filter()这样的助手来接近它。

JDK 7示例代码:

findItemsLargerThan(List<Integer> l, int what){
   return filter(boolean(Integer x) { x > what }, l);
}  
findItemsLargerThan(Arrays.asList(1,2,5,6,9), 5)

Groovy示例代码:

Arrays.asList(1,2,5,6,9).findAll{ it > 5}

番石榴样本代码:

Collections2.filter(Arrays.asList(1, 2, 5, 6, 9),
    new Predicate<Integer>(){
        @Override
        public boolean apply(final Integer input){
            return input.intValue() > 5;
        }
    }
);

Scala示例代码(感谢Bolo):

Array(1, 2, 5, 6, 9) filter (x => x > 5)

答案 1 :(得分:3)

您可以查看lambdaj。您可以使用hamcrest条件选择一种方法。

答案 2 :(得分:1)

没有什么是不可能的( - :

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ListCleaner {

    public static void main(String[] args) {

        final List<Integer> oldList = Arrays.asList(new Integer[] { 23, 4, 5,
                657 });
        System.out.println(oldList);

        List<Integer> newList = new ArrayList<Integer>() {
            {
                for (Integer element : oldList) {
                    if (element > 5) {
                        this.add(element);
                    }
                }
            }

        };
        System.out.println(newList);
    }
}

唯一的限制是oldList必须是最终的。

答案 3 :(得分:1)

它可以在纯Java中完成,但您需要编写一个支持类Filter,以便以下代码成功运行:

    List<Integer> oldList = Arrays.asList(new Integer[] { 1, 2, 5, 6, 9 });
    List<Integer> newList = new Filter<Integer>(oldList) {
        {
            findAll(it > 5);
        }
    }.values();
    System.out.println(newList); // [6, 9] 

如果你想知道为什么这个代码编译看看Hidden features of Java:双括号初始化。这将创建包含it变量的类Filter的匿名实例,并提供方法findAll()。

Filter类本身有一个缺点,就是为每个列表元素创建一个新实例来评估findAll()的布尔条件:

public abstract class Filter<T> {

    protected List<T> values = new ArrayList<T>();

    protected T it;

    public Filter(List<T> values) {
        if (values != null) {
            this.values.addAll(values);
        }
        if (values.isEmpty()) {
            throw new RuntimeException("Not for empty collections!");
        }
        it = values.iterator().next();
        // instance initializer gets executed here, calls findAll
    }

    protected void findAll(boolean b) throws Throwable {
        // exit condition for future calls
        if (values.size() > 1) {
            // only repeat for each entry, if values has multiple entries
            Iterator<T> iterator = values.iterator();
            while (iterator.hasNext()) {
                // don't evalute again for the first entry
                if (!b) {
                    iterator.next();
                    iterator.remove();
                    b = true;
                } else {
                    // for each other entry create an argument with one element
                    List<T> next = new ArrayList<T>();
                    next.add(iterator.next());
                    // get constructor of anonymous class
                    Constructor<?> constructor = this.getClass().getDeclaredConstructors()[0];
                    // invoke constructor and thus execute instance initializer again
                    Filter<T> filtered = (Filter<T>) constructor.newInstance(new Object[] { null, next });
                    // if values is empty, the condition didn't match and the element can be removed
                    if (filtered.values.isEmpty()) {
                        iterator.remove();
                    }
                }
            }
        } else {
            // one element can be checked directly
            if (!b) {
                values.clear();
            }
        }
    }

    public List<T> values() {
        return values;
    }

}

但是由于实例创建现在相当便宜,并且Filter类可用于所有对象,因此可能值得包含在Utils包中。

格尔茨, GHAD

答案 4 :(得分:0)

不,Java还不支持这种动态语言结构:-)所以你必须忍受你的选择2

相关问题