英文:
How to remove duplicates from the list?
问题
@Slf4j
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class M3MachineSale {
private Integer OPCSHC;
private String OPITNO;
private String OPTRDT;
private Integer OPIVQT;
}
// Remove duplicates and objects with Product = PANT L
List<M3MachineSale> updatedM3MachineSaleList = m3MachineSaleList.stream()
.filter(m3MachineSale -> !m3MachineSale.getOPITNO().equals("PANT L"))
.distinct()
.collect(Collectors.toList());
Input list:
M3MachineSale(OPCSHC=123, OPITNO=5562, OPTRDT=20200821, OPIVQT=1)
M3MachineSale(OPCSHC=123, OPITNO=5562, OPTRDT=20200821, OPIVQT=1)
M3MachineSale(OPCSHC=456, OPITNO=3497, OPTRDT=20200821, OPIVQT=15)
M3MachineSale(OPCSHC=789, OPITNO=6663, OPTRDT=20200821, OPIVQT=4)
M3MachineSale(OPCSHC=456, OPITNO=3497, OPTRDT=20200821, OPIVQT=15)
Output should be:
M3MachineSale(OPCSHC=123, OPITNO=5562, OPTRDT=20200821, OPIVQT=1)
M3MachineSale(OPCSHC=456, OPITNO=3497, OPTRDT=20200821, OPIVQT=15)
M3MachineSale(OPCSHC=789, OPITNO=6663, OPTRDT=20200821, OPIVQT=4)
You can also use the distinctByKey method you provided to find duplicates:
private <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.FALSE) == null;
}
List<M3MachineSale> uniqueM3MachineSaleList = m3MachineSaleList.stream()
.filter(m3MachineSale -> !m3MachineSale.getOPITNO().equals("PANT L"))
.filter(distinctByKey(m3MachineSale -> Arrays.asList(
m3MachineSale.getOPCSHC(),
m3MachineSale.getOPITNO(),
m3MachineSale.getOPTRDT(),
m3MachineSale.getOPIVQT())))
.collect(Collectors.toList());
This will give you a list without duplicates based on the specified attributes.
英文:
I have a object class with these attributes:
@Slf4j
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class M3MachineSale {
private Integer OPCSHC;
private String OPITNO;
private String OPTRDT;
private Integer OPIVQT;
}
I want to remove all the duplicates. I have used .distinct()
but it does not remove duplicates, only give me back the duplicates from the list.
// Remove duplicates and objects with Product = PANT L
List<M3MachineSale> updatedM3MachineSaleList = m3MachineSaleList.stream()
.filter(m3MachineSale -> !m3MachineSale.getOPITNO().equals("PANT L"))
.collect(Collectors.toList());
What am I doing worong? I know I have some objects with same attributes, but I want to remove these from the list, not return the duplicates.
Input list:
M3MachineSale(OPCSHC=123, OPITNO=5562, OPTRDT=20200821, OPIVQT=1)
M3MachineSale(OPCSHC=123, OPITNO=5562, OPTRDT=20200821, OPIVQT=1)
M3MachineSale(OPCSHC=456, OPITNO=3497, OPTRDT=20200821, OPIVQT=15)
M3MachineSale(OPCSHC=789, OPITNO=6663, OPTRDT=20200821, OPIVQT=4)
M3MachineSale(OPCSHC=456, OPITNO=3497, OPTRDT=20200821, OPIVQT=15)
Output should be:
M3MachineSale(OPCSHC=123, OPITNO=5562, OPTRDT=20200821, OPIVQT=1)
M3MachineSale(OPCSHC=456, OPITNO=3497, OPTRDT=20200821, OPIVQT=15)
M3MachineSale(OPCSHC=789, OPITNO=6663, OPTRDT=20200821, OPIVQT=4)
I have tried do this to find duplicates:
private <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.FALSE) == null;
}
And it returned a list that i have 605 duplicates.
Example from Baekdung
Any idea how I can do it? Thank for the help!
答案1
得分: 1
在你的洛姆克注解圣诞树中,你忘记了 @EqualsAndHashCode
,它使得去重不可能。
我建议你将批处理缩减为仅使用 @Data @Slf4j @NoArgsConstructor @AllArgsConstructor
,这些注解可以完成所有操作。
英文:
In your christmas tree of lombok annotations, you forgot @EqualsAndHashCode
which makes de-duplication not possible.
I suggest you reduce your batch down to just @Data @Slf4j @NoArgsConstructor @AllArgsConstructor
which does all that.
答案2
得分: 0
尝试这个方法。在使用collect()之前,我成功地通过distinct()移除了重复项。
List<M3MachineSale> updatedM3MachineSaleList = m3MachineSaleList.stream()
.filter(m3MachineSale -> !m3MachineSale.getOPITNO().equals("PANT L"))
.distinct().collect(Collectors.toList());
实际工作示例在这里 https://onlinegdb.com/BJA-h5z7P
英文:
Try this approach. I was able to remove duplicates with distinct() before collect().
List<M3MachineSale> updatedM3MachineSaleList = m3MachineSaleList.stream()
.filter(m3MachineSale -> !m3MachineSale.getOPITNO().equals("PANT L"))
.distinct().collect(Collectors.toList());
Working example here https://onlinegdb.com/BJA-h5z7P
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论