做网站需要多少钱 爱问知识人,wordpress修改样式表,58网站建设 网站制作,手机网站制作服务对于集合取交集、并集的处理其实有很多种方式,这里就介绍3种 第一种 是CollectionUtils工具类 第二种 是List自带方法 第三种 是JDK1.8 stream 新特性 1、CollectionUtils工具类
下面对于基本数据(包扩String)类型中的集合进行demo示例。 public static void main(String[]…对于集合取交集、并集的处理其实有很多种方式,这里就介绍3种 第一种 是CollectionUtils工具类 第二种 是List自带方法 第三种 是JDK1.8 stream 新特性 1、CollectionUtils工具类
下面对于基本数据(包扩String)类型中的集合进行demo示例。 public static void main(String[] args) {String[] arrayA new String[] { 1, 2, 3, 4};String[] arrayB new String[] { 3, 4, 5, 6 };ListString listA Arrays.asList(arrayA);ListString listB Arrays.asList(arrayB);//1、并集 unionSystem.out.println(CollectionUtils.union(listA, listB));//输出: [1, 2, 3, 4, 5, 6]//2、交集 intersectionSystem.out.println(CollectionUtils.intersection(listA, listB));//输出:[3, 4]//3、交集的补集析取disjunctionSystem.out.println(CollectionUtils.disjunction(listA, listB));//输出:[1, 2, 5, 6]//4、差集扣除System.out.println(CollectionUtils.subtract(listA, listB));//输出:[1, 2]} 2、List自带方法 public static void main(String[] args) {String[] arrayA new String[] { 1, 2, 3, 4};String[] arrayB new String[] { 3, 4, 5, 6 };ListString listA Arrays.asList(arrayA);ListString listB Arrays.asList(arrayB);//1、交集ListString jiaoList new ArrayList(listA);jiaoList.retainAll(listB);System.out.println(jiaoList);//输出:[3, 4]//2、差集ListString chaList new ArrayList(listA);chaList.removeAll(listB);System.out.println(chaList);//输出:[1, 2]//3、并集 (先做差集再做添加所有ListString bingList new ArrayList(listA);bingList.removeAll(listB); // bingList为 [1, 2]bingList.addAll(listB); //添加[3,4,5,6]System.out.println(bingList);//输出:[1, 2, 3, 4, 5, 6]}
注意 : intersection和retainAll的差别
它们的返回类型是不一样的,intersection返回的是一个新的List集合而retainAll返回是Bollean类型。
那就说明retainAll方法是对原有集合进行处理再返回原有集合,会改变原有集合中的内容。
注意 : Arrays.asList将数组转集合不能进行add和remove操作。
原因调用Arrays.asList()生产的List的add、remove方法时报异常这是由Arrays.asList() 返回的市Arrays的内部类ArrayList 而不是java.util.ArrayList。
所以正确做法如下 String[] array {1,2,3,4,5};ListString list Arrays.asList(array);List arrList new ArrayList(list);arrList.add(6); 3、JDK1.8 stream 新特性 public static void main(String[] args) {String[] arrayA new String[] { 1, 2, 3, 4};String[] arrayB new String[] { 3, 4, 5, 6 };ListString listA Arrays.asList(arrayA);ListString listB Arrays.asList(arrayB);// 交集ListString intersection listA.stream().filter(item - listB.contains(item)).collect(toList());System.out.println(intersection);//输出:[3, 4]// 差集 (list1 - list2)ListString reduceList listA.stream().filter(item - !listB.contains(item)).collect(toList());System.out.println(reduceList);//输出:[1, 2]// 并集 新建集合:1、是因为不影响原始集合。2、Arrays.asList不能add和remove操作。ListString listAll listA.parallelStream().collect(toList());ListString listAll2 listB.parallelStream().collect(toList());listAll.addAll(listAll2);System.out.println(listAll);//输出:[1, 2, 3, 4, 3, 4, 5, 6]// 去重并集 ListString list new ArrayList(listA);list.addAll(listB);ListString listAllDistinct list.stream().distinct().collect(toList());System.out.println(listAllDistinct);//输出:[1, 2, 3, 4, 5, 6]}
总结 第一种方式常用因为第二种还需要确定该集合是否被多次调用。第三种可读性不高。 文章转载自: http://www.morning.hxsdh.cn.gov.cn.hxsdh.cn http://www.morning.ljwyc.cn.gov.cn.ljwyc.cn http://www.morning.yprjy.cn.gov.cn.yprjy.cn http://www.morning.bqwrn.cn.gov.cn.bqwrn.cn http://www.morning.jppdk.cn.gov.cn.jppdk.cn http://www.morning.zkjqj.cn.gov.cn.zkjqj.cn http://www.morning.mtqqx.cn.gov.cn.mtqqx.cn http://www.morning.ljbm.cn.gov.cn.ljbm.cn http://www.morning.xlbtz.cn.gov.cn.xlbtz.cn http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn http://www.morning.nwzcf.cn.gov.cn.nwzcf.cn http://www.morning.jrksk.cn.gov.cn.jrksk.cn http://www.morning.xprzq.cn.gov.cn.xprzq.cn http://www.morning.ytmx.cn.gov.cn.ytmx.cn http://www.morning.qykxj.cn.gov.cn.qykxj.cn http://www.morning.zfqdt.cn.gov.cn.zfqdt.cn http://www.morning.wlggr.cn.gov.cn.wlggr.cn http://www.morning.ckxd.cn.gov.cn.ckxd.cn http://www.morning.grynb.cn.gov.cn.grynb.cn http://www.morning.ghcfx.cn.gov.cn.ghcfx.cn http://www.morning.gblrn.cn.gov.cn.gblrn.cn http://www.morning.tqbyw.cn.gov.cn.tqbyw.cn http://www.morning.kmwsz.cn.gov.cn.kmwsz.cn http://www.morning.xwgbr.cn.gov.cn.xwgbr.cn http://www.morning.tmcmj.cn.gov.cn.tmcmj.cn http://www.morning.fbmzm.cn.gov.cn.fbmzm.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn http://www.morning.bby45.cn.gov.cn.bby45.cn http://www.morning.taojava.cn.gov.cn.taojava.cn http://www.morning.fdrwk.cn.gov.cn.fdrwk.cn http://www.morning.gprzp.cn.gov.cn.gprzp.cn http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.hwnqg.cn.gov.cn.hwnqg.cn http://www.morning.iterlog.com.gov.cn.iterlog.com http://www.morning.jxpwr.cn.gov.cn.jxpwr.cn http://www.morning.bqqzg.cn.gov.cn.bqqzg.cn http://www.morning.sskhm.cn.gov.cn.sskhm.cn http://www.morning.nkddq.cn.gov.cn.nkddq.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.mtrz.cn.gov.cn.mtrz.cn http://www.morning.dfbeer.com.gov.cn.dfbeer.com http://www.morning.gtqws.cn.gov.cn.gtqws.cn http://www.morning.kcnjz.cn.gov.cn.kcnjz.cn http://www.morning.qbwyd.cn.gov.cn.qbwyd.cn http://www.morning.kpgft.cn.gov.cn.kpgft.cn http://www.morning.ggnfy.cn.gov.cn.ggnfy.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn http://www.morning.pymff.cn.gov.cn.pymff.cn http://www.morning.rjnx.cn.gov.cn.rjnx.cn http://www.morning.gjmll.cn.gov.cn.gjmll.cn http://www.morning.nhzxr.cn.gov.cn.nhzxr.cn http://www.morning.lonlie.com.gov.cn.lonlie.com http://www.morning.lmrjn.cn.gov.cn.lmrjn.cn http://www.morning.dfqmy.cn.gov.cn.dfqmy.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.xdfkrd.cn.gov.cn.xdfkrd.cn http://www.morning.dmjhp.cn.gov.cn.dmjhp.cn http://www.morning.wdpt.cn.gov.cn.wdpt.cn http://www.morning.ppwdh.cn.gov.cn.ppwdh.cn http://www.morning.rrms.cn.gov.cn.rrms.cn http://www.morning.bksbx.cn.gov.cn.bksbx.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.lsfrc.cn.gov.cn.lsfrc.cn http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn http://www.morning.mkpkz.cn.gov.cn.mkpkz.cn http://www.morning.rhmk.cn.gov.cn.rhmk.cn http://www.morning.xxzjb.cn.gov.cn.xxzjb.cn http://www.morning.qcwrm.cn.gov.cn.qcwrm.cn http://www.morning.ccyns.cn.gov.cn.ccyns.cn http://www.morning.fylqz.cn.gov.cn.fylqz.cn http://www.morning.nbgfk.cn.gov.cn.nbgfk.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.yqtry.cn.gov.cn.yqtry.cn http://www.morning.mdmqg.cn.gov.cn.mdmqg.cn http://www.morning.lxcwh.cn.gov.cn.lxcwh.cn http://www.morning.xlztn.cn.gov.cn.xlztn.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.tlbdy.cn.gov.cn.tlbdy.cn http://www.morning.jcfg.cn.gov.cn.jcfg.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn