Get a simple look at the new features of JAVA8

Source: Internet
Author: User

JAVA8 new features will subvert the programming habits of the entire Java programmer

Even if you stick to the programming habits of JAVA7, you'll see that younger programmers will not understand the Java code they write.

So in order to ensure not decoupling, I think it is necessary to learn the new features of JAVA8, moreover, this will greatly improve CPU efficiency and coding efficiency

Let's take a quick look at the new features of JAVA8

I think there are two main things that are more important, one is the lambda expression, and the other is the stream operation of the collection class.

About these two points, I wrote a few small demo, with notes, should be able to quickly grasp

If you are curious about more JAVA8 new features, please read more professional knowledge, agile development superficial understanding is my programming attitude.

By the way, JAVA8 currently only has the latest version of Eclipse Mars support compilation

Even if you downloaded the JAVA8 JDK and jre,myeclipse currently do not support the compilation of JAVA8

In other words, the following code will error below MyEclipse.

 Packagecom.newflypig.test;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.List;Importjava.util.stream.Collectors; Public classTestjava { Public Static voidMain (string[] args) {//new features of java8, lambda expressions, similar to anonymous inner classesThread Testthread =NewThread ((){List<String> list =NewArraylist<string>(); List.add (A); List.add ("B"); List.add (C); List.add ("AA"); List.add ("BB"); List.add ("CC"); List.foreach (S-{System.out.println (s);            ;            }); List.stream (). Filter (S-s.length () = = 2). ForEach (S-{System.out.println (s);        });        }); System.out.println ("======================================"); Distinct ("2", "3", "4", "5", "6", "7", "8", "9", "2", "3", "4", "5", "6", "7", "8", "9");    Testthread.run (); }    //new features, indeterminate parameters    Private Static voiddistinct (String ... numbers) {List<String> list =arrays.aslist (numbers); List<Integer> Listint =List.stream (). Map (SInteger.parseint (s))//maps element One by one in the collection stream to a new element and generates it into a new output stream. Filter (A-isprime (a))//filter, lambda expression. DISTINCT ()//Stream Advanced method to remove duplicates. Collect (Collectors.tolist ());//collects the sorted output stream into the new Listint collection        /*** You might think that in this example, the list list has been iterated several times, * map,filter,distinct is a cycle, the efficiency will not be good. * This is not actually the case.         These methods that return another stream are "lazy", and the Collect method that returns the final result is "eager".         * The lazy method will not execute until the eager method is encountered.         * * When the eager method is encountered, the previous lazy method is executed sequentially. * and is pipeline through-type execution.         This means that each element passes through these pipelines sequentially. * For example, there is an element "3", first it is map into an integer type 3; * Then through the filter, it is found that the prime number is preserved, and through distinct, * if there is already a 3, then discard directly, if not yet retained.         In this way, 3 operations actually go through only one cycle. * * Except collect and other eager operation and foreach,toarray,reduce, etc.*/Listint. Stream (). ForEach (i, {//Stream's ForEach, similar to Collections.foreachSystem.out.println (i);                                }); intsum=Listint. Stream (). reduce ((Isum,item)->isum+item). get (); intmin=Listint. Stream (). reduce ((b)->a<b?a:b). get (); intmax=Listint. Stream (). reduce ((b)->a>b?a:b). get (); /*** The Reduce method accepts a function that has two parameters, * The first parameter is the return value of the last function execution (also known as an intermediate result), and the second argument is the element in the stream, which adds the two values         * The resulting and assigned value is assigned to the first parameter of the next execution of this function.         * Note that the first value of the first parameter is the first element of the stream, and the second argument is the second element of the stream.         * This method returns a value type of optional, which is a viable way to Java8 prevent the NPE (NULL point ELEMENT), which is simply considered a container, which may contain 0 or 1 objects. */System.out.println (Sum+ "\ t" +min+ "\ T" +max); }            Private Static BooleanIsPrime (inta) {BooleanFlag =true;  for(inti = 2; I <= A/2; i++) {            if(a% i = = 0) {flag=false;  Break; }        }        returnFlag; }}

Learning about the new features of JAVA8 can be a scratch, as most existing frameworks are currently and will not be migrated to JAVA8 for quite a long time.

But with the introduction of the new feature lambda expression, I feel like I'm going to break out of another, more excellent frame.

In form, the lambda expression simply saves you a few lines of code.

But the motivation to introduce lambda expressions into Java is not just for that.

JAVA8 has a short-term goal and a long-term goal.

The short-term goal is to match the internal iterations of the collection class batch operation (out of the customer code) and parallel processing (more efficient use of modern CPUs);

The long-term goal is to steer Java to the functional programming language in this direction

Not to become a functional programming language, but to have more features of functional programming languages,

It is for this reason that Oracle does not simply use internal classes to implement lambda expressions, but instead uses a strategy that is more dynamic, flexible, and easy to scale and change in the future.

Get a simple look at the new features of JAVA8

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.