求List,Map,Set的交集,並集與差集
應用場景
在大資料的背景下,我們在做專案的時候往往使用單表在資料庫中查詢資料,然後多表在service層進行關聯操作。比如說下面的情況就是如此,在這裡我並不是展開講多表之間如何實現解耦的單表查詢操作,我只是針對其中的涉及多表關聯的集合操作進行講解。
ListMap集合操作
資料準備
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<String> 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
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);
交集
@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集合操作
資料準備
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<String> iterator = set1.iterator();
Set<String> 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<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());
}
}
執行結果:
[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<String> iterator = set1.iterator();
Set<String> 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
相關推薦
求List,Map,Set的交集,並集與差集
應用場景 在大資料的背景下,我們在做專案的時候往往使用單表在資料庫中查詢資料,然後多表在service層進行關聯操作。比如說下面的情況就是如此,在這裡我並不是展開講多表之間如何實現解耦的單表查詢操作,我只是針對其中的涉及多表關聯的集合操作進行講解
python 兩個list 求交集,並集,差集
pytho 列表解析 int __main__ class clas etc intersect run def diff(listA,listB): #求交集的兩種方式 retA = [i for i in listA if i in listB]
求兩個list的交集,並集,差集。
1. 獲取兩個list 的交集 1 2 3 4 5 6 7 8 9 #方法一: a=[2,3,4,5] b=[2,5,8] tmp = [val for
python兩個 list 獲取交集,並集,差集的方法
交集 spa 兩個 方法 val inter tmp for col 1. 獲取兩個list 的交集 #方法一: a=[2,3,4,5] b=[2,5,8] tmp = [val for val in a if val in b] print tmp #[2, 5] #
python 求列表的交集,並集,差集
#coding=utf-8 def foo(list1,list2): #求交集 ret1 = [i for i in list1 if i in list2] ret2 = list(set(list1).intersection(set(lis
資料結構:求兩個有序列表的交集,並集
1.求兩個有序列表的交集 LNode* Intersection(LNode* La,LNode* Lb) { if (La==NULL||Lb==NULL) { return NULL; } LNode *pCHead = NULL; //A與B交集頭 LNode *pCE
java兩個List的交集,並集,差異集
public static void main(String[] args) { List list1 =new ArrayList(); list1.add("1111"); list1.add("2222"); list1.add("3333"); lis
求A,B陣列的交集,並集和差集
注意函式自身是否有元素重複的判斷,還有要把switch函式放在最後面,小細節。 還有一個小細節就是函式呼叫直接寫,前面不要在加cout; cout>>chaji(a,b,n);就會在結尾多出來一個數,為迴圈次數。 #include<iostream&
java(List或Array陣列)求交集、並集、差集, 泛型工具類
業務需要求不同型別的交集、並集、差集為避免程式碼冗餘編寫工具類。 注:list 轉陣列需傳入陣列,如果將原陣列傳入將會改變原陣列的值,同時泛型陣列又不可以例項化,解決方案:Arrays.copyOf(n,list.size()) ,使用copyOf功能,開闢返回集合的等長新陣列,避免修改原陣列。
java 對兩個list進行“交集,並集,差集,去重複並集”的操作
@Test public void testTwoList(){ List<String> a = new ArrayList<String>(); List<
LeetCode : Intersection of Two Arrays II 兩個向量去重問題 求交集,並集,差集
std :: set_intersection 兩個排序範圍的交叉點(交集) 構造一個從result指向的位置開始的排序範圍,其中包含兩個已排序範圍 [ first1,last1 ) 和 [ first2,last2)的集合交集。 兩組的交集僅由兩組
兩個整數集合A,B,求二者交集、並集、差集
交集: void Intersect(const vector<int>& A,const vector<int>& B,vector<int>& ans) { map<int, int> Counter; cons
已知兩個連結串列A和B分別表示兩個集合,其元素遞增排列。請設計演算法求出兩個集合A和B的差集(即僅由在A中出現而不在B中出現的元素所構成的集合),並以同樣的形式儲存,同時返回該集合的元素個數。
語言:C++ #include <iostream> using namespace std; typedef struct LNode { int data; LNode *next; }LNode,*LinkList; //建立連結串列 int CreateList(Li
[Python]集合的交集,並集,差集
前提:測試中需要給某些應用設定黑名單,所以從.txt檔案中求兩者的差集,就可以篩選出需要測試的應用 思路:將.txt檔案中的資料讀到列表中,求列表的集合,再輸出到指定目錄 集合的思路: a = [1,2,3,4,5,6] b = [5,6,7] c = [] ①交集 c = [i for i i
求兩個元素遞增排列的連結串列的交集,並將其存放在某個連結串列中
#include "stdafx.h" #include<stdio.h> #include<malloc.h> #include<stdlib.h> typed
hibernateTemplate 使用原生sql查詢,並將查詢結果集直接封裝成map物件
private List<?> findBySQLForWhat(String sql,Object[] args,String what) { SQLQuery query = this.getSession().createSQLQuery(sql);
JS陣列操作:去重,交集,並集,差集
var aHasNaN = a.some(function(v){ return isNaN(v) }) var bHasNaN = b.some(function(v){ return isNaN(v) }) // 並集 var union = a.concat(b.filter(function(v)
java集合類,List和Set比較,各自的子類比較(ArrayList,Vector,LinkedList;HashSet,TreeSet),Map集合比較
ArrayList,LinkedList,Vector都屬於ListList:元素是有順序的,元素可以重複因為每個元素有自己的角標(索引)|-- ArrayList:底層是陣列結構,特點是:查詢很快,增刪稍微慢點,執行緒不同步:A執行緒將元素放在索引0位置,CPU排程執行緒A停止,B執行,也將元素放在索引0位
bash技巧:求集合的交集、並集、差集、對稱差集
網上轉的,不錯,比使用awk容易點 給定兩個檔案 a.txt 和 b.txt ,每行是一個記錄(假設沒有重複),要求輸出兩集合的交集、並集、差集,輸出的結果只包括唯一項。交集定義為同時出現在兩個檔案中的記錄項,並集定義為出現在任何一個檔案中的記錄項,差集(A-B)定義為出
mysql中交集,並集,差集,左連線,右連線
學習mysql也有一個月啦,在這個月中,都是按照需求對資料表進行一些基本操作,在這個過程當中,經常使用到左連線,右連線,交集,取差集等,現在對其基本操作進行歸納總結。 資料來源: 表一: