• Java求字符串数组交集、并集和差集


    //求两个字符串数组的并集,利用set的元素唯一性   
        public static String[] union(String[] arr1, String[] arr2) {   
            Set<String> set = new HashSet<String>();   
            for (String str : arr1) {   
                set.add(str);   
            }   
            for (String str : arr2) {   
                set.add(str);   
            }   
            String[] result = {};   
            return set.toArray(result);   
        }   
      
        //求两个数组的交集   
        public static String[] intersect(String[] arr1, String[] arr2) {   
            Map<String, Boolean> map = new HashMap<String, Boolean>();   
            LinkedList<String> list = new LinkedList<String>();   
            for (String str : arr1) {   
                if (!map.containsKey(str)) {   
                    map.put(str, Boolean.FALSE);   
                }   
            }   
            for (String str : arr2) {   
                if (map.containsKey(str)) {   
                    map.put(str, Boolean.TRUE);   
                }   
            }   
      
            for (Entry<String, Boolean> e : map.entrySet()) {   
                if (e.getValue().equals(Boolean.TRUE)) {   
                    list.add(e.getKey());   
                }   
            }   
      
            String[] result = {};   
            return list.toArray(result);   
        }   
      
        //求两个数组的差集   
        public static String[] minus(String[] arr1, String[] arr2) {   
            LinkedList<String> list = new LinkedList<String>();   
            LinkedList<String> history = new LinkedList<String>();   
            String[] longerArr = arr1;   
            String[] shorterArr = arr2;   
            //找出较长的数组来减较短的数组   
            if (arr1.length > arr2.length) {   
                longerArr = arr2;   
                shorterArr = arr1;   
            }   
            for (String str : longerArr) {   
                if (!list.contains(str)) {   
                    list.add(str);   
                }   
            }   
            for (String str : shorterArr) {   
                if (list.contains(str)) {   
                    history.add(str);   
                    list.remove(str);   
                } else {   
                    if (!history.contains(str)) {   
                        list.add(str);   
                    }   
                }   
            }   
      
            String[] result = {};   
            return list.toArray(result);   
        }   
  • 相关阅读:
    android scroll 中 scroll Bar 修改
    android 在代码中安装apk的方法
    android JSON 的应用
    android 混淆相关 proguard
    listView 相关的优化设置
    android 名称解释
    android Gallery 两侧阴影实现
    Service 详解
    使用alias 简化命令
    android 动画小结
  • 原文地址:https://www.cnblogs.com/libaoting/p/20131024zfc.html
Copyright © 2020-2023  润新知