List允许合并时出现重复的项,如果合并时不允许出现相同的项,可以借助HashSet,因为HashSet不允许出现相同的项。
步骤为:
1. 两个List都添加到HashSet中。
2. HashSet赋给一个List。
代码:
HashSet functionSet=new HashSet();
List<Function> functionList=new ArrayList<Function>();
for (Position pos : posList) {
Integer posId=pos.getId();
functionList=positionDAO.getFunctionListById(posId);
functionSet.addAll(functionList);
}
functionList=new ArrayList<Function>(functionSet);
直接从俺的程序中贴来的,可以将Function类变为String理解。
注意:HashSet中的数据不会按照加入的顺序排列,如果对顺序有要求,可以用TreeMap。