在大數據的背景下,我們在做項目的時候往往使用單表在數據庫中查詢數據,然後多表在service層進行關聯操作。比如說下面的情況就是如此,在這裡我并不是展開講多表之間如何實現解耦的單表查詢操作,我隻是針對其中的涉及多表關聯的集合操作進行講解。
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
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!