LinkedHashSet remove duplicates object

前端 未结 5 2020
梦毁少年i
梦毁少年i 2021-01-13 06:58

I have simple question to you, I have class Product that have fields like this:

private Integer id;
private String category;
private String symbol;
private S         


        
5条回答
  •  醉话见心
    2021-01-13 07:46

    As others have already said, you will need to implement (override) equals(), hashCode() and (prefarably) compareTo() from Comparable interface. These methods, if not implemented correctly can lead to unexpected runtime behavior. Hard to debug issues. Hence, I suggest you use Apache Commons EqualsBuilder, HashcodeBuilder and ComparableBuilder to implement these methods. An example of how to use Apache Commons builder can be seen in this link http://www.javaworld.com/community/node/1859

提交回复
热议问题