Java implementation version comparison, java Implementation

Source: Internet
Author: User

Java implementation version comparison, java Implementation

Package com. hzxc. chess. server. util;/*** Created by hdwang on 2018/3/19. * version comparison tool class */public class VersionCompareUtil {/*** compare version size ** Description: supported basic n-bit version number + 1-seat version number * example: 1.0.2> 1.0.1, 1.0.1.1> 1.0.1 ** @ param version1 version 1 * @ param version2 version 2 * @ return 0: Same 1: version1 greater than version2-1: version1 is smaller than version2 */public static int compareVersion (String version1, String version2) {if (version1.equals (version2) {retu Rn 0; // same version} String [] v1Array = version1.split ("\\. "); String [] v2Array = version2.split ("\\. "); int v1Len = v1Array. length; int v2Len = v2Array. length; int baseLen = 0; // number of digits of the base version number (smaller) if (v1Len> v2Len) {baseLen = v2Len;} else {baseLen = v1Len ;} for (int I = 0; I <baseLen; I ++) {// compare the base version with if (v1Array [I]. equals (v2Array [I]) {// The same bit version number is the same as that of continue; // compare the next bit} else {return Integer. parseInt (v1Array [I])> Integ Er. parseInt (v2Array [I])? 1:-1 ;}/// the basic version is the same, and then compare the sub-version if (v1Len! = V2Len) {return v1Len> v2Len? 1:-1;} else {// The basic version is the same, with no Subversion: return 0;} public static void main (String [] args) {String v1 = "1.0.1 "; string v2 = "1.0.2"; String v2_1 = "1.0.2.1"; String v2_2 = "1.0.2.2"; String v3 = "1.0.3"; System. out. println ("v1> v1:" + compareVersion (v1, v1); System. out. println ("v1> v2:" + compareVersion (v1, v2); System. out. println ("v2> v1:" + compareVersion (v2, v1); System. out. println ("v2_1> v1:" + compareVersion (v2_1, v1); System. out. println ("v2_1> v2:" + compareVersion (v2_1, v2); System. out. println ("v2_2> v2_1:" + compareVersion (v2_2, v2_1); System. out. println ("v3> v2:" + compareVersion (v3, v2); System. out. println ("v3> v2_2:" + compareVersion (v3, v2_2 ));}}

 

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.