在大数据的背景下,我们在做项目的时候往往使用单表在数据库中查询数据,然后多表在service层进行关联操作。比如说下面的情况就是如此,在这里我并不是展开讲多表之间如何实现解耦的单表查询操作,我只是针对其中的涉及多表关联的集合操作进行讲解。
List<String> list1 = Lists.newArrayList("a","b","c","f");
List<String> list2 = Lists.newArrayList("c","d","e","f");
@Test
public void testIntersection(){
list1.retainAll(list2);
List<String> list3 = Lists.newArrayList("c","f");
Assert.assertEquals(list3,list1);
list1.forEach( str -> log.info("打印出的字符为:{}",str));
}
执行结果:
[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
@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));
}
执行结果:
[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
@Test
public void testSubtract(){
list1.removeAll(list2);
List list3 = Lists.newArrayList();
list3.add("a");
list3.add("b");
Assert.assertEquals(list3,list1);
list1.forEach( str -> log.info("打印出的字符为:{}",str));
}
执行结果:
[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
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);
@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);
});
}
执行结果:
[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
@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);
});
}
执行结果:
[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
@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);
});
}
执行结果:
[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
@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);
});
}
执行结果:
[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
Set<String> set1 = Sets.newHashSet("a","b","c","f");
Set<String> set2 = Sets.newHashSet("c","d","e","f");
@Test
public void testIntersection(){
set1.retainAll(set2);
Iterator iterator = set1.iterator();
Set set3 = Sets.newHashSet("c","f");
//断言
Assert.assertEquals(set3,set1);
while(iterator.hasNext()){
log.info("打印出的字符为:{}",iterator.next());
}
}
执行结果:
[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
@Test
public void testUnion(){
set1.addAll(set2);
Iterator iterator = set1.iterator();
Set set3 = Sets.newHashSet("a","b","c","f","d","e");
//断言
Assert.assertEquals(set3,set1);
while(iterator.hasNext()){
log.info("打印出的字符为:{}",iterator.next());
}
}
执行结果:
[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
@Test
public void testSubtract(){
set1.removeAll(set2);
Iterator iterator = set1.iterator();
Set set3 = Sets.newHashSet("a","b");
//断言
Assert.assertEquals(set3,set1);
while(iterator.hasNext()){
log.info("打印出的字符为:{}",iterator.next());
}
}
执行结果:
[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