Java Objects hash code

Description

Java Objects hash code

import java.util.Objects;

public class Main {

   public static void main(String[] args) {
      int code = Objects.hashCode("asdf");
      System.out.println(code);//from w  w  w.j  av  a2  s.  co  m

      code = Objects.hashCode(null);
      System.out.println(code);
   }

}

Use Objects.hashCode() in class

 Code:
 import java.util.Objects;
             /*from w  w w  .j a v a 2  s. c  o  m*/
 class Item {
    private String name;
    private int partNumber;
             
    public Item() {
       this("Widget", 0);
    }
             
    public Item(String name, int partNumber) {
       this.name = Objects.requireNonNull(name);
       this.partNumber = partNumber;
    }
             
    public String getName() {
       return name;
    }
             
    public void setName(String name) {
 //      this.name = Objects.requireNonNull(name);
         this.name = Objects.requireNonNull(name, "The name field requires a non-null value");        
    }
             
    public int getPartNumber() {
       return partNumber;
    }
             
    public void setPartNumber(int partNumber) {
       this.partNumber = partNumber;
    }
             
    @Override
    public boolean equals(Object obj) {
       if (obj == null) {
          return false;
       }
       if (getClass() != obj.getClass()) {
          return false;
       }
       final Item other = (Item) obj;
       if (!Objects.equals(this.name, other.name)) {
          return false;
       }
       if (this.partNumber != other.partNumber) {
          return false;
       }
       return true;
    }
             
    @Override
    public int hashCode() {
       int hash = 7;
       hash = 47 * hash + Objects.hashCode(this.name);
       hash = 47 * hash + this.partNumber;
       return hash;
    }
             
    @Override
    public String toString() {
       return name + " - " + partNumber;
    }
             
 }
             
 public class Main {
             
    public static void main(String[] args) {
       Item item1 = new Item("CSS", 2200);
       Item item2 = new Item("CSS2", 1200);
       Item item3 = new Item("HTML", 1100);
       Item item4 = null;
             
       System.out.println("item1 equals item1: " + item1.equals(item1));
       System.out.println("item1 equals item2: " + item1.equals(item2));
       System.out.println("item1 equals item3: " + item1.equals(item3));
       System.out.println("item1 equals item4: " + item1.equals(item4));
             
       item2.setName(null);
       System.out.println("item1 equals item2: " + item1.equals(item2));
             
    }
             
 }



PreviousNext

Related