了解Lambda表达式

时间:2014-12-27 09:22:34

标签: java lambda java-8

我不太确定我是否理解Oracle Java教程中的Lambda表达式教程。令我困惑的主要问题是lambda的Index参数。ds.print(index ->{...}编译器如何知道甚至是什么值索引?索引未在程序中的任何其他位置声明,因此索引参数甚至引用甚至是什么,编译器如何知道?

有问题:

public class DataStructure {

    private final static int SIZE = 15;
    private int[] arrayOfInts = new int[SIZE];

    public DataStructure() {
        for (int i = 0; i < SIZE; i++) {
            arrayOfInts[i] = i;
        }
    }

    public int size() {
        return SIZE;
    }

    public int get(int index) {
        return arrayOfInts[index];
    }

    interface DataStructureIterator extends java.util.Iterator<Integer> { }

    private class EvenIterator implements DataStructureIterator {

        private int nextIndex = 0;

        public boolean hasNext() {
            return (nextIndex <= SIZE - 1);
        }

        public Integer next() {
            Integer retValue = Integer.valueOf(arrayOfInts[nextIndex]);
            nextIndex += 2;
            return retValue;
        }
    }

    public DataStructureIterator getEvenIterator() {
        return new EvenIterator();
    }

    public void printEven() {
        DataStructureIterator iterator = getEvenIterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
    }

    public void print(DataStructureIterator iterator) {
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
    }

    public void print(java.util.function.Function<Integer, Boolean> function) {
        for (int i = 0; i < SIZE; i++) {
            if (function.apply(i)) {
                System.out.print(arrayOfInts[i] + " ");
            }
        }
        System.out.println();
    }

    public static Boolean isEvenIndex(Integer index) {
        if (index % 2 == 0) return Boolean.TRUE;
        return Boolean.FALSE;
    }

    public static Boolean isOddIndex(Integer index) {
        if (index % 2 == 0) return Boolean.FALSE;
        return Boolean.TRUE;
    }

    public static void main(String s[]) {

        DataStructure ds = new DataStructure();

        System.out.println("printEven()");
        ds.printEven();

        System.out.println("print(DataStructureIterator) with "
                + "getEvenIterator");
        ds.print(ds.getEvenIterator());

        System.out.println("print(DataStructureIterator) with "
                + "anonymous class, odd indicies");
        ds.print(
                new DataStructure.DataStructureIterator() {
                    private int nextIndex = 1;
                    public boolean hasNext() {
                        return (nextIndex <= ds.size() - 1);
                    }
                    public Integer next() {
                        int retValue = ds.get(nextIndex);
                        nextIndex += 2;
                        return retValue;
                    }
                }
        );

        System.out.println("print(Function) with lambda expressions");
        ds.print(index -> {
            if (index % 2 == 0) return Boolean.TRUE;
            return Boolean.FALSE;
        });
        ds.print(index -> {
            if (index % 2 == 0) return Boolean.FALSE;
            return Boolean.TRUE;
        });

        System.out.println("print(Function) with method references");
        ds.print(DataStructure::isEvenIndex);
        ds.print(DataStructure::isOddIndex);
    }
}

2 个答案:

答案 0 :(得分:6)

ds.print方法采用类型为Function<Integer,Boolean>的参数。所以这个:

ds.print(index -> {
    if (index % 2 == 0) return Boolean.TRUE;
    return Boolean.FALSE;
});

使用匿名类而不是lambda:

等效于此语法
ds.print(new Function<Integer,Boolean>() {
    @Override
    public Boolean apply(Integer index) {
        if (index % 2 == 0) return Boolean.TRUE;
        return Boolean.FALSE;
    }
});

Function类的功能方法是apply。)

因此参数名index是任意的。你可以随意调用它。它只是lambda方法的本地名称。它的值由print

中的此调用提供
if (function.apply(i)) ...

答案 1 :(得分:0)

因为print的参数声明为类型:函数从Integer到Boolean。