最近工作中遇到Map取差集的问题,两个Map
然后就想着写个通用的方法来解决。
提示:以下是本篇文章正文内容,下面案例可供参考
想达到的效果是:入参是啥,出参就是啥。
如:入参Map
入参:Map
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class NapUtil {
/**
* 取Map集合的差集
*/
public static <S,T> Map<S, T> getDifferenceSetByGuava(Map<S, T> leftMap, Map<S, T> rightMap) {
if (null != leftMap && null != rightMap) {
Set<S> leftMapKey = leftMap.keySet();
Set<S> rightMapKey = rightMap.keySet();
Set<S> differenceSet = Sets.difference(leftMapKey, rightMapKey);
Map<S, T> result = Maps.newHashMap();
for (S key : differenceSet) {
result.put(key, leftMap.get(key));
}
return result;
} else {
return null;
}
}
/**
* 取Map集合的并集
*/
public static <S,T> Map<S, T> getUnionSetByGuava(Map<S, T> leftMap, Map<S, T> rightMap) {
if (null != leftMap && null != rightMap) {
Set<S> leftMapKey = leftMap.keySet();
Set<S> rightMapKey = rightMap.keySet();
Set<S> differenceSet = Sets.union(leftMapKey, rightMapKey);
Map<S, T> result = Maps.newHashMap();
for (S key : differenceSet) {
if (leftMap.containsKey(key)) {
result.put(key, leftMap.get(key));
} else {
result.put(key, rightMap.get(key));
}
}
return result;
} else {
return null;
}
}
/**
* 取Map集合的交集(String,String)
*/
public static <S,T> Map<S, T> getIntersectionSetByGuava(Map<S, T> leftMap, Map<S, T> rightMap) {
if (null != leftMap && null != rightMap) {
Set<S> leftMapKey = leftMap.keySet();
Set<S> rightMapKey = rightMap.keySet();
Set<S> differenceSet = Sets.intersection(leftMapKey, rightMapKey);
Map<S, T> result = Maps.newHashMap();
for (S key : differenceSet) {
result.put(key, leftMap.get(key));
}
return result;
} else {
return null;
}
}
public static void main(String[] args) {
Map<String, Person> map1 = new HashMap<>();
map1.put("a", new Person(1));
map1.put("b", new Person(2));
map1.put("c", new Person(3));
Map<String, Person> map2 = new HashMap<>();
map2.put("c", new Person(3));
map2.put("d", new Person(4));
map2.put("e", new Person(5));
Map<String, Person> diffMap1 = getDifferenceSetByGuava(map1, map2);
System.out.println("-------------差集结果,入参:A,B 出参:A-B后A中剩余的 -----------");
diffMap1.forEach((k, v) -> System.out.println(k + ":" + v));
Map<String, Person> diffMap2 = getDifferenceSetByGuava(map2, map1);
System.out.println("-------------差集结果,入参:B,A 出参:B-A后B中剩余的 -----------");
diffMap2.forEach((k, v) -> System.out.println(k + ":" + v));
Map<String, Person> unionMap = getUnionSetByGuava(map1, map2);
System.out.println("-------------并集结果-----------");
unionMap.forEach((k, v) -> System.out.println(k + ":" + v));
Map<String, Person> intersectionMap = getIntersectionSetByGuava(map1, map2);
System.out.println("-------------交结果-----------");
intersectionMap.forEach((k, v) -> System.out.println(k + ":" + v));
}
}
import lombok.Data;
@Data
public class Person {
private Integer number;
public Person(Integer number) {
this.number = number;
}
}
结果如下:
-------------差集结果,入参:A,B 出参:A-B后A中剩余的 -----------
a:Person(number=1)
b:Person(number=2)
-------------差集结果,入参:B,A 出参:B-A后B中剩余的 -----------
d:Person(number=4)
e:Person(number=5)
-------------并集结果-----------
a:Person(number=1)
b:Person(number=2)
c:Person(number=3)
d:Person(number=4)
e:Person(number=5)
-------------交结果-----------
c:Person(number=3)
/**
* 取Map集合的List
*/
public static <S,T> List<T> getDifferenceListByGuava(Map<S, T> leftMap, Map<S, T> rightMap) {
if (null != leftMap && null != rightMap) {
Set<S> leftMapKey = leftMap.keySet();
Set<S> rightMapKey = rightMap.keySet();
Set<S> differenceSet = Sets.difference(leftMapKey, rightMapKey);
List<T> result = new ArrayList<>();
for (S key : differenceSet) {
result.add(leftMap.get(key));
}
return result;
} else {
return Collections.emptyList();
}
}
提示:使用泛型,是真的香。