zoukankan      html  css  js  c++  java
  • 【Set】Set集合求并集,交集,差集

    /**
     * @author: Sam.yang
     * @date: 2020/11/16 11:14
     * @desc: Set集合操作工具类
     */
    public class SetOptUtils {
        /**
         * 取两数交集.
         * <P>
         * Example:
         *
         * <pre>
         * src={1,2,3},dest={2,4}
         * intersect(dest,src)={2}
         * </pre>
         *
         * @param dest
         *            The destination set.
         * @param src
         *            The source set.
         * @return the same elements of src and dest
         */
        public static <T> Set<T> intersect(Set<T> dest, Set<T> src) {
            Set<T> set = new HashSet<T>(src.size());
            copy(set, src);
            set.retainAll(dest);
            return set;
        }
    
        /**
         * 取两数并集.
         * <P>
         * Example:
         *
         * <pre>
         * src={1,2,3},dest={2,4,5}
         * union(dest,src)={1,2,3,4,5}
         * </pre>
         *
         * @param dest
         *            The destination set.
         * @param src
         *            The source set.
         * @return the all elements of src and dest
         */
        public static <T> Set<T> union(Set<T> dest, Set<T> src) {
            Set<T> set = new HashSet<T>(src.size());
            copy(set, src);
            set.addAll(dest);
            return set;
        }
    
        /**
         * 取两数差集(减法).
         * <P>
         * Example:
         *
         * <pre>
         * src={1,2,3},dest={2,4,5},src-dest={1,3}
         * diff(dest,src)={1,3}
         * </pre>
         *
         * @param dest
         *            The destination set.
         * @param src
         *            The source set.
         * @return the elements in src but not exist dest
         */
        public static <T> Set<T> diff(Set<T> dest, Set<T> src) {
            Set<T> set = new HashSet<T>(src.size());
            copy(set, src);
            set.removeAll(dest);
            return set;
        }
    
        /**
         * 集合判空.
         *
         * @param c
         *            The source collection.
         * @return true/false
         */
        public static boolean isEmpty(Collection<?> c) {
            boolean rs = false;
            if (c == null || (c != null && c.isEmpty())) {
                rs = true;
            }
            return rs;
        }
    
        /**
         * 判断两集合是否有相同的元素.
         * @param dest The destination set.
         * @param src The source list.
         * @return true/false
         */
        public static <T> boolean isSameElements(Set<T> dest, Set<T> src) {
            if (isEmpty(dest) || isEmpty(src)) {
                return false;
            }
    
            Set<T> set = intersect(dest, src);
            if (set.size() > 0) {
                return true;
            }
    
            return false;
        }
    
        /**
         * Copies all of the elements from src set into dest.
         *
         * @param dest
         *            The destination set.
         * @param src
         *            The source list.
         */
        private static <T> void copy(Set<T> dest, Set<T> src) {
            dest.addAll(src);
        }
    
        public static void main(String[] args) {
            Set<String> set = new HashSet<String>();
            Set<String> set2 = new HashSet<String>();
            set.add("111");
            set.add("010W");
            set2.add("010W");
            System.out.println(diff(set, set2));
        }
  • 相关阅读:
    微信浏览器取消缓存的方法
    iphone safari浏览器CSS兼容性的解决方案集合
    配置iis支持.json格式的文件
    win7下使用IIS服务器及自定义服务器端包含模块(SSI)步骤
    前端组件库集合
    ClientValidationFunction
    java 查询solr时间格式
    为何大量网站不能抓取?爬虫突破封禁的6种常见方法
    反爬虫四个基本策略
    ScheduledExecutorService 定时器用法
  • 原文地址:https://www.cnblogs.com/july-sunny/p/13995559.html
Copyright © 2011-2022 走看看