The single or multiple fields to the weight of the object list

pojo omitted  

In the object list, performed according to a weight field, rewriting Comparator

1      / * 
2       * deduplication
 . 3       * 
 . 4       * @param OrderList
 . 5       * @return 
. 6       * @author Ziggo
 . 7       * / 
. 8      Private  static List <Drug> removeDuplicateOrder (List <Drug> OrderList) {
 . 9          the Set <Drug> SET = new new TreeSet <Drug> ( new new Comparator <Drug> () {
 10              @Override
 . 11              public  int Compare (Drug A, Drug B) {
 12 is                  // string is arranged in ascending order of code asicc 
13                 return a.getName().compareTo(b.getName());
14             }
15         });
17         set.addAll(orderList);
18         return new ArrayList<Drug>(set);
19     }

In the object list, a plurality of fields according to weight, rewriting Comparator

    /**
     * Deduplication (abandoned, under many conditions inexplicable failure)
     * 
     * @param orderList
     * @return
     * @author ziggo
     */
    private static List<Drug> removeDuplicateOrder(List<Drug> orderList) {
        Set<Drug> set = new TreeSet<Drug>(new Comparator<Drug>() {
            @Override
            public  int Compare (Drug A, Drug B) {
                 int compareToResult =. 1; // == 0 represents a repeating
                 // string is arranged in ascending asicc code 
                IF (a.getName () the equals (b.getName ()) &&. a.getDepartment (). equals (b.getDepartment ( ))) {
                    compareToResult = 0 ;
                }
                return compareToResult;
            }
        });

        set.addAll(orderList);
        return new ArrayList<Drug>(set);
    }

Comparator rewritten according to weight a plurality of fields, this method was found not to normal weight, yet to troubleshoot what reason, then look for another way, the override equals method pojo

Entity classes: DRUG

package com.msun.mr.app.pojo;

/ ** 
 * @author d_hb drug information
  * / 
public  class Drug {
     / **

 Field is omitted portion
********************************


*/
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        Drug drug = (Drug) obj;
        if (this.getName() .compareTo(drug.getName())==0
                && this.getDepartment().equals(drug.getDepartment()) && this.getSpec_id().equals(drug.getSpec_id())){
            return true;
        }
        return false;
    }
}

 

 When the determination logic is repeated, while meeting the three fields, deduplication

    // deduplication (override equals Method) 
    Private  static List <Drug> removeDuplicateInvoice (List <Drug> OrderList) {
        List<Drug> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(orderList)) {
            for (Drug drug : orderList) {
                // list去重复,内部重写equals
                if (!list.contains(drug)) {
                    list.add(drug);
                }
            }
        }
        return list;
    }

Therefore, the current de-duplication logic codes used are: a single field to the Comparator heavy rewriting, when multiple fields deduplication, override equals method but I feel more fields to re-use the same method to rewrite Comparator can be achieved. temporarily not locate the problem ............

 

Guess you like

Origin www.cnblogs.com/by-xu/p/11683742.html