多个集合取交集、并集、差集值相同,比对却不同

集合的打印结果:

集合一:

[
    {
        "cjsj": 1632972963000,
        "dm": "a2i1d",
        "id": "becb31ad-c6d9-42d4-bde9-60363f57e89b",
        "mc": "测试2222",
        "sfdm": "1101111",
        "zt": 0
    },
    {
        "cjsj": 1621220076000,
        "dm": "30010",
        "id": "4e4c4a14-2678-4915-b2dd-9acf767ffaf4",
        "mc": "名称2",
        "zt": 0
    }
]

集合二:

[
    {
        "cjsj": 1632972963000,
        "dm": "a2i1d",
        "id": "becb31ad-c6d9-42d4-bde9-60363f57e89b",
        "mc": "测试2222",
        "sfdm": "1101111",
        "zt": 0
    }
]

取交集结果为:

交集为:[]
对象数值是否相同:false
问题原因:两个集合中的对象是否相等,比对的是对象的地址值
解决:对象里面重写equals方法和 hashCode方法
解决详细:点击

在对象中重写方法:

@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;//地址相等
		//非空性:对于任意非空引用x,x.equals(null)应该返回false。
		if (obj == null) return false;
		if (obj instanceof MpSxpz) {
			MpSxpz other = (MpSxpz) obj;
			//需要比较的字段相等,则这两个对象相等
			if (equalsStr(this.id, other.id)
					&& equalsStr(this.dm, other.dm)
					&& equalsStr(this.fid, other.fid)
					&& equalsStr(this.sfdm, other.sfdm)) {
				return true;
			}
		}
		return false;
	}

	private boolean equalsStr(String str1, String str2) {
		if (StringUtils.isEmpty(str1) && StringUtils.isEmpty(str2)) {
			return true;
		}
		if (!StringUtils.isEmpty(str1) && str1.equals(str2)) {
			return true;
		}
		return false;
	}

	@Override
	public int hashCode() {
		int result = 17;
		result = 31 * result + (id == null ? 0 : id.hashCode());
		result = 31 * result + (dm == null ? 0 : dm.hashCode());
		result = 31 * result + (fid == null ? 0 : fid.hashCode());
		result = 31 * result + (sfdm == null ? 0 : sfdm.hashCode());
		return result;
	}

重写之后输出结果:

交集为:[{"cjsj":1632972963000,"dm":"a2i1d","id":"becb31ad-c6d9-42d4-bde9-60363f57e89b","mc":"测试2222","sfdm":"1101111","zt":0}]
对象是否数值相同:true

.
.
.

就此完成!!!

在C语言中,处理数据结构特别是集合(如数组、链表或哈希表等)的并集交集差集操作,可以使用多种方法,具体决于数据存储的方式。以下是基本的思路: 1. **集合的表示**:一种常见的做法是将集合视为无序的元素列表。例如,可以使用数组或动态数组(如`int*`指针和大小)来存储整数元素。 2. **交集**: - 对于两个已排序的集合,可以使用双指针法遍历两个数组,找到第一个匹配的元素,并将其添加到结果数组中。遍历结束后,结果就是交集。 3. **并集**: - 可以创建一个新的空数组(或列表),然后将两个集合的所有元素都添加进去,避免重复即可。 4. **差集**: - 如果其中一个集合是另一个集合的子集,直接从大集合中移除小集合的元素即可得到差集。否则,需要分别计算两个集合并集再减去较小的那个集合。 5. **哈希表/集合**(如果使用更高级的数据结构): - 使用哈希表可以快速地进行查找和插入操作,使得上述操作更为高效,尤其是对于大量数据。 ```c #include <stdio.h> #include <stdlib.h> // 假设集合元素是整数,且用数组表示 void union_set(int *set1, int *set2, int n1, int m1, int n2, int *result, int *result_size) { // ... 实现并集... } void intersection_set(int *set1, int *set2, int n1, int m1, int n2, int *result, int *result_size) { // ... 实现交集... } void difference_set(int *set1, int *set2, int n1, int m1, int n2, int *result, int *result_size) { // ... 实现差集... } // 示例函数 void print_set(int *set, int size) { for (int i = 0; i < size; ++i) printf("%d ", set[i]); printf("\n"); } int main() { int set1[] = {1, 2, 3, 4, 5}; int set2[] = {4, 5, 6, 7}; int n1 = sizeof(set1) / sizeof(set1[0]); int n2 = sizeof(set2) / sizeof(set2[0]); int result[100]; // 假设最大并集长度不超过100 int result_size = 0; intersection_set(set1, set2, n1, n2, result, &result_size); print_set(result, result_size); union_set(set1, set2, n1, n2, result, &result_size); print_set(result, result_size); difference_set(set1, set2, n1, n2, result, &result_size); print_set(result, result_size); return 0; } ``` 注意:这只是一个简化版本的示例,实际实现会更复杂,包括错误检查和边界条件处理。同时,这个代码假设了输入集合没有重复元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值