集合总结时求交集还是并集(求ListMapSet的交集)

  • 应用场景
  • ListMap集合操作
  • 数据准备
  • 交集
  • 并集
  • 差集
  • Map集合操作
  • 数据准备
  • 交集
  • 并集
  • 差集(左侧)
  • 差集(右侧)
  • Set集合操作
  • 数据准备
  • 交集
  • 并集
  • 差集
应用场景

在大数据的背景下,我们在做项目的时候往往使用单表在数据库中查询数据,然后多表在service层进行关联操作。比如说下面的情况就是如此,在这里我并不是展开讲多表之间如何实现解耦的单表查询操作,我只是针对其中的涉及多表关联的集合操作进行讲解。

集合总结时求交集还是并集(求ListMapSet的交集)(1)

ListMap集合操作

数据准备

List<String> list1 = Lists.newArrayList("a","b","c","f"); List<String> list2 = Lists.newArrayList("c","d","e","f"); 1 2

交集

@Test public void testIntersection(){ list1.retainAll(list2); List<String> list3 = Lists.newArrayList("c","f"); Assert.assertEquals(list3,list1); list1.forEach( str -> log.info("打印出的字符为:{}",str)); } 1 2 3 4 5 6 7

执行结果:

[13:31:43.729] INFO com.baishun.login.setOperation.ListOperation 24 lambda$testIntersection$0 - 打印出的字符为:c [13:31:43.737] INFO com.baishun.login.setOperation.ListOperation 24 lambda$testIntersection$0 - 打印出的字符为:f 1 2

并集

@Test public void testUnion(){ list1.addAll(list2); List<String> list3 = Lists.newArrayList("a","b","c","f","c","d","e","f"); Assert.assertEquals(list3,list1); list1.forEach( str -> log.info("打印出的字符为:{}",str)); } 1 2 3 4 5 6 7

执行结果:

[13:32:53.028] INFO com.baishun.login.setOperation.ListOperation 35 lambda$testUnion$1 - 打印出的字符为:a [13:32:53.035] INFO com.baishun.login.setOperation.ListOperation 35 lambda$testUnion$1 - 打印出的字符为:b [13:32:53.035] INFO com.baishun.login.setOperation.ListOperation 35 lambda$testUnion$1 - 打印出的字符为:c [13:32:53.036] INFO com.baishun.login.setOperation.ListOperation 35 lambda$testUnion$1 - 打印出的字符为:f [13:32:53.037] INFO com.baishun.login.setOperation.ListOperation 35 lambda$testUnion$1 - 打印出的字符为:c [13:32:53.037] INFO com.baishun.login.setOperation.ListOperation 35 lambda$testUnion$1 - 打印出的字符为:d [13:32:53.037] INFO com.baishun.login.setOperation.ListOperation 35 lambda$testUnion$1 - 打印出的字符为:e [13:32:53.037] INFO com.baishun.login.setOperation.ListOperation 35 lambda$testUnion$1 - 打印出的字符为:f 1 2 3 4 5 6 7 8

差集

@Test public void testSubtract(){ list1.removeAll(list2); List<String> list3 = Lists.newArrayList(); list3.add("a"); list3.add("b"); Assert.assertEquals(list3,list1); list1.forEach( str -> log.info("打印出的字符为:{}",str)); } 1 2 3 4 5 6 7 8 9

执行结果:

[13:35:01.629] INFO com.baishun.login.setOperation.ListOperation 48 lambda$testSubtract$2 - 打印出的字符为:a [13:35:01.641] INFO com.baishun.login.setOperation.ListOperation 48 lambda$testSubtract$2 - 打印出的字符为:b 1 2

Map集合操作

数据准备

ImmutableMap<String,String> immutableMap1 = ImmutableMap.of("a","a","b","b","c","c","f","f"); ImmutableMap<String,String> immutableMap2 = ImmutableMap.of("c","c","d","d","e","e","f","f"); MapDifference<String,String> difference = Maps.difference(immutableMap1,immutableMap2); 1 2 3 4

交集

@Test public void testIntersection(){ Map<String,String> commonMap = difference.entriesInCommon(); Map<String,String> map3 = Maps.newHashMap(); map3.put("c","c"); map3.put("f","f"); Assert.assertEquals(map3,commonMap); commonMap.forEach((k,v) -> { log.info("打印出的字符为:{}:{}",k,v); }); } 1 2 3 4 5 6 7 8 9 10 11

执行结果:

[13:37:23.391] INFO com.baishun.login.setOperation.MapOperation 33 lambda$testIntersection$0 - 打印出的字符为:c:c [13:37:23.398] INFO com.baishun.login.setOperation.MapOperation 33 lambda$testIntersection$0 - 打印出的字符为:f:f 1 2

并集

