exclude函数计算_Java Stream exclude()函数删除重复项

exclude函数计算

Java Stream distinct() method returns a new stream of distinct elements. It’s useful in removing duplicate elements from the collection before processing them.

Java Stream独特的()方法返回一个新的独特元素流。 在处理重复元素之前,从集合中删除重复元素非常有用。

Java Stream different()方法 (Java Stream distinct() Method)

  • The elements are compared using the equals() method. So it’s necessary that the stream elements have proper implementation of equals() method.

    使用equals()方法比较元素。 因此,有必要使stream元素正确实现equals()方法。
  • If the stream is ordered, the encounter order is preserved. It means that the element occurring first will be present in the distinct elements stream.

    如果流是有序的,则遇到顺序将保留。 这意味着首先出现的元素将出现在不同的元素流中。
  • If the stream is unordered, then the resulting stream elements can be in any order.

    如果流是无序的,那么生成的流元素可以是任何顺序。
  • Stream distinct() is a stateful intermediate operation.

    流distinct()是有状态的中间操作。
  • Using distinct() with an ordered parallel stream can have poor performance because of significant buffering overhead. In that case, go with sequential stream processing.

    由于有大量的缓冲开销,因此对有序并行流使用distinct()可能会导致性能下降。 在这种情况下,请进行顺序流处理。

使用distinct()删除重复元素 (Remove Duplicate Elements using distinct())

Let’s see how to use stream distinct() method to remove duplicate elements from a collection.

让我们看一下如何使用流distinct()方法从集合中删除重复的元素。

jshell> List<Integer> list = List.of(1, 2, 3, 4, 3, 2, 1);
list ==> [1, 2, 3, 4, 3, 2, 1]

jshell> List<Integer> distinctInts = list.stream().distinct().collect(Collectors.toList());
distinctInts ==> [1, 2, 3, 4]
Java Stream Distinct Example

Java Stream distinct() Example

Java Stream different()示例

使用Stream different()和forEach()仅处理唯一元素 (Processing only Unique Elements using Stream distinct() and forEach())

Since distinct() is a intermediate operation, we can use forEach() method with it to process only the unique elements.

由于distinct()是中间操作,因此我们可以将forEach()方法与它一起使用以仅处理唯一元素。

jshell> List<Integer> list = List.of(1, 2, 3, 4, 3, 2, 1);
list ==> [1, 2, 3, 4, 3, 2, 1]

jshell> list.stream().distinct().forEach(x -> System.out.println("Processing " + x));
Processing 1
Processing 2
Processing 3
Processing 4
Java Stream Distinct ForEach Example

Java Stream distinct() forEach() Example

Java Stream different()forEach()示例

使用自定义对象流distinct() (Stream distinct() with custom objects)

Let’s look at a simple example of using distinct() to remove duplicate elements from a list.

让我们看一个简单的示例,该示例使用distinct()从list中删除重复的元素。

package com.journaldev.java;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class JavaStreamDistinct {

	public static void main(String[] args) {
		List<Data> dataList = new ArrayList<>();
		dataList.add(new Data(10));
		dataList.add(new Data(20));
		dataList.add(new Data(10));
		dataList.add(new Data(20));

		System.out.println("Data List = "+dataList);

		List<Data> uniqueDataList = dataList.stream().distinct().collect(Collectors.toList());

		System.out.println("Unique Data List = "+uniqueDataList);
	}

}

class Data {
	private int id;

	Data(int i) {
		this.setId(i);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Override
	public String toString() {
		return String.format("Data[%d]", this.id);
	}
}

Output:

输出:

Data List = [Data[10], Data[20], Data[10], Data[20]]
Unique Data List = [Data[10], Data[20], Data[10], Data[20]]

The distinct() method didn’t remove the duplicate elements. It’s because we didn’t implement the equals() method in the Data class. So the superclass Object equals() method was used to identify equal elements. The Object class equals() method implementation is:

unique()方法没有删除重复的元素。 这是因为我们没有在Data类中实现equals()方法。 因此,使用超类Object equals()方法来标识相等的元素。 Object类的equals()方法实现为:

public boolean equals(Object obj) {
    return (this == obj);
}

Since the Data objects had the same ids’ but they were referring to the different objects, they were considered not equal. That’s why it’s very important to implement equals() method if you are planning to use stream distinct() method with custom objects.

由于数据对象具有相同的ID,但它们引用的是不同的对象,因此认为它们不相等。 这就是为什么如果您打算对自定义对象使用stream different()方法,实现equals()方法非常重要。

Note that both equals() and hashCode() methods are used by Collection classes API to check if two objects are equal or not. So it’s better to provide an implementation for both of them.

请注意,Collection类API使用equals()和hashCode()方法来检查两个对象是否相等。 因此最好为它们两个都提供一个实现。

@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + id;
	return result;
}

@Override
public boolean equals(Object obj) {
	System.out.println("Data equals method");
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	Data other = (Data) obj;
	if (id != other.id)
		return false;
	return true;
}

Tip: You can easily generate equals() and hashCode() method using “Eclipse > Source > Generate equals() and hashCode()” menu option.

提示 :您可以使用“ Eclipse>源>生成equals()和hashCode()”菜单选项轻松地生成equals()和hashCode()方法。

The output after adding equals() and hashCode() implementation is:

添加equals()和hashCode()实现后的输出为:

Data List = [Data[10], Data[20], Data[10], Data[20]]
Data equals method
Data equals method
Unique Data List = [Data[10], Data[20

Reference: Stream distinct() API Doc

参考流distinct()API文档

翻译自: https://www.journaldev.com/31860/java-stream-distinct-function

exclude函数计算

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值