简体   繁体   中英

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. 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. I have explained him how number of line reduced using java8.

I have also provided example of Comparator interface that we can directly implement Comparator interface using lambda expression. 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. So I didn't know how to explain how java 8 provide concise code because as per his description he was right.

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. 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? Refer Example 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.

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.

You just have to catch the lambda into a variable as done in the first example and use it everywhere you want. 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; 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...

For Stream API we can also sort elements, So why should we use Stream API of Java8 for sorting

Sorting is exactly one of the functionalities that are not good in streaming 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. What's more important is the transition of our mindset from OOP in Java7 to Functional Programming in 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). Then you can use it anywhere and be free from the rigid Class-Object hierarchy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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