@Test public void testUnion(){ //将ImmutableMap转换成Map Map<String,String> map1 = Maps.newHashMap(immutableMap1); Map<String,String> map2 = Maps.newHashMap(immutableMap2); map1.putAll(map2); Map<String,String> map3 = Maps.newHashMap(); map3.put("a","a"); map3.put("b","b"); map3.put("c","c"); map3.put("d","d"); map3.put("e","e"); map3.put("f","f"); Assert.assertEquals(map3,map1); map1.forEach((k,v) -> { log.info("打印出的字符为:{}:{}",k,v); }); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

执行结果:

[13:38:06.784] INFO com.baishun.login.setOperation.MapOperation 57 lambda$testUnion$1 - 打印出的字符为:a:a [13:38:06.792] INFO com.baishun.login.setOperation.MapOperation 57 lambda$testUnion$1 - 打印出的字符为:b:b [13:38:06.793] INFO com.baishun.login.setOperation.MapOperation 57 lambda$testUnion$1 - 打印出的字符为:c:c [13:38:06.793] INFO com.baishun.login.setOperation.MapOperation 57 lambda$testUnion$1 - 打印出的字符为:d:d [13:38:06.793] INFO com.baishun.login.setOperation.MapOperation 57 lambda$testUnion$1 - 打印出的字符为:e:e [13:38:06.794] INFO com.baishun.login.setOperation.MapOperation 57 lambda$testUnion$1 - 打印出的字符为:f:f 1 2 3 4 5 6

差集(左侧)

@Test public void testLeftSubtract(){ Map<String,String> leftMap = difference.entriesOnlyOnLeft(); Map<String,String> map3 = Maps.newHashMap(); map3.put("a","a"); map3.put("b","b"); Assert.assertEquals(map3,leftMap); leftMap.forEach((k,v) -> { log.info("打印出的字符为:{}:{}",k,v); }); } 1 2 3 4 5 6 7 8 9 10 11

执行结果:

[13:39:22.543] INFO com.baishun.login.setOperation.MapOperation 72 lambda$testLeftSubtract$2 - 打印出的字符为:a:a [13:39:22.550] INFO com.baishun.login.setOperation.MapOperation 72 lambda$testLeftSubtract$2 - 打印出的字符为:b:b 1 2

差集(右侧)

@Test public void testRightSubtract(){ Map<String,String> rightMap = difference.entriesOnlyOnRight(); Map<String,String> map3 = Maps.newHashMap(); map3.put("d","d"); map3.put("e","e"); Assert.assertEquals(map3,rightMap); rightMap.forEach((k,v) -> { log.info("打印出的字符为:{}:{}",k,v); }); } 1 2 3 4 5 6 7 8 9 10 11

执行结果:

[13:40:07.171] INFO com.baishun.login.setOperation.MapOperation 87 lambda$testRightSubtract$3 - 打印出的字符为:d:d [13:40:07.180] INFO com.baishun.login.setOperation.MapOperation 87 lambda$testRightSubtract$3 - 打印出的字符为:e:e 1 2

Set集合操作

数据准备

Set<String> set1 = Sets.newHashSet("a","b","c","f"); Set<String> set2 = Sets.newHashSet("c","d","e","f"); 1 2

交集

@Test public void testIntersection(){ set1.retainAll(set2); Iterator<String> iterator = set1.iterator(); Set<String> set3 = Sets.newHashSet("c","f"); //断言 Assert.assertEquals(set3,set1); while(iterator.hasNext()){ log.info("打印出的字符为:{}",iterator.next()); } } 1 2 3 4 5 6 7 8 9 10 11

执行结果:

[13:41:28.227] INFO com.baishun.login.setOperation.SetOperation 28 testIntersection - 打印出的字符为:c [13:41:28.233] INFO com.baishun.login.setOperation.SetOperation 28 testIntersection - 打印出的字符为:f 1 2

并集

@Test public void testUnion(){ set1.addAll(set2); Iterator<String> iterator = set1.iterator(); Set<String> set3 = Sets.newHashSet("a","b","c","f","d","e"); //断言 Assert.assertEquals(set3,set1); while(iterator.hasNext()){ log.info("打印出的字符为:{}",iterator.next()); } } 1 2 3 4 5 6 7 8 9 10 11

执行结果:

[13:42:10.658] INFO com.baishun.login.setOperation.SetOperation 43 testUnion - 打印出的字符为:a [13:42:10.666] INFO com.baishun.login.setOperation.SetOperation 43 testUnion - 打印出的字符为:b [13:42:10.667] INFO com.baishun.login.setOperation.SetOperation 43 testUnion - 打印出的字符为:c [13:42:10.669] INFO com.baishun.login.setOperation.SetOperation 43 testUnion - 打印出的字符为:d [13:42:10.670] INFO com.baishun.login.setOperation.SetOperation 43 testUnion - 打印出的字符为:e [13:42:10.670] INFO com.baishun.login.setOperation.SetOperation 43 testUnion - 打印出的字符为:f 1 2 3 4 5 6

差集

@Test public void testSubtract(){ set1.removeAll(set2); Iterator<String> iterator = set1.iterator(); Set<String> set3 = Sets.newHashSet("a","b"); //断言 Assert.assertEquals(set3,set1); while(iterator.hasNext()){ log.info("打印出的字符为:{}",iterator.next()); } } 1 2 3 4 5 6 7 8 9 10 11

执行结果:

[13:42:59.123] INFO com.baishun.login.setOperation.SetOperation 58 testSubtract - 打印出的字符为:a [13:42:59.130] INFO com.baishun.login.setOperation.SetOperation 58 testSubtract - 打印出的字符为:b

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页