Java实现数组的交集、并集、差集
来源:互联网 发布:巴顿比格斯 知乎 编辑:程序博客网 时间:2024/05/21 18:43
/** * 数组交集,并集,差集 * @author Administrator * */public class cal3 { public static void main(String[] args) { String[] arr1 = {"abc", "df", "abc"}; String[] arr2 = {"abc", "cc", "df", "d", "abc"}; intersect(arr1, arr2); } //并集 public static Set 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); } return set; } //差集(在数组A中不在数组B中) public static List<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); } } } return list; } //交集 public static List<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()); System.out.println(e.getKey()); } } return list; } }
0 0
- Java实现数组的交集、并集、差集
- Set数据结构实现数组的交集、并集、差集
- java求两个数组的并集、交集、差集
- java数组的交集、并集、差集
- ES6数组实现并集、交集、差集
- java数组并集/交集/差集(补集)
- JAVA程序:字符串数组交集、并集和差集
- JAVA求字符串数组交集、并集和差集
- JAVA求字符串数组交集、并集和差集
- JAVA求字符串数组交集、并集和差集
- JAVA程序:字符串数组交集、并集和差集
- JAVA求字符串数组交集、并集和差集
- JAVA求字符串数组交集、并集和差集
- 两个数组的交集,并集,差集。
- c#数组的交集,差集,并集
- c#数组的交集,差集,并集
- c#数组的交集,差集,并集
- ES6 操作数组的并集/交集/差集
- 约瑟夫环的基本问题
- LCTの题表
- 常用排序算法-JAVA实现
- dbcp 配置数据库连接池
- Servlet解决中文乱码现象
- Java实现数组的交集、并集、差集
- Snackbar的使用
- POJ 3107 Godfather (求树的重心)
- 第三次培训 matlab代码
- 精确表达浮点数
- [MySQL][ODBC 5.7 Driver]Can't connect to MySQL server on 'localhost' (10061)
- 从psd到HTML个人记录
- 常用网络服务器总结
- CI框架nginx服务器,该如何配置前后台。