集合数据快速取交集、差集...

在大规模数据处理中,传统的集合操作效率低下。本文介绍了如何利用Guava库来高效地计算Java集合的交集和差集。通过集成Guava并使用`Sets.difference()`和`Sets.intersection()`方法,实现了数十万条数据的快速运算,显著提升了性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JAVA集合快速取交集方法

在实际业务开发中,遇见集合取交集、差集的数据,每个集合几十万条左右数据,利用传统方式取效率低的可怕,在网上找过很多的方法,无意中看到Guava,是谷歌出的一个工具类,经过验证,效率高的让人意想不到。

项目集成

pom文件

 <dependency>
  	<groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
 </dependency>

项目实例

   List<String> personList = new ArrayList<>();
   List<String> personOldList = new ArrayList<>();
   Set person = new HashSet<>(personList);
   Set personOld = new HashSet<>(personOldList);
   //取差集
   Set<String> result3 = Sets.difference(person, personOld);
   System.out.println(result3.size());
   //取交集
   Set<String> result4 = Sets.intersection(person, personOld);
   System.out.println(result4.size()); 

源码

有很多实用的方法,有兴趣的可以自己查阅了。

    @java.lang.Deprecated
    public static <E> java.util.Set<E> newSetFromMap(java.util.Map<E,java.lang.Boolean> map) { /* compiled code */ }

    public static <E> com.google.common.collect.Sets.SetView<E> union(java.util.Set<? extends E> set1, java.util.Set<? extends E> set2) { /* compiled code */ }

    public static <E> com.google.common.collect.Sets.SetView<E> intersection(java.util.Set<E> set1, java.util.Set<?> set2) { /* compiled code */ }

    public static <E> com.google.common.collect.Sets.SetView<E> difference(java.util.Set<E> set1, java.util.Set<?> set2) { /* compiled code */ }

    public static <E> com.google.common.collect.Sets.SetView<E> symmetricDifference(java.util.Set<? extends E> set1, java.util.Set<? extends E> set2) { /* compiled code */ }

在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、付费专栏及课程。

余额充值