Hashcode de una cadena
Frecuentes
Visto 2,797 veces
4 Respuestas
8
Solo llama hashCode()
en el String
:
String s = "Hello World";
System.out.println(s.hashCode());
If you want it in the same format as Object.toString()
, prueba esto:
System.out.println(Integer.toHexString(s.hashCode()));
Respondido 28 ago 12, 14:08
Más precisamente: s.getClass().getName() + "@" + Integer.toHexString(s.hashCode());
- Assylias
@assylias Yes ofcourse: "String@" + Integer.toHexString(s.hashCode()));
- Jesper
@Jesper: ok, hashCode() is the member of the object class. Thnx. - Biswanath Chowdhury
2
System.out.println("Some String".hashCode());
Respondido 28 ago 12, 14:08
2
You can get the hash code of any Java object by invoking the hashCode()
method. The result will be an int
that you can then print or do anything else you want with it.
If you are interested in the implementation of Object.toString
, it is very easy to check at códigogrep. Dice:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Respondido 28 ago 12, 15:08
1
Simplemente llame al hashcode()
method. It comes from Object
.
String str = "mystring";
System.out.println(str.hashCode());
Respondido 28 ago 12, 14:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas java string object or haz tu propia pregunta.
What do you mean by "hash code of the String variable"? Why can't you simply call
hashCode()
¿en eso? - Thomas OwenstoString()
doesn't just print the hashCode() by default. You are better off callinghashCode()
if that is what you want. Note: it might not be unique. - Peter Lawreyyou don't get the hashcode, you get the objectId. - Joeri Hendrickx
@JoeriHendrickx And what would objectid be, this is not a standard Java term? - Marko Topolnik
@PetarMinchev There is no hay tal cosa as a unique
hashCode
implementation, except for objects whose total number of possible states fits into anint
. Such objects are muy raro. - Marko Topolnik