关于使用工具类org.apache.commons.collections.ListUtils合并List的问题
2014-06-05 18:07
826 查看
今天在做项目时,需要将几个List进行合并,于是就用到了apache提供关于List操作的工具类ListUtils,但是在使用的过程中发现一些问题。
本以为打印结果一样的,但是实际结果不一样。
方法1的结果是:[1, 2, 2, 3, 4]
方法2的结果是:[1, 2, 3, 4]
方法3的结果是:[1, 2, 2, 3, 4]
原来工具类在合并是,会自动除去重复的元素,看来以后使用工具类时还需要多加注意,要不然会坑了自己的。
public static void main(String[] args) { List<String> list1 = new ArrayList<String>(); list1.add("1"); list1.add("2"); List<String> list2 = new ArrayList<String>(); list2.add("2"); list2.add("3"); list2.add("4"); // 方法1, 为原始方法 List<String> list = new ArrayList<String>(); for (String str : list1) { list.add(str); } for (String str : list2) { list.add(str); } System.out.println(list); // 方法2, 使用工具类 list = ListUtils.sum(list1, list2); System.out.println(list); // 方法3 list = new ArrayList<String>(); list.addAll(list1); list.addAll(list2); System.out.println(list); }
本以为打印结果一样的,但是实际结果不一样。
方法1的结果是:[1, 2, 2, 3, 4]
方法2的结果是:[1, 2, 3, 4]
方法3的结果是:[1, 2, 2, 3, 4]
原来工具类在合并是,会自动除去重复的元素,看来以后使用工具类时还需要多加注意,要不然会坑了自己的。
相关文章推荐
- 使用org.apache.commons.collections.CollectionUtils进行List与List比较
- org.apache.commons.collectionsJDK中不是已经有了Java集合框架了吗,为什么还要使用Apache的集合呢?
- org/apache/commons/collections/map/ListOrderedMap不存在
- maven使用mvn命令创建项目异常java.lang.NoClassDefFoundError: org/apache/commons/collections/ExtendedProperties
- java.lang.ClassNotFoundException: org.apache.commons.collections.map.ListOrderedMap解决方法
- java.lang.ClassNotFoundException: org.apache.commons.collections.CursorableLinkedList
- apache commons collections CollectionUtils工具类简单使用
- org.apache.commons.lang.xwork.StringUtils 问题的解决!json使用时产生的问题
- JSON调试找不到 net.sf.ezmorph.Morpher与org/apache/commons/collections/map/ListOrderedMap这个异常。
- 使用org.apache.commons.io.FileUtils,IOUtils;工具类操作文件
- java.lang.ClassNotFoundException: org.apache.commons.collections.map.ListOrderedMap解决方法
- 关于java中的List集合的排序问题/Collections.sort()的使用(转)
- json,junit运行java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap
- java.lang.NoClassDefFoundError: org/apache/commons/collections/CursorableLinkedList
- SSH框架搭建遇到的问题-----java.lang.NoClassDefFoundError: org/apache/commons/collections/map/LRUMap
- java.lang.ClassNotFoundException:org/apache/commons/collections/CursorableLinkedList
- java.lang.ClassNotFoundException: org.apache.commons.collections.map.ListOrderedMap解决方法
- 【转】class "org.apache.commons.collections.CursorableLinkedList"'s signer information does not match signer information of other c
- Struts + Hibernate 的异常,class "org.apache.commons.collections.CursorableLinkedList"'s signer information does not match signer in
- 集合工具类 org.apache.commons.collections.CollectionUtils