You need to determine whether the set of two operations when using the same, all the tools to provide a method using a characteristic (unique element) set collection:
private Map<String,Set<Integer>> getCategoryApiId(Set<Integer> oldAuthSet , Set<Integer> newAuthSet){ Map<String,Set<Integer>> categoryApiId = new HashMap(); if (oldAuthSet!=null&&newAuthSet!=null){ //首先判断两个集合是否一致 if (oldAuthSet.size()==newAuthSet.size()){ Set<Integer> tempSet = new HashSet<Integer>(); tempSet.addAll(oldAuthSet); tempSet.addAll(newAuthSet); if (tempSet.size()==oldAuthSet.size()){ categoryApiId = null ; // not increase the number of api logger.info ( "not to add and delete api" ); } the else { // . 1: intersection; 2: ab set difference; 3: Set and Set <Integer> apiSet1 getApiSet = (oldAuthSet, newAuthSet,. 1 ); the Set <Integer> = deleteAuthSet getApiSet (oldAuthSet, apiSet1, 2 ); the Set <Integer> = insertAuthSet getApiSet (newAuthSet, oldAuthSet, 2); // Get new apiId logger.info ( "remove the API:" + deleteAuthSet); logger.info ("The new API:" + insertAuthSet); categoryApiId.put ( "insertApi" logger.info ( "Deleted api: "+, insertAuthSet); categoryApiId.put ( "datele" , deleteAuthSet); } } the else { // . 1: intersection; 2: ab set difference; 3: ba set difference; 4: Set and Set <Integer> apiSet1 = getApiSet (oldAuthSet, newAuthSet, 1 ); the Set <Integer> deleteAuthSet = getApiSet (oldAuthSet, apiSet1, 2); // get apiId remove the Set <Integer> insertAuthSet = getApiSet (newAuthSet, oldAuthSet, 2); // get the new apiId categoryApiId.put (deleteAuthSet); logger.info ( "new API:" + insertAuthSet); "insertApi" , insertAuthSet); categoryApiId.put ( "datele" , deleteAuthSet); } } the else { IF (oldAuthSet == null && newAuthSet =! null ) { // all new API logger.info ( "the first time the new authorization API" ); categoryApiId.put ( "insertApi" , newAuthSet); } the else IF ! (oldAuthSet = null &&newAuthSet == null ) { // unmodified api user authorization data logger.info (" no new authorization api " ); categoryApiId.put ( "datele" , oldAuthSet); } the else IF (oldAuthSet == null && newAuthSet == null ) { logger.info ( "new authorization and authorized api api is empty " ); categoryApiId = null ; } } return categoryApiId; } Private the Set <Integer> getApiSet (the Set <Integer> oldAuthSet, the Set <Integer> newAuthSet, int In Flag) { the Set <Integer> = Result new new HashSet <Integer>(); if(flag==1){ //求交集 result.clear(); result.addAll(oldAuthSet); result.retainAll(newAuthSet); }else if(flag==2){ //求差集 result.clear(); result.addAll(oldAuthSet); result.removeAll(newAuthSet); } else if (flag==3){ //求并集 result.clear(); result.addAll(oldAuthSet); result.addAll(newAuthSet); } return result; }