lambdas只是java中的一个语法糖

时间:2018-05-16 08:50:21

标签: java lambda

我来到这段代码

Collections.sort(list, (o1, o2) -> o1.getValue().compareTo(o2.getValue()));

我想知道,这只是

的语法糖
Collections.sort(list, new Comparator<CodeTableEntry>() {
    @Override
    public int compare(CodeTableEntry o1, CodeTableEntry o2) {
        return o1.getValue().compareTo(o2.getValue());
    }
});

还是更智能的东西?是否优化不是每次都创建新实例?

这只是我的观点,但是这种非平凡的单行并不是更容易遵循简单的代码......(这不是上面的那个,而是下面的一个)

static final Comparator<CodeTableEntry> codeTableEntryValueComparator = new Comparator<CodeTableEntry>() {
    @Override
    public int compare(CodeTableEntry o1, CodeTableEntry o2) {
        return o1.getValue().compareTo(o2.getValue());
    }
};

... 
    Collections.sort(list, codeTableEntryValueComparator);
...

澄清为“它是否优化不是每次都创建新实例?”

因为只有一个new我相信它很清楚,似乎不是。我做了这个代码来检查:

public class ComparatorTest {

    private static void sort(List<String> list) {
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                System.out.println("comparator: " + this);
                return s1.compareTo(s2);
            }
        });
        System.out.println("sorted: " + list);
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();

        list.add("c");
        sort(list);
        list.add("b");
        sort(list);
        list.add("a");
        sort(list);
    }

}

,输出

sorted: [c]
comparator: ComparatorTest$1@7852e922
sorted: [b, c]
comparator: ComparatorTest$1@4e25154f
comparator: ComparatorTest$1@4e25154f
comparator: ComparatorTest$1@4e25154f
comparator: ComparatorTest$1@4e25154f
sorted: [a, b, c]

我相信显示,为每个方法调用创建了比较器的新实例(因为不同的哈希码)

如何检查lambdas,仅用于我的测试我在创建异常时打印了stacktrace

Collections.sort(list, (o1, o2) -> getValue(o1).compareTo(o2));

其中getValue

public static String getValue(String s) {
    RuntimeException re = new RuntimeException();
    re.printStackTrace(System.out);
    return s;
}

并打印

sorted: [c]
java.lang.RuntimeException
    at ComparatorTest.getValue(ComparatorTest.java:20)
    at ComparatorTest.lambda$0(ComparatorTest.java:15)
    at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
    at java.util.TimSort.sort(Unknown Source)
    at java.util.Arrays.sort(Unknown Source)
    at java.util.ArrayList.sort(Unknown Source)
    at java.util.Collections.sort(Unknown Source)
    at ComparatorTest.sort(ComparatorTest.java:15)
    at ComparatorTest.main(ComparatorTest.java:32)
sorted: [b, c]
java.lang.RuntimeException
    at ComparatorTest.getValue(ComparatorTest.java:20)
    at ComparatorTest.lambda$0(ComparatorTest.java:15)
    at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
    at java.util.TimSort.sort(Unknown Source)
    at java.util.Arrays.sort(Unknown Source)
    at java.util.ArrayList.sort(Unknown Source)
    at java.util.Collections.sort(Unknown Source)
    at ComparatorTest.sort(ComparatorTest.java:15)
    at ComparatorTest.main(ComparatorTest.java:34)
java.lang.RuntimeException
    at ComparatorTest.getValue(ComparatorTest.java:20)
    at ComparatorTest.lambda$0(ComparatorTest.java:15)
    at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
    at java.util.TimSort.sort(Unknown Source)
    at java.util.Arrays.sort(Unknown Source)
    at java.util.ArrayList.sort(Unknown Source)
    at java.util.Collections.sort(Unknown Source)
    at ComparatorTest.sort(ComparatorTest.java:15)
    at ComparatorTest.main(ComparatorTest.java:34)
java.lang.RuntimeException
    at ComparatorTest.getValue(ComparatorTest.java:20)
    at ComparatorTest.lambda$0(ComparatorTest.java:15)
    at java.util.TimSort.binarySort(Unknown Source)
    at java.util.TimSort.sort(Unknown Source)
    at java.util.Arrays.sort(Unknown Source)
    at java.util.ArrayList.sort(Unknown Source)
    at java.util.Collections.sort(Unknown Source)
    at ComparatorTest.sort(ComparatorTest.java:15)
    at ComparatorTest.main(ComparatorTest.java:34)
java.lang.RuntimeException
    at ComparatorTest.getValue(ComparatorTest.java:20)
    at ComparatorTest.lambda$0(ComparatorTest.java:15)
    at java.util.TimSort.binarySort(Unknown Source)
    at java.util.TimSort.sort(Unknown Source)
    at java.util.Arrays.sort(Unknown Source)
    at java.util.ArrayList.sort(Unknown Source)
    at java.util.Collections.sort(Unknown Source)
    at ComparatorTest.sort(ComparatorTest.java:15)
    at ComparatorTest.main(ComparatorTest.java:34)
sorted: [a, b, c]

似乎只有一个ComparatorTest.lambda$0 ...

0 个答案:

没有答案