遍历求值,该如何解决
阅读
我有一个String[]acctitleIdArray 数组,里面为[1,2,3,4],还有一个List limitSubList,里面为[1,3,7,8]
,问怎么遍历出acctitleIdArray在limitSubList里的值,和不在limitSubList里的值。把这两个结果集分别放到2个字符串中,字符串中的字符用,隔开。
------解决方案--------------------
使用 apache commons collections 包中的collectionUtils类的intersection方法和disjunction方法可以达到你的要求
。此方法可以算出两个集合中的并集和非并集
------解决方案--------------------
利用集合的交并差方法就可以了
- Java code
List<String> list = new ArrayList<String>(); for (String s : acctitleIdArray) { list.add(s); } List<String> existList = new ArrayList<String>(limitSubList).retainAll(list); List<String> notExistList = list.removeAll(limitSubList); StringBuilder existBuf = new StringBuilder(); for (String s : existsList) { existBuf.append(s).append(","); } if (existBuf.length()>0) {existBuf.delete(existBur.length()-1, existBur.length());} StringBuilder notExistBuf = new StringBuilder(); for (String s : notExistList) { notExistBuf.append(s).append(","); } if (notExistBuf.length()>0) {notExistBuf.delete(notExistBuf.length()-1, notExistBuf.length());} String[] result = new String[]{existBuf.toString(), notExistBuf.toString()}; return result;
------解决方案--------------------
阅读
上一篇:Struts2 怎么保存查询条件 下一篇:返回列表