0

I need to create a Unique Integer from a String in Java so that collision chances are pretty less.

Is there any way, we can create a integer in Java which is always Unique for the same string?

I have to store the lacks of string in database, so i want to convert into hashcode for less retrieving time where I fire SELECT query..

Satish Kr
  • 590
  • 5
  • 12
  • 3
    So, you need to take a string as input, and produce a unique integer? Do you mean like the [hashcode](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#hashCode--)? – 4castle Nov 08 '16 at 04:56
  • Possible duplicate of [how can i generate a unique int from a unique string?](http://stackoverflow.com/questions/5459436/how-can-i-generate-a-unique-int-from-a-unique-string) – 4castle Nov 08 '16 at 05:06
  • I have to store the lacks of string in database, so i want to convert into hashcode for less retrieving time – Satish Kr Nov 08 '16 at 05:07
  • If you need a faster SELECT query, just put an index on the column you're searching. The database will handle all of the hashing for you. – 4castle Nov 08 '16 at 05:10
  • So no need of hashing only index will do? – Satish Kr Nov 08 '16 at 05:33
  • Exactly. Database engines are optimized for this purpose. If you implement your own hashing and put the hash in a separate column, you will still need to put an index on that hashed column. – 4castle Nov 08 '16 at 05:36

3 Answers3

8

"always unique"? No.

What you're talking about is basically hashing and, unless your strings have no more information content (i.e., bits) than your integers, you cannot guarantee uniqueness.

The best you can hope for is some reasonably balanced loading factor and there are many general purpose hashing functions available on the net.

Java's Object class actually provides a .hashCode() method which most sub-classes can override if they want different behaviour.

And, in fact, Java's String class does exactly that.

So you can simply do something like:

String str = "My hovercraft is full of eels";
int code = str.hashCode();
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    "Siblings" and "Teheran" have the same hashcode. – xenteros Nov 08 '16 at 05:39
  • 1
    Of course. There are always collisions when reducing the number of bits available. – Jim Garrison Nov 08 '16 at 06:16
  • 1
    @xenteros: yes, see my second and third paragraphs :-) Even if the character set were limited to alphas (52 upper/lower letters), each character there would require just shy of six bits. That's ~48 bits of information and, since a Java int has only 32 bits, there's bound to be collisions. In any case, a single Java character is *not* limited to alphas, each requires 16 bits. Given that info seems to have come from http://stackoverflow.com/questions/9406775/why-does-string-hashcode-in-java-have-many-conflicts, you should mention that they were the only two collisions amongst 58,000 words. – paxdiablo Nov 08 '16 at 06:50
0

See this example

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str = new String("Welcome to Tutorialspoint.com");
      System.out.println("Hashcode for Str :" + Str.hashCode() );
   }
}

If srring will be same then it will produce the same hash code Reference.

You can also check it How to generate a unique hash code for string input in android...?

Community
  • 1
  • 1
xrcwrn
  • 5,339
  • 17
  • 68
  • 129
-1

loop the string and get one char from string and get the ascii of each charter and combine the ascii . it will give unique integer. below is the code how to get ascii from a character.

String name = "admin";
char character = name.charAt(0); // This gives the character 'a'
int ascii = (int) character; // ascii is now 97.
Nasim Bahar
  • 115
  • 3
  • 13