HashCode For String : HashCodeBuilder « Apache Common « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Apache Common » HashCodeBuilder 
37.16.1.HashCode For StringPrevious/Next
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;


public class MainClass {
  public static void main(String[] args) {
    //Create new BuilderTrial instances
    BuilderTrial one = new BuilderTrial("Becker"35);
    BuilderTrial two = new BuilderTrial("Becker"35);
    BuilderTrial three = new BuilderTrial("Agassi"33);

    //one and two hold the same data in different objects
    //three holds different data
    System.out.println("One>>>" + one);
    System.out.println("Two>>>" + two);
    System.out.println("Three>>>" + three);

    System.out.println("one equals two? " + one.equals(two));
    System.out.println("one equals three? " + one.equals(three));

    //As one and two hold the same data, the same hashcode is returned.
    System.out.println("One HashCode>>> " + one.hashCode());
    System.out.println("Two HashCode>>> " + two.hashCode());
    System.out.println("Three HashCode>>> " + three.hashCode());
  }
}

class BuilderTrial {
  private String name;
  private int age;

  public BuilderTrial(String name, int age) {
      this.name = name;
      this.age = age;
  }

  public static void main(String[] args) {
  }

  public boolean equals(Object objCompared) {
      if (!(objCompared instanceof BuilderTrial)) {
          return false;
      }

      BuilderTrial rhs = (BuilderTrialobjCompared;

      return new EqualsBuilder().append(name, rhs.name).append(age, rhs.age)
                                .isEquals();
  }

  public String toString() {
      return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("Name",
          name).append("Age", age).toString();
  }

  public int hashCode() {
      return new HashCodeBuilder(1519).append(name).append(age).toHashCode();
  }
}
One>>>BuilderTrial@a981ca[
  Name=Becker
  Age=35
]
Two>>>BuilderTrial@e7b241[
  Name=Becker
  Age=35
]
Three>>>BuilderTrial@167d940[
  Name=Agassi
  Age=33
]
one equals two? true
one equals three? false
One HashCode>>> -923455822
Two HashCode>>> -923455822
Three HashCode>>> -1433293806
  Download:  CommonLangHashCodeForString.zip( 200 k)
37.16.HashCodeBuilder
37.16.1.HashCode For String
37.16.2.HashCodeBuilder.reflectionHashCode
37.16.3.HashCode Builder
37.16.4.Custom HashCode Builder
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.