cn.woodwhales.common.business.collection.CollectionTool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of woodwhales-common Show documentation
Show all versions of woodwhales-common Show documentation
https://github.com/woodwhales
package cn.woodwhales.common.business.collection;
import com.google.common.collect.Sets;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.util.Collections.emptySet;
import static org.apache.commons.collections4.CollectionUtils.isEmpty;
/**
* @author woodwhales on 2020-12-07
*/
public class CollectionTool {
private CollectionTool() {
}
/**
* set1 和 set2 集合进行差集运算
* @param set1 集合1
* @param set2 集合2
* @param 数据泛型
* @return set
*/
public static Set difference(Set set1, Set set2) {
if(isEmpty(set1)) {
return emptySet();
}
if(isEmpty(set2)) {
return set1;
}
return Sets.difference(set1, set2);
}
/**
* 从原始 set 集合生成新的 set
* @param set 源数据集合
* @param function 生成新的 set 集合接口
* @param 源数据类型
* @param 目标数据类型
* @return set
*/
public static Set toSet(Set set, Function function) {
if(isEmpty(set)) {
return emptySet();
}
return set.stream().map(function).collect(Collectors.toSet());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy