比较两个List元素是否相同
/** <Description functions in a word>
* <Detail description>
* @param <T>
* @param a
* @param b
* @return [Parameters description]
*
* @return boolean [Return type description]
* @exception throws [Excetion] [Exception description]
* @see [Related classes#Related methods#Related properties]
*/
public synchronized <T extends Comparable<T>> boolean compare(List<T> a, List<T> b) {
if(a.size() != b.size())
return false;
Collections.sort(a);
Collections.sort(b);
for(int i=0;i<a.size();i++){
if(!a.get(i).equals(b.get(i)))
return false;
}
return true;
}
查找出两个list的不同元素
public List<String> getDiffElementUseMap(List<String> list1,List<String> list2){ //获得两个list中的不同元素,map方法
final Integer flagUnique = 1;//无重复字符串的键值
log.info("====Start getDiffElementUseMap ====================="+new Date());
long runtime = System.nanoTime();//开始计时
//利用map中不能有重复元素的特点
Map<String, Integer> map = new HashMap<String,Integer>(list1.size()+ list2.size());
List<String> diffList = new ArrayList<String>();//用于保存两个list中不同的元素
for (String string : list1) {
map.put(string,flagUnique);//先将list1中元素复制到map中保存
}
for (String string : list2) {
Integer key = map.get(string);// 获得键值
if (key != null) {// 如果map中已经存在该元素,说明list1中存在该元素,那么将其key加1
map.put(string, ++key);
continue;
} else {// 如果不存在,则放入map中
map.put(string, flagUnique);
}
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (entry.getValue() == flagUnique)// 在map中,键值为flagUnique的元素即为无重复的元素
{
diffList.add(entry.getKey());
}
}
log.info("getDiffElementUseMap run time:"
+ (System.nanoTime() - runtime));
log.info("====End getDiffElementUseMap ====================="+new Date());
return diffList;
}