import org.apache.commons.lang.ArrayUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CompareStringArrayUtil {
/**
* @param oldArr 旧数组
* @param newArr 新数组
* @return Map 包含两个数组,相较于旧数组,新数组多了哪些元素,以及少了哪些元素
*/
public static List<String> compareStringArray(String[] oldArr, String[] newArr) {
List<String> addList = new ArrayList<>();
List<String> deleteList = new ArrayList<>();
for (String anOldArr : oldArr) {
if (!ArrayUtils.contains(newArr, anOldArr)) {
deleteList.add(anOldArr);
}
}
for (String aNewArr : newArr) {
if (!ArrayUtils.contains(oldArr, aNewArr)) {
addList.add(aNewArr);
}
}
String[] addArr = addList.toArray(new String[0]);
String[] deleteArr = deleteList.toArray(new String[0]);
Map<String, String[]> res = new HashMap<>();
res.put("addArr", addArr);
res.put("deleteArr", deleteArr);
return deleteList;
}
}
两个String数组进行对比
最新推荐文章于 2024-03-08 00:07:16 发布