两个List<int> 数据高速去重
时间: 2023-06-27 14:05:49 浏览: 39
可以使用 HashSet<T> 来高效地去重,具体实现代码如下:
```csharp
List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
List<int> list2 = new List<int> { 3, 4, 5, 6, 7 };
HashSet<int> set = new HashSet<int>(list1);
set.UnionWith(list2);
List<int> result = set.ToList();
```
这段代码中,首先将第一个 List<int> 转化为 HashSet<int>,然后使用 UnionWith 方法将第二个 List<int> 并入 HashSet<int> 中,这个方法会自动去重。最后,将去重后的 HashSet<int> 转化为 List<int> 即可得到最终结果。
相关问题
java 若类T有3个字段biz,title,updateTime,假设他们三个字段相同的两个对象则视为重复,如何在List<T>中进行去重重复操作
可以重写T类的equals和hashCode方法来实现List<T>中去重复操作,具体实现如下:
```java
public class T {
private String biz;
private String title;
private Date updateTime;
// 省略构造方法和getter/setter方法
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (obj.getClass() != getClass()) {
return false;
}
T other = (T) obj;
return biz.equals(other.biz) && title.equals(other.title) && updateTime.equals(other.updateTime);
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + biz.hashCode();
result = 31 * result + title.hashCode();
result = 31 * result + updateTime.hashCode();
return result;
}
}
```
重写了equals和hashCode方法后,可以使用Java 8中的Stream API来进行去重复操作,代码如下:
```java
List<T> list = new ArrayList<>();
// 添加T对象到list中
List<T> distinctList = list.stream().distinct().collect(Collectors.toList());
```
这样得到的distinctList中就不存在重复的T对象了。
JAVA代码实现 两个List<String>类型合并,删除重复元素,按照第二个List<String>列表排序
下面是 Java 代码实现,将两个 List<String> 类型的列表合并成一个列表,删除重复元素,按照第二个 List<String> 列表排序:
```
List<String> list1 = new ArrayList<>();
list1.add("apple");
list1.add("banana");
list1.add("orange");
List<String> list2 = new ArrayList<>();
list2.add("banana");
list2.add("grape");
list2.add("apple");
List<String> list3 = new ArrayList<>();
list3.addAll(list1);
list3.addAll(list2);
// 删除重复元素
List<String> distinctList = list3.stream().distinct().collect(Collectors.toList());
// 按照 list2 列表排序
distinctList.sort((a, b) -> {
int indexA = list2.indexOf(a);
int indexB = list2.indexOf(b);
return Integer.compare(indexA, indexB);
});
System.out.println(distinctList);
```
这段代码中,我们先创建了两个 List<String> 类型的列表 list1 和 list2,然后使用 addAll() 方法将它们合并成一个列表 list3。接下来,我们使用 Java 8 中的流式 API 将列表中的重复元素去重,得到 distinctList 列表。最后,我们使用 sort() 方法按照 list2 列表中元素的顺序对 distinctList 列表进行排序。最终输出排序后的列表。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)