package com.wish.datastrustudy;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public class StringArray {
public static void main(String[] args) {
//测试union
String[] arr1 = {"abc", "df", "abc","dd","bdfe"};
String[] arr2 = {"abc", "cc", "df", "d", "abc"};
String[] result_union = union(arr1, arr2);
System.out.println("求并集的结果如下:");
for (String str : result_union) {
System.out.println(str);
}

//测试insect
String[] result_insect = intersect(arr1, arr2);
System.out.println("求交集的结果如下:");
for (String str : result_insect) {
System.out.println(str);
}
//测试 substract
String[] result_minus = substract(arr1, arr2);
System.out.println("求差集的结果如下:");
for (String str : result_minus) {
System.out.println(str);
}
}

//并集(set唯一性)
public static String[] union (String[] arr1, String[] arr2){
Set<String> hs = new HashSet<String>();
for(String str:arr1){
hs.add(str);
}
for(String str:arr2){
hs.add(str);
}
String[] result={};
return hs.toArray(result);
}

//交集(注意结果集中若使用LinkedList添加,则需要判断是否包含该元素,否则其中会包含重复的元素)
public static String[] intersect(String[] arr1, String[] arr2){
List<String> l = new LinkedList<String>();
Set<String> common = new HashSet<String>();
for(String str:arr1){
if(!l.contains(str)){
l.add(str);
}
}
for(String str:arr2){
if(l.contains(str)){
common.add(str);
}
}
String[] result={};
return common.toArray(result);
}
//求两个数组的差集
public static String[] substract(String[] arr1, String[] arr2) {
LinkedList<String> list = new LinkedList<String>();
for (String str : arr1) {
if(!list.contains(str)) {
list.add(str);
}
}
for (String str : arr2) {
if (list.contains(str)) {
list.remove(str);
}
}
String[] result = {};
return list.toArray(result);
}
}

java 交集 差集 并集的更多相关文章

  1. python-->(set /dict)交集 差集 并集 补集(功能用来做交差并补的)

    # ### 集合 作用:交集 差集 并集 补集(功能用来做交差并补的) '''特征:自动去重 无序''' #定义一个空集合 setvar = set() #set()强制转换成一个空集合的数据类型 p ...

  2. C# 集合的交集 差集 并集 去重

    C# 集合的交集 差集 并集 去重 两个对象list,直接比较是不行的,因为他们存的地址不一样 需要重写GetHashCode()与Equals(object obj)方法告诉电脑 class Stu ...

  3. js取两个数组的交集|差集|并集|补集|去重示例代码

    http://www.jb51.net/article/40385.htm 代码如下: /** * each是一个集合迭代函数,它接受一个函数作为参数和一组可选的参数 * 这个迭代函数依次将集合的每一 ...

  4. C# 数组比较--取得两个集合的交集,差集,并集的方法

    方法关键字: 交集:Intersect 差集:Except 并集:Union 使用代码: , , , , }; , , , , }; var 交集 = arr1.Intersect(arr2).ToL ...

  5. [转]list的交集,差集,并集

    原文地址:https://www.cnblogs.com/changfanchangle/p/8966860.html 工作中用到了list的取差集,发现还是挺好用的.所以记录下. 需求 list的方 ...

  6. javascript 数组求交集/差集/并集/过滤重复

    最近在小一个小程序项目,突然发现 javscript 对数组支持不是很好,连这些基本的功能,都还要自己封装.网上查了下,再结合自己的想法,封装了一下,代码如下. //数组交集 Array.protot ...

  7. C# 交集 差集 并集

    Intersect 交集,Except 差集,Union 并集 , , , , }; , , , , , }; var jiaoJi = oldArray.Intersect(newArray).To ...

  8. sqlite支持各种交集差集 并集操作了

  9. .NET的求复杂类型集合的差集、交集、并集

    前言 如标题所述,在ASP.NET应用程序开发中,两个集合做比较时 我们使用微软IEnumerable封装的 Except/Intersect/Union 取 差集/交集/并集 方法是非常的方便的: ...

随机推荐

  1. 面向对象设计:共性VS个性-------继承的粒度和聚合的粒度以及类的重构

    共性和个性 依据面向对象的原理.类是对象的抽象.也就是说.类是一系列的既有共性又有个性的对象的高度概括,类的属性和方法代表了隶属于该类的全部对象的共性,类的每一个对象实例都能够有不同的属性值,这反映了 ...

  2. Xcode9自带版本控制系统(Git)的使用

    情形一:新项目工程 前提是首先你得有个Github账号. 1.创建新工程,选中使用git,创建本地仓库. 2.在偏好设置添加git账号. 3.创建Github远程仓库,xcode会自动帮你在Githu ...

  3. 【转】 vsftp上传文件出现553 Could not create file解决方法

    因工作需要,需要搭建一个ftp服务器,我使用ubuntu 10.04操作系统,下载vsftpdy源代码, 进行了编译,安装,然后按照INSTALL文件,创建了用户等操作.    因为时间比较紧,我采用 ...

  4. PHPthinking官方论坛招募版主

    时间飞逝.就在昨天,我们PHPthinking的官方论坛刚刚上线了我们自己的论坛! 欢迎大家注冊账号,活跃在论坛的大家庭中,我们会及时关注论坛公布的全部内容.在开发学习的过程中,遇到的不论什么问题,有 ...

  5. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  6. 进程同步与相互排斥:POSIX有名信号量

    在 POSIX 标准中,信号量分两种,一种是无名信号量,一种是有名信号量. 无名信号量一般用于线程间同步或相互排斥,而有名信号量一般用于进程间同步或相互排斥. 它们的差别和管道及命名管道的差别类似.无 ...

  7. 洛谷 P3398 仓鼠找sugar —— 树链剖分

    题目:https://www.luogu.org/problemnew/show/P3398 树链剖分一下,路径就变成线段树上的几个区间: 两条路径相交就是线段树上有区间相交,所以在相应位置打个标记, ...

  8. Applications(模拟)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3705 题意:主要是分值计算要注意以下几点: (1) 在MOJ上解出的题,如 ...

  9. pcntl研究

    虽说php用于并发计算有点山寨,但总比没有强把.(有问题请指正) 下面是pcntl多线程的例子.(只能用于cli模式,而且只能用于linux环境) <?php $starttime=microt ...

  10. 湖南集训Day1

    难度 不断网:☆☆☆ 断网:☆☆☆☆ /* 卡特兰数取模 由于数据范围小,直接做. 考试时断网.忘记卡特兰数公式,推错了只有5分. 数学公式要记别每次都现用现搜!!! */ #include<i ...