-1

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..

6
  • 3
    So, you need to take a string as input, and produce a unique integer? Do you mean like the hashcode? Commented Nov 8, 2016 at 4:56
  • Possible duplicate of how can i generate a unique int from a unique string? Commented Nov 8, 2016 at 5:06
  • I have to store the lacks of string in database, so i want to convert into hashcode for less retrieving time Commented Nov 8, 2016 at 5: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. Commented Nov 8, 2016 at 5:10
  • So no need of hashing only index will do? Commented Nov 8, 2016 at 5:33

3 Answers 3

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();
Sign up to request clarification or add additional context in comments.

3 Comments

"Siblings" and "Teheran" have the same hashcode.
Of course. There are always collisions when reducing the number of bits available.
@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 stackoverflow.com/questions/9406775/…, you should mention that they were the only two collisions amongst 58,000 words.
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...?

Comments

-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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.