如何从列表中删除重复项?

huangapple go评论75阅读模式
英文:

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&lt;M3MachineSale&gt; updatedM3MachineSaleList = m3MachineSaleList.stream()
                .filter(m3MachineSale -&gt; !m3MachineSale.getOPITNO().equals(&quot;PANT L&quot;))
                .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 &lt;T&gt; Predicate&lt;T&gt; distinctByKey(Function&lt;? super T, Object&gt; keyExtractor) {
    Map&lt;Object, Boolean&gt; seen = new ConcurrentHashMap&lt;&gt;();
    return t -&gt; 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&lt;M3MachineSale&gt; updatedM3MachineSaleList = m3MachineSaleList.stream()
    .filter(m3MachineSale  -&gt; !m3MachineSale.getOPITNO().equals(&quot;PANT L&quot;))
    .distinct().collect(Collectors.toList());

Working example here https://onlinegdb.com/BJA-h5z7P

huangapple
  • 本文由 发表于 2020年8月25日 20:58:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63579381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定