了解何时以及如何使用Java 8 Lambdas [英] Understanding when and how to use Java 8 Lambdas

查看:150
本文介绍了了解何时以及如何使用Java 8 Lambdas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一些时间来学习Java 8的一些新功能。作为练习,我使用一些Java 8功能接口编写了一个MergeSort。我在下面包含完整的代码(可能存在错误/优化,如果它们与Java 8功能相关,我只对它们感兴趣)。我的问题是,我相信在使用我的功能界面时有机会利用lambda表达式,但它还没有点击我的大脑。感觉就像每次我打电话申请,我应该有一种方法可以使用 - >代替。有人可以给我看灯吗?

I have been spending some time trying to learn some of Java 8's new features. As an exercise, I wrote a MergeSort using some Java 8 Functional Interfaces. I'm including the full code below (bugs/optimizations may exist, I am only interested in them if they relate to Java 8 features). My question is, I believe there are opportunities to utilize lambda expressions when I am using my functional interfaces, but it's just not clicking in my brain yet. It feels like everytime I am calling apply, there should be a way I can use "->" instead. Can someone please show me the light?

public class Merge implements BinaryOperator<int[]>{

@Override
public int[] apply(int[] t, int[] u) {

    int[] result = new int[t.length + u.length];

    for (int i = 0, j = 0, k = 0; i < result.length; i++){

        if( j == t.length){
            result[i] = u[k++];
        } else if (k == u.length) {
            result[i] = t[j++];
        } else {
            result[i] = t[j] < u [k] ? t[j++] : u[k++];
        }

    }

    return result;
}

}



将MergeSort写成java.util .function.Function



MergeSort written as a java.util.function.Function

public class MergeSort implements Function<int[], int[]>{

Merge merge = new Merge();

@Override
public int[] apply(int[] t) {

    if(t.length <= 1){
        return t;
    }

     return merge.apply( apply(Arrays.copyOfRange(t, 0, t.length / 2)), 
                         apply(Arrays.copyOfRange(t, t.length / 2, t.length )));

}

}



主要有一个简单的测试用例



Main with one simple test case

public class MergeSortMain {

public static void main(String[] args) {

    int values[] = {3,12,6,7,2,1,23,4,5,7,8,4,2,5,365};

    MergeSort mergeSort = new MergeSort();

    System.out.println(Arrays.toString(mergeSort.apply(values)));
}

}

产生

[1, 2, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 12, 23, 365]


推荐答案

lambda表达式的概念是代替创建一个类实现了一个功能接口,你可以定义该接口类型的lambda表达式。

The idea of lambda expressions is that instead of creating a class that implements a functional interface, you can define a lambda expression of the type of that interface.

例如,你的合并类实现 BinaryOperator< int []> 并且可以替换为以下lambda表达式:

For example, your Merge class implements BinaryOperator<int[]> and can be replaced by the following lambda expression :

BinaryOperator<int[]> merge = (t,u) -> {
    int[] result = new int[t.length + u.length];
    for (int i = 0, j = 0, k = 0; i < result.length; i++){
        if( j == t.length){
            result[i] = u[k++];
        } else if (k == u.length) {
            result[i] = t[j++];
        } else {
            result[i] = t[j] < u [k] ? t[j++] : u[k++];
        }
    }
    return result;
};

现在我们可以类似地创建一个lambda表达式来替换 MergeSort class,并且,结合两个lambdas,我们得到:

Now we can similarly create a lambda expression to replace the MergeSort class, and, combining the two lambdas, we get :

public class MergeSortMain {
    public static Function<int[], int[]> mergeSort;
    public static void main(String[] args) {
        int values[] = {3,12,6,7,2,1,23,4,5,7,8,4,2,5,365};
        mergeSort = l -> {
            BinaryOperator<int[]> merge = (t,u) -> {
                int[] result = new int[t.length + u.length];
                for (int i = 0, j = 0, k = 0; i < result.length; i++){
                    if( j == t.length){
                        result[i] = u[k++];
                    } else if (k == u.length) {
                        result[i] = t[j++];
                    } else {
                        result[i] = t[j] < u [k] ? t[j++] : u[k++];
                    }
                }
                return result;
            };
            if(l.length <= 1){
                return l;
            }
            return merge.apply( mergeSort.apply(Arrays.copyOfRange(l, 0, l.length / 2)), 
                                mergeSort.apply(Arrays.copyOfRange(l, l.length / 2, l.length )));
        };
        System.out.println(Arrays.toString(mergeSort.apply(values)));
    }
}

关于此代码的一些要点:

Some points regarding this code :


  1. 我必须从 t mergeSort lambda的参数c $ c>到 l ,因为 t 也用于 merge lambda。

  2. 我必须将 mergeSort lambda声明为静态成员(在分配其值之前),因为它包含对自身的递归调用。

  1. I had to rename the parameter of mergeSort lambda from t to l, since t is also used in the merge lambda.
  2. I had to declare the mergeSort lambda as a static member (prior to assigning its value), since it contains recursive calls to itself.

这篇关于了解何时以及如何使用Java 8 Lambdas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