<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 删除重复对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)

                  • <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>