[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

int representation of a string

Hello experts,

I need to take a unique id which is a string and convert it into an integer. How can I do that and ensure the integer is also unique?

Cheers,
Matt.
0
mb2297
Asked:
mb2297
  • 2
  • 2
1 Solution
 
objectsCommented:
String sid = "123";
int id = Integer.parseInt(sid);

unique according to what exactly?
0
 
mb2297Author Commented:
Sorry, I should have said the String is alphanumeric - parseInt will throw a NumberFormatException in that case.

I'm basically trying to override Object's hashCode() function.

Each of my objects is a row in a table, so they have a row identifier (an int) and a table identifier (a String). I want to concatenate the two together to form an id which can uniquely id any row in any table.

However, hashCode() requires that you return an int - so I need to parse that unique alphanumeric id into a unique integer id.

Hope that makes sense. I should have been more clear, sorry.

Thanks,
Matt.
0
 
objectsCommented:
return value from hashCode does not need to be unique.
you could just return the hashCode() of the concatenated string you have
0
 
mb2297Author Commented:
Hello again objects.

In the interest of HashMap efficiency, I would like the codes to be unique, and returning the code of the concatentated string works just fine.

Many thanks!
Matt.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now