-6

In java, I need a hash function or algorithm that generate the number from the specified input string in such a way that the generated number should be in the range like 1000 to 6000.

Condition to satisfy : 1) The algorithm should always reproduce the same number for same string. 2) Algorithm should generate unique positive number for each different strings 3) The number of input string is restricted to the range. i.e if range is 5000, then i will allow only 5000 input strings to algorithm

Thanks in advance.

Is there any built in API available, please refer that also.

Vinoth
  • 7
  • 2
  • 10
  • 1
    Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/136759/discussion-on-question-by-vinoth-java-is-there-any-hash-function-available-for). – Bhargav Rao Feb 27 '17 at 13:36

2 Answers2

3

You could do something like this:

public class MyDataStructure {

    private int size, counter = 0, start;
    private Map<String, Integer> map;

    public MyDataStructure(int size, int start) {
        this.size = size;
        this.start = start;
        map = new HashMap<>();
    }

    public int add(String s) throws LimitExceededException {
        if (map.containsKey(s)) {
            return map.get(s);
        } else if (counter >= size) {
            throw new LimitExceededException();
        } else {
            map.put(s, counter + start);
            return counter++ + start;
        }
    }

}

So in your case you would initialize with new MyDataStructure(5000, 1000) and then you add strings or get their corresponding number with the add function.

maraca
  • 8,468
  • 3
  • 23
  • 45
  • 1
    Generally agreed. But remember, OP later added *"And User may also delete the input string as well"* – domsson Feb 27 '17 at 12:14
  • 1
    @domdom That would involve having to store something along the lines of `class Slot(String, int) {}`, and keeping track of empty slots, which isn't particularly hard, I think. But since original question did not include that part, I'd say that maraca's answer is just fine. – M. Prokhorov Feb 27 '17 at 12:20
  • @M.Prokhorov Agreed and it got my upvote, too. – domsson Feb 27 '17 at 12:25
  • 1
    I agree with both of you. If we go with a "slot" solution then we are very close to the database that was also suggested in the comments. – maraca Feb 27 '17 at 12:26
  • @M.Prokhorov domdom. This is fine for successful scenario, where i will not let down the server.. But in failover scenario, This map will get empty, then i have to populate this map. but we dont know the order by which the input strings are created, So we cannot populate exact map like before.. So this will not satisfy my concern – Vinoth Feb 27 '17 at 12:29
  • @Vinoth then have a look at SQLite if you don't want to set up a whole database. – maraca Feb 27 '17 at 12:30
  • 1
    @Vinoth, If you want to keep the map between restarts, then write it in file or in database (database is actually a form of file as well, btw). The more we discuss this, the more it starts to look like you want database, but "don't want it" at the same time. Decide which one is it, please. – M. Prokhorov Feb 27 '17 at 12:31
  • Then serialize the whole thing and write it to a file or somesuch. – domsson Feb 27 '17 at 12:31
  • @M.Prokhorov i dont want to go with the database concepts and also file.. – Vinoth Feb 27 '17 at 12:33
  • @Vinoth, then this means you will inevitably lose information when application is restarted. – M. Prokhorov Feb 27 '17 at 12:34
  • I want a algorithm that generates the unique number for unique string. even if the fail over scenario, we will pass the same string and will get the same number. so no need to store it in map or list. – Vinoth Feb 27 '17 at 12:34
  • Example, public int getNumber(String S){ }. The above should return the same number when i am passing same string.. I will use the generated number to represent that string. If server restarts, then i will get the input string one by one, as the above method will generate the same output, then i dont want to worry about the order.. Understood?? – Vinoth Feb 27 '17 at 12:35
  • 1
    You either need to persist the information (db or file) or you need that perfect function that magically gives you the right number for each string. I think the latter is only possible if you know the possible strings in advance or if they behave in some very deterministic way. – maraca Feb 27 '17 at 12:39
  • @Vinoth, the only actual way your example will work and follow requirement from your question is when the implementation of such method is `return Integer.parseString(S, 10);`. No other implementation will satisfy everything you want. – M. Prokhorov Feb 27 '17 at 12:39
  • Example, public int getNumber(String S){ }. The above should return the same number when i am passing same string.. I will use the generated number to represent that string. If server restarts, then i will get the input string one by one, as the above method will generate the same output, then i dont want to worry about the order.. Understood?? – Vinoth Feb 27 '17 at 12:41
  • I'm pretty curious as to why the range of resulting numbers specifically needs to be [1000..6000] (or [0..5000] for that matter) – domsson Feb 27 '17 at 13:29
0

Your Second condition is impossible. 2) Algorithm should generate unique number for each different strings

Assume a hash function produces an unique integer number for each string. There are only 5000 possible integer numbers in range 1000 to 6000. So it can support maximum 5000 different strings.

Krishna
  • 88
  • 7
  • 1
    My input also will restricted to the specified range.. If range is 5000, then i will allow only 5000 string not more – Vinoth Feb 27 '17 at 11:02
  • Are you hashing the strings after you've acquired all of them? In that case, how about just sort them (alphabetically) and assign them the numbers 1000 to 6000? – domsson Feb 27 '17 at 11:05
  • I am not aware of input strings.. Consider the strings are generated by user randomly.. – Vinoth Feb 27 '17 at 11:07
  • So, if I understood correctly, you could just use an index/ID. First time a user inputs a string, you assign it the number 1000. Next time a user inputs a string, you look up the highest number you've already used (1000), so you assign it the next higher number (1001). And so on. A database will do that for you... – domsson Feb 27 '17 at 11:18
  • @domdom, database is an overkill, really. The simplest form will be just a list, where strings are appended to the end, and number is index of string in that list. – M. Prokhorov Feb 27 '17 at 11:21
  • Here the problem is order of the string.. I will not maintain the order of string anywhere.. If i down and up the server, then the order will be collapsed right. – Vinoth Feb 27 '17 at 11:29
  • And User may also delete the input string as well – Vinoth Feb 27 '17 at 11:30
  • Don't go with the database approach.. In our project, we are using virtual DB. – Vinoth Feb 27 '17 at 11:31
  • Just use a collection that does preserve the order of its elements. Or one that maps the numbers to the strings. Or write one that suits your needs. Or write the inputs into a text file. Or use a database. I'd probably just go with a `HashMap`. – domsson Feb 27 '17 at 11:31
  • @Vinoth *"And User may also delete the input string as well"* - you keep throwing bits and pieces of your requirements at us. Next time just be clear about what you're trying to do and what you need from the get-go. We're not here to worm things out of you... – domsson Feb 27 '17 at 11:41
  • @domdom sorry for not clearing told my requirment. Simple buddy, I need a algorithm, it should satisfy the conditions specified in the question. Storing into the DB or file are not allowed in our project. If i use any DS like map or list, it will be emptied when i am restart the server. there is no order maintained, we can't populate the same map like before restart.. – Vinoth Feb 27 '17 at 12:46
  • @Vinoth What you are asking for is provably impossible. Imagine deleting the 5000th item. Then you add a new random string. Then you delete the 1000th item. Then you add the first one you deleted back again. Guaranteed collision! – Patrick Parker Feb 27 '17 at 13:39