Analyzing equal to customize how the entity to compare the size and determines whether the two objects exactly equal

Determined by solid portions or attributes equal magnitude comparator

Examples of custom entity equal to 1. Analyzing

a) override hashCode () and equals () Method two

b) Specific Example:

 1 class Stu{
 2     private String name = null;
 3     private int age = 0;
 4     
 5     public Stu(String name, int age){
 6         this.name = name;
 7         this.age = age;
 8     }
 9     //覆写Object中的equals方法
10     public boolean equals(Object obj){
11         if(this == obj){
12             return true;
13         }
14         if(!(obj instanceof Stu)){
15             return false;
16         }
17         Stu stu = (Stu)obj;
18         if((stu.age == this.age) && this.name.equals(stu.name)){
19             return true;
20         }
21         return false;
22     }
23     //覆写Object中的hashCode方法
24     public int hashCode(){
25         return this.name.hashCode() * this.age;
26     }
27 }

 

2. some properties (one or more fields) comparing the size of the entity instance

1) make a custom class that implements the Comparable interface

Comparable interface and implement override the compareTo () method, indicate how to sort the compareTo () method.

2) Specify the sort mode Comparator

Comparator and rewrite the incoming compare (Object o1, Object o2) method, and size comparison o1 o2 in a subject instance, if the method returns a positive integer, greater than said o1 o2, if returns 0, indicating they are equal, if the return -negative integer less than o1 o2.

      . I implement Comparable interface, particularly Example A:

 1 public class Goods implements Comparable {
 2     private String name;
 3     private double price;
 4  
 5     public Goods() {
 6     }
 7  
 8     public Goods(String name, double price) {
 9         this.name = name;
10         this.price = price;
11     }
12  
13     public String getName() {
14         return name;
15     }
16  
17     public void setName(String name) {
18         this.name = name;
19     }
20  
21     public double getPrice() {
22         return price;
23     }
24  
25     public void setPrice(double price) {
26         this.price = price;
27     }
28  
29     @Override
30     public String toString() {
31         return "Goods{" +
32                 "name = '" + name +' \ '' +
 33 is                  ",. price =" +. price +
 34 is                  '}' ;
 35      }
 36   
37 [      // specified commodity comparison of the way, according to the price from Low to High, if there the same price, then by product names sorted from low to high 
38 is      @Override
 39      public  int the compareTo (Object O) {
 40          IF (O the instanceof goods) {
 41 is              goods goods = (goods) O;
 42 is              IF ( the this .price> goods .price) {
 43 is                  return . 1 ;
 44 is              } the else IF ( the this .price < goods.price) {
 45                  return   -1 ;
 46 is              } the else 
47                  // return 0; 
48                  return  the this .name.compareTo (goods.name);
 49          }
 50         the throw  new new a RuntimeException ( "incoming data type mismatch " );
 51      }
 52 }

 

    ii. Incoming Comparator interface, a specific example B

 1    @Test
 2    public void test2(){
 3 
 4        Goods[] arr=new Goods[5];
 5        arr[0] = new Goods("lenovoMouse",34);
 6        arr[1] = new Goods("dellMouse",66);
 7        arr[2] = new Goods("xiaomiMouse",50);
 8        arr[3] = new Goods("hahaMouse",66);
 9        arr[4] = new Goods("hahaMouse",166);
10 
11        Arrays.sort(arr, new new Comparator () {
 12 is  
13 is              // specified commodity comparison of the way, according to the product name sorted from low to high, and then sorted by price Low 
14             @Override
 15             public  int Compare (Object O1, O2 Object) {
 16  
. 17                IF (O1 the instanceof   Goods O2 && the instanceof Goods) {
 18 is  
. 19                    Goods G1 = (Goods) O1;
 20 is                    Goods G2 = (Goods) O2;
 21 is                    IF (. g1.getName () the equals (g2.getName ())) {
 22 is                        return - Double.compare (g1.getPrice (), g2.getPrice ());
 23 is                    }the else {
 24                        return g1.getName () the compareTo (g2.getName ());.
 25                    }
 26 is                }
 27                 the throw  new new a RuntimeException ( "inconsistent with the data type input" );
 28             }
 29         });
 30  
31 is         System.out.println (of Arrays.toString (ARR));
 32  
33 is         / * 
34 is         run results:
 35         [Goods {name = 'dellMouse',. price = 66.0}, {Goods name = 'hahaMouse',}. price = 166.0,
 36          Goods name = { 'hahaMouse',. price = 66.0}, {Goods name = 'lenovoMouse',}. price = 34.0,
 37 [         Goods{name='xiaomiMouse', price=50.0}]
38         */
39 
40    }

 

Comparative 3) Comparable interface with the interface Comparator

Comparable interface Once specified manner, ensuring that the object class Comparable interface implemented at any location can compare the size.

 

Comparator interface is relatively temporary nature.

 

Reference data:

1. How to determine whether two objects are exactly equal

2. The two implementations of Java in comparison size

Guess you like

Origin www.cnblogs.com/bridgestone29-08/p/11258457.html