From imperative programming to fork/join to parallel streams in Java 8

Source: Internet
Author: User
Tags join sort

Java 8 brings a lot of features that can make coding more concise. For example, like the following code:

Collections.sort (Transactions, new comparator<transaction> () {public
  int compare (Transaction t1, Transaction T2) {return
    t1.getvalue (). CompareTo (T2.getvalue ());
  }
);

Can be replaced with the following more compact code, the same functionality, but read up with the problem statement itself closer:

Transactions.sort (Comparing (Transaction::getvalue));

The main features introduced in Java 8 are lambda expressions, method references, and new streams APIs. It is considered to be the largest language-changing version since Java was born 20 ago. To learn how to benefit from these features with detailed and practical examples, refer to the Java 8 in Action:lambdas, Streams and Functional-style, written by the author of this article and Alan Mycroft. Programming "book.

These features support programmers to write more concise code and enable them to benefit from multi-core architectures. In fact, writing programs that can be executed gracefully in parallel is the prerogative of Java experts. However, the new streams Api,java 8 has changed this situation, making it easier for everyone to write code that leverages multi-core architectures.

In this article, we will use the following three styles to compute the variance of a large dataset in different ways and compare it.

Style of command

Fork/join Framework

Streams API

Variance is a statistical concept that measures the degree of deviation of a group of numbers. The variance can be calculated by averaging the square sum of the difference between each data and the average. For example, given a set of numbers that represent the age of the population: 40, 30, 50, and 80, we can calculate the variance in this way:

Calculate Average: (40 + 30 + 50 + 80)/4 = 50

Calculates the square sum of the difference between each data and average: (40-50)2 + (30-50)2 + (50-50)2 + (80-50)2 = 1400

Last average: 1400/4 = 350

Style of command

The following is a typical command-line implementation of the computed variance:

public static double varianceimperative (double[] population) {
   double average = 0.0;
   for (double p:population) {
      average + = p;
   }
   Average/= population.length;

   Double variance = 0.0;
   for (double p:population) {
     Variance = = (p-average) * (p-average);
   }
   return variance/population.length;
}

Why do you say this is an imperative? Our implementation describes the computational process by modifying the state of the statement sequence . Here, we explicitly iterate over each element of the population age array and update the average and variance local variables each iteration. This code is ideal for hardware architectures with only one CPU. Indeed, it can be mapped very directly to the CPU's instruction set.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

Related Article

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.