这段代码是查找当前数组中的内容,是否在排除数组中出现,其实这种业务无非是循环遍历两个数组,看看有没有相同的内容,但是原先的代码写的太挫了,稍微改了一下,没有算法上面的优化。
优化后代码
/**
* 检查是否是排除类目
*
* @param excludeCats 排除类目列表
* @param categoryId 当前类目列表
* @return
*/
private boolean isExcludeCats(String[] excludeCats, String[] categoryIds) {
if (excludeCats != null)
{
for (String cat : excludeCats) {
if (categoryIds != null) {
for (String str : categoryIds) {
if (!StringUtil.isEmpty(str))
if (str.equals(cat)) {
return true;
}
}
}
}
}
return false;
}
优化后代码
private boolean isExcludeCats(String[] excludeCats, String[] categoryIds) {
if (excludeCats != null && categoryIds != null)
{
for (String cat : excludeCats) {
if (StringUtil.isEmpty(cat)) {
continue;
}
for (String str : categoryIds) {
if (cat.equals(str)) {
return true;
}
}
}
}
return false;
}