Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java8流在一行而不是两行中消除重复项_Java_List_Duplicates_Java Stream - Fatal编程技术网

使用Java8流在一行而不是两行中消除重复项

使用Java8流在一行而不是两行中消除重复项,java,list,duplicates,java-stream,Java,List,Duplicates,Java Stream,它可以用一行代码而不是两行代码来编写吗?因为我尝试在第一行中添加.distinct,但不知怎么的,它没有起作用。我不明白这里的区别 List<BgwContract> contractListWithDuplicates = monthlyFeePaymentList .stream() .map(MonthlyFeePayment::getBgwContract) .collect(Collectors.toList

它可以用一行代码而不是两行代码来编写吗?因为我尝试在第一行中添加.distinct,但不知怎么的,它没有起作用。我不明白这里的区别

List<BgwContract> contractListWithDuplicates = monthlyFeePaymentList
           .stream()
           .map(MonthlyFeePayment::getBgwContract)
           .collect(Collectors.toList());

List<BgwContract> contractListWithoutDuplicates = contractListWithDuplicates
           .stream()
           .distinct()
           .collect(Collectors.toList());
您可以对现有流本身使用distinct:

您可以对现有流本身使用distinct:


使用DINTINTCT,使用equals方法对元素进行比较。
您必须为BgwContract对象重写equals。

使用dintinct,使用equals方法比较元素。
您必须为您的BgwContract对象重写equals。

因为您需要关于distinct的正确位置的解释:

当你写作时:

List<BgwContract> contractListWithDuplicates = monthlyFeePaymentList
           .stream()
           .distinct()
           .map(MonthlyFeePayment::getBgwContract)
           .collect(Collectors.toList());
List<BgwContract> contractListWithDuplicates = monthlyFeePaymentList
           .stream()
           .map(MonthlyFeePayment::getBgwContract)
           .distinct()
           .collect(Collectors.toList());

您首先将MonthlyFeepPayment实例映射到BgwContract实例,然后才删除带有distinct的副本,这正是您所需要的。

因为您需要关于distinct的正确位置的解释:

当你写作时:

List<BgwContract> contractListWithDuplicates = monthlyFeePaymentList
           .stream()
           .distinct()
           .map(MonthlyFeePayment::getBgwContract)
           .collect(Collectors.toList());
List<BgwContract> contractListWithDuplicates = monthlyFeePaymentList
           .stream()
           .map(MonthlyFeePayment::getBgwContract)
           .distinct()
           .collect(Collectors.toList());

您首先将MonthlyFeepPayment实例映射到BgwContract实例,然后才删除带有distinct的重复项,这是您所需要的。

哦,好的,我将其放在前面。map。你能解释一下为什么它不起作用吗?因为据我所知,我想区分流,它是一个列表,而不是地图。我添加了一个解释。好的,我把它放在了。地图之前。你能解释一下为什么它不起作用吗?因为据我所知,我想区分流,它是一个列表,而不是地图