public static void compareGroups ( List < ProviderGroup > oldGroups , List < ProviderGroup > newGroups , List < ProviderInfo > add , List < ProviderInfo > remove ) { // 比较老列表和当前列表 if ( CommonUtils . isEmpty ( oldGroups ) ) { // 空变成非空 if ( CommonUtils . isNotEmpty ( newGroups ) ) { for ( ProviderGroup newGroup : newGroups ) { add . addAll ( newGroup . getProviderInfos ( ) ) ; } } // 空到空，忽略 } else { // 非空变成空 if ( CommonUtils . isEmpty ( newGroups ) ) { for ( ProviderGroup oldGroup : oldGroups ) { remove . addAll ( oldGroup . getProviderInfos ( ) ) ; } } else { // 非空变成非空，比较 if ( CommonUtils . isNotEmpty ( oldGroups ) ) { Map < String , List < ProviderInfo > > oldMap = convertToMap ( oldGroups ) ; Map < String , List < ProviderInfo > > mapTmp = convertToMap ( newGroups ) ; // 遍历新的 for ( Map . Entry < String , List < ProviderInfo > > oldEntry : oldMap . entrySet ( ) ) { String key = oldEntry . getKey ( ) ; List < ProviderInfo > oldList = oldEntry . getValue ( ) ; if ( mapTmp . containsKey ( key ) ) { // 老的有，新的也有，比较变化的部分 final List < ProviderInfo > newList = mapTmp . remove ( key ) ; compareProviders ( oldList , newList , add , remove ) ; mapTmp . remove ( key ) ; } else { // 老的有，新的没有 remove . addAll ( oldList ) ; } } // 新的有，老的没有 for ( Map . Entry < String , List < ProviderInfo > > entry : mapTmp . entrySet ( ) ) { add . addAll ( entry . getValue ( ) ) ; } } } } }
Compare two provider group list return add list and remove list