<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 删除重复对象

        时间:2023-10-14

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

                  上一篇:HashSet contains() 方法 下一篇:如何定义我自己的元素类以与 Set 一起使用

                  相关文章

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

                        <bdo id='q5JR0'></bdo><ul id='q5JR0'></ul>
                    1. <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>

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

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