<bdo id='7BpLo'></bdo><ul id='7BpLo'></ul>

      <small id='7BpLo'></small><noframes id='7BpLo'>

    1. <legend id='7BpLo'><style id='7BpLo'><dir id='7BpLo'><q id='7BpLo'></q></dir></style></legend>
        <tfoot id='7BpLo'></tfoot>

        <i id='7BpLo'><tr id='7BpLo'><dt id='7BpLo'><q id='7BpLo'><span id='7BpLo'><b id='7BpLo'><form id='7BpLo'><ins id='7BpLo'></ins><ul id='7BpLo'></ul><sub id='7BpLo'></sub></form><legend id='7BpLo'></legend><bdo id='7BpLo'><pre id='7BpLo'><center id='7BpLo'></center></pre></bdo></b><th id='7BpLo'></th></span></q></dt></tr></i><div id='7BpLo'><tfoot id='7BpLo'></tfoot><dl id='7BpLo'><fieldset id='7BpLo'></fieldset></dl></div>
      1. LinkedHashSet 删除重复对象

        LinkedHashSet remove duplicates object(LinkedHashSet 删除重复对象)

          • <bdo id='PCRxD'></bdo><ul id='PCRxD'></ul>
                  <tfoot id='PCRxD'></tfoot>
                • <legend id='PCRxD'><style id='PCRxD'><dir id='PCRxD'><q id='PCRxD'></q></dir></style></legend>
                  <i id='PCRxD'><tr id='PCRxD'><dt id='PCRxD'><q id='PCRxD'><span id='PCRxD'><b id='PCRxD'><form id='PCRxD'><ins id='PCRxD'></ins><ul id='PCRxD'></ul><sub id='PCRxD'></sub></form><legend id='PCRxD'></legend><bdo id='PCRxD'><pre id='PCRxD'><center id='PCRxD'></center></pre></bdo></b><th id='PCRxD'></th></span></q></dt></tr></i><div id='PCRxD'><tfoot id='PCRxD'></tfoot><dl id='PCRxD'><fieldset id='PCRxD'></fieldset></dl></div>
                    <tbody id='PCRxD'></tbody>
                • <small id='PCRxD'></small><noframes id='PCRxD'>

                • 本文介绍了LinkedHashSet 删除重复对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  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 String desc;
                  private Double price;
                  private Integer quantity;
                  

                  I want to remove duplicates item from LinkedHasSet based on ID, e.g Products that have same ID but diffrent quantity will be add to set, I want to remove (update) products with same ID, and it will by my unique id of object, how to do that?

                  e.g Product: id=1, category=CCTV, symbol=TVC-DS, desc=Simple Camera, price=100.00, quantity=1 Product: id=1, category=CCTV, symbol=TVC-DS, desc=Simple Camera, price=100.00, quantity=3

                  won't be added to set

                  my code:

                      public void setList(Set<Product> list) {
                      if(list.isEmpty()) 
                          this.list = list;
                      else {
                          this.list.addAll(list);
                          Iterator<Product> it = this.list.iterator();
                          for(Product p : list) {
                              while(it.hasNext()) {
                                  if(it.next().getId() != p.getId())
                                      it.remove();
                                      this.list.add(p);   
                              }
                          }
                      }
                  }
                  

                  解决方案

                  All Set implementations remove duplicates, and the LinkedHashSet is no exception.

                  The definition of duplicate is two objects that are equal to each other, according to their equals() method. If you haven't overridden equals on your Product class, then only identical references will be considered equal - not different instances with the same values.

                  So you need to add a more specific implementation of equals (and hashcode) for your class. For some examples and guidance, see Overriding equals and hashcode in Java. (Note that you must override hashcode as well, otherwise your class will not behave correctly in hash sets.)

                  这篇关于LinkedHashSet 删除重复对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                  • <tfoot id='q5JR0'></tfoot>

                          <bdo id='q5JR0'></bdo><ul id='q5JR0'></ul>
                        • <i id='q5JR0'><tr id='q5JR0'><dt id='q5JR0'><q id='q5JR0'><span id='q5JR0'><b id='q5JR0'><form id='q5JR0'><ins id='q5JR0'></ins><ul id='q5JR0'></ul><sub id='q5JR0'></sub></form><legend id='q5JR0'></legend><bdo id='q5JR0'><pre id='q5JR0'><center id='q5JR0'></center></pre></bdo></b><th id='q5JR0'></th></span></q></dt></tr></i><div id='q5JR0'><tfoot id='q5JR0'></tfoot><dl id='q5JR0'><fieldset id='q5JR0'></fieldset></dl></div>
                              <tbody id='q5JR0'></tbody>

                            <small id='q5JR0'></small><noframes id='q5JR0'>

                            <legend id='q5JR0'><style id='q5JR0'><dir id='q5JR0'><q id='q5JR0'></q></dir></style></legend>