New features of JAVA8, binary sequence to decimal digits

Source: Internet
Author: User

 Packagekata_007_ binary sequence to decimal int;/*** java8 lambda expression convert binary sequence--decimal number*/Importjava.util.ArrayList; Public classShortbinarytodecimal { Public Static voidMain (string[] args) {ArrayList<Integer> binary =NewArrayList (); Binary.add (1); Binary.add (0); Binary.add (1); Binary.add (0); intresult =binarytodecimal (binary);    SYSTEM.OUT.PRINTLN (result); }         Public Static intBinarytodecimal (arraylist<integer>binary) {          returnBinary.stream (). Reduce ((x, y) x * 2 +y). get (); }}

If you are not using a lambda expression, use the traditional way:

 Packagekata_007_ binary sequence to decimal int;Importjava.awt.List;Importjava.util.ArrayList; Public classTestbinarytodecimal { Public Static voidMain (string[] args) {//test MethodArrayList binary =NewArrayList (); Binary.add (1); Binary.add (0); Binary.add (0); Binary.add (1); intresult =binarytodecimal (binary);    SYSTEM.OUT.PRINTLN (result); }         Public Static intBinarytodecimal (arraylist<integer>binary) {String Binarystr= "";  for(intI=0;i<binary.size (); i++) {Binarystr+=Binary.get (i); }        //String binary-string decimalString decimalstr = integer.valueof (binarystr,2). toString (); //String decimal-int        intDecimalresult =Integer.parseint (DECIMALSTR); returnDecimalresult; }}

New features of JAVA8, binary sequence to decimal digits

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.