Java8 new feature--LAMBDA expression. (Case: Complete number classification)

Source: Internet
Author: User

Requirement: Enter a number to determine its type (total number, excess number, insufficient number)

Full number: All factors outside of itself and = = itself

Excess: all factors outside of itself and > itself

Insufficiency: All factors outside of itself and < itself

1 Package Cn._3.numberclassifier;2 3 //BEGIN number_classifier_java84 import java.util.List;5 import Java.util.stream.IntStream;6 7ImportStaticjava.lang.Math.sqrt;8ImportStaticjava.util.stream.Collectors.toList;9ImportStaticJava.util.stream.IntStream.range;Ten  One  Public classNumberclassifier { A  -     //BEGIN Java8_filter -      Public StaticIntstream Factorsof (intNumber ) { the         returnRange1, number +1). Filter (potential, number% potential = =0); -     } -     //END Java8_filter -      Public Static intAliquotsum (intNumber ) { +         returnFactorsof (number). SUM ()-Number ; -     } +      Public StaticBoolean Isperfect (intNumber ) { A         returnAliquotsum (number) = =Number ; at     } -      Public StaticBoolean isabundant (intNumber ) { -         returnAliquotsum (number) >Number ; -     } -      Public StaticBoolean isdeficient (intNumber ) { -         returnAliquotsum (number) <Number ; in     } -     //END number_classifier_java8 to  +     //BEGIN Java8_filter_fast -      Public StaticList<integer> Fastfactorsof (intNumber ) { thelist<integer> factors = range (1, (int) (sqrt (number) +1)) *. filter (potential, number% potential = =0). Boxed ()//back to Stream<integer> $. Collect (ToList ());//folding/approx., reducePanax Notoginsenglist<integer> factorsabovesqrt = Factors.stream (). Map (E-and number/e). Collect (ToList ()); - Factors.addall (factorsabovesqrt); the         //Change stream to ArrayList +         returnFactors.stream (). Distinct (). Collect (ToList ()); A     } the     //END Java8_filter_fast +}

Test class:

1 Package Cn._3.numberclassifier;2 3  Public classMmain {4 5      Public Static voidMain (string[] args) {6Integer number =666;7System. out. println (Numberclassifier.isperfect (number) +","+numberclassifier.isabundant (number) +","+numberclassifier.isdeficient (number));8     }9}

Java8 new feature--LAMBDA expression. (Case: Complete number classification)

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.