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 ));}}