简体   繁体   English

Java 8 具有 Stream API、Lambda 以及与 Java 7 的简洁代码比较

[英]Java 8 features Stream API, Lambda and concise code comparison with java 7

I am learning Java 8 new features and I came across one situation where I was not able to convince my interviewer how java 8 provide more concise code than older version of java.我正在学习 Java 8 的新特性,我遇到了一种情况,我无法说服我的面试官 java 8 如何提供比旧版本的 java 更简洁的代码。 I have provided an example of Functional Interface and comparator interface that class don't have to implement interface.我提供了一个功能接口和比较器接口的示例,该类不必实现接口。 Using lambda expression we can directly provide implementation of method like below code Example1.使用 lambda 表达式,我们可以直接提供方法的实现,如下面的代码 Example1。 I have explained him how number of line reduced using java8.我已经向他解释了如何使用 java8 减少行数。

I have also provided example of Comparator interface that we can directly implement Comparator interface using lambda expression.我还提供了 Comparator 接口的示例,我们可以使用 lambda 表达式直接实现 Comparator 接口。 But He said if we are implementing Comparator interface with some class then I can reuse it while using Lambda expression I have to write logic again and again whenever I what to do sorting.但是他说如果我们用某个类实现 Comparator 接口,那么我可以在使用 Lambda 表达式时重用它,我必须一次又一次地编写逻辑,每当我要进行排序时。 So I didn't know how to explain how java 8 provide concise code because as per his description he was right.所以我不知道如何解释 java 8 如何提供简洁的代码,因为根据他的描述,他是对的。

For Stream API we can also sort elements, So why should we use Stream API of Java8 for sorting while collection have method Collections.sort.对于Stream API,我们也可以对元素进行排序,那么为什么要使用Java8的Stream API进行排序,而collection有方法Collections.sort。 If I am using Stream API for sorting then I have collect all elements in new List while using Collections.sort will sort existing list then why should we use Stream API?如果我使用 Stream API 进行排序,那么我已经收集了新列表中的所有元素,而使用 Collections.sort 将对现有列表进行排序,那么我们为什么要使用 Stream API? Refer Example 3.请参阅示例 3。

So I was not able to understand how Java8 provide concise code and how stream API is helpful or why should I use Stream API.所以我无法理解 Java8 如何提供简洁的代码以及流 API 有什么帮助,或者我为什么要使用流 API。

I have done some searching on google but I haven't found any satisfactory answers.我在谷歌上做了一些搜索,但我没有找到任何令人满意的答案。

//Exaple 1
//Traditional Way
interface SampleInterface {
    public void sampleMethod();
}

public class SampleClass implements SampleInterface {
    @Override
    public void sampleMethod() {
        // Implementation Logic
    }
}

//Using Lambda expression and Functional Interface
@FunctionalInterface
interface SampleInterface {
    public void sampleMethod();
}

public class SampleClass {
    public static void main(String[] args) {
        SampleInterface sf = () -> {System.out.println("Implementation of interface method");};
    }
}


//Example 2
Comparator<Student> c = (s1, s2) -> {return (s1.age < s2.age ? -1 : (s1.age > s2.age) ? 1 : 0 );};


//Example 3
//Using Stream API
List<Employee> newList = empList.stream().sorted((v1, v2) -> (v2.id < v1.id) ? -1 : (v2.id > v1.id) ? 1 : 0).collect(Collectors.toList());

//Using comparator 
Collections.sort(list, comparator);

But He said if we are implementing Comparator interface with some class then I can reuse it while using Lambda expression I have to write logic again and again whenever I what to do sorting.但是他说如果我们用某个类实现 Comparator 接口,那么我可以在使用 Lambda 表达式时重用它,我必须一次又一次地编写逻辑,每当我要进行排序时。

You just have to catch the lambda into a variable as done in the first example and use it everywhere you want.您只需像第一个示例中所做的那样将 lambda 捕获到一个变量中,然后在您想要的任何地方使用它。 This argument is very common among 7 resisting devs, first tell him that you don't have to construct a class for a single instanciation;这种说法在 7 位反抗的开发者中很常见,首先告诉他你不必为单个实例构建一个类; second that the comparator code is present at the right place, so that reading how the collection is sorted doesn't need to search for the definition of the sorting class;其次,比较器代码出现在正确的位置,因此读取集合的排序方式不需要搜索排序类的定义; third that the compiler is best at optimizations when using lambdas...第三,编译器在使用 lambda 时最擅长优化......

For Stream API we can also sort elements, So why should we use Stream API of Java8 for sorting Stream API 也可以对元素进行排序,那么为什么要使用 Java8 的 Stream API 进行排序呢?

Sorting is exactly one of the functionalities that are not good in streaming POV.排序正是流式 POV 中不好的功能之一。 It is given just not to break the stream : construct a stream, collect, sort, stream again, etc它只是为了不破坏流:构造流,收集,排序,再次流等

I think the key here is not about implementing Comparator or using Lambda, both are ok.我认为这里的关键不是实现 Comparator 或使用 Lambda,两者都可以。 What's more important is the transition of our mindset from OOP in Java7 to Functional Programming in Java8.更重要的是我们的思维方式从 Java7 中的 OOP 转变为 Java8 中的函数式编程。 The key of functional programming is to treat functions as the first class citizens, meaning functions and data are of the same importance.函数式编程的关键是将函数视为一等公民,这意味着函数和数据同等重要。 It blurs boundaries between them, thus functions can be stored in variables and passed anywhere (pretty much like function pointer in C).它模糊了它们之间的界限,因此函数可以存储在变量中并传递到任何地方(很像 C 中的函数指针)。 Then you can use it anywhere and be free from the rigid Class-Object hierarchy.然后,您可以在任何地方使用它,并摆脱僵化的 Class-Object 层次结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM