You can use String.hashCode().
String a1 = "Hello World";
String a2 = new String(a1); // don't do this unless you have to have a different object.
System.out.println("Identity hashCode " + System.identityHashCode(a1) + " != " + System.identityHashCode(a2));
System.out.println("String.hashCode " + a1.hashCode() + " == " + a2.hashCode());
prints
Identity hashCode 551677275 != 1353056826
String.hashCode -862545276 == -862545276
In terms of performance, hashCode() is much faster than creating the String itself. If this is not fast enough, I would avoid using/creating String in the first place. (highly unlikely you need to so this)
The identity hash code changes each time the program is run. Note: hashCode() can be negative so you have to adjust for this.
int hash = (text.hashCode() & 0x7FFFFFFF) % K + 1;
or if you don't want to discard the top bit
int hash = (int) ((text.hashCode() & 0xFFFFFFFFL) % K + 1);