Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashtable key within integer interval

Tags:

java

hashtable

I don't know if this is possible but i'm trying to make an Hashtable of where Interval is a class with 2 integer / long values, a start and an end and i wanted to make something like this:

Hashtable<Interval, WhateverObject> test = new Hashtable<Interval, WhateverObject>();
test.put(new Interval(100, 200), new WhateverObject());
test.get(new Interval(150, 150)) // returns the new WhateverObject i created above because 150 is betwwen 100 and 200
test.get(new Interval(250, 250)) // doesn't find the value because there is no key that contains 250 in it's interval

So basically what i want is that a key between a range of values in an Interval object give the correspondent WhateverObject. I know i have to override equals() and hashcode() in the interval object, the main problem i think is to somehow have all the values between 100 and 200 (in this specific example) to give the same hash.

Any ideias if this is possible?

Thanks

like image 969
xlar8or Avatar asked Jun 25 '12 11:06

xlar8or


People also ask

Can integer be used as key in HashMap?

It can store different types: Integer keys and String values or same types: Integer keys and Integer values. HashMap is similar to HashTable, but it is unsynchronized. It is allowed to store null keys as well, but there can only be one null key and there can be any number of null values.

Can Hashtable have multiple keys?

Object Types in HashTables The keys and values in a hash table can have any . NET object type, and a single hash table can have keys and values of multiple types.

Why hash tables are useful for storing many integers?

Hash function is what makes hash table a powerful and useful data structure. A hash function takes a piece of data, or usually referred to as a key, and returns a hash code as an output. This hash code, which is an integer, is then mapped to an index in the array, where the value will be stored.

Do Hashtable keys have to be unique?

In hashtable, you can store elements of the same type and of the different types. The elements of hashtable that is a key/value pair are stored in DictionaryEntry, so you can also cast the key/value pairs to a DictionaryEntry. In Hashtable, key must be unique. Duplicate keys are not allowed.


2 Answers

No need to reinvent the wheel, use a NavigableMap. Example Code:

final NavigableMap<Integer, String> map = new TreeMap<Integer, String>();
map.put(0, "Cry Baby");
map.put(6, "School Time");
map.put(16, "Got a car yet?");
map.put(21, "Tequila anyone?");
map.put(45, "Time to buy a corvette");

System.out.println(map.floorEntry(3).getValue());
System.out.println(map.floorEntry(10).getValue());
System.out.println(map.floorEntry(18).getValue());

Output:

Cry Baby
School Time
Got a car yet?

like image 137
Sean Patrick Floyd Avatar answered Nov 15 '22 12:11

Sean Patrick Floyd


A naive HashTable is the wrong solution here. Overriding the equals() method doesn't do you any good because the HashTable compares a key entry by the hash code first, NOT the equals() method. The equals() method is only checked AFTER the hash code is matched.

It's easy to make a hash function on your interval object, but it's much more difficult to make one that would yield the same hashcode for all possible intervals that would be within another interval. Overriding the get() method (such as here https://stackoverflow.com/a/11189075/1261844) for a HashTable completely negates the advantages of a HashTable, which is very fast lookup times. At the point where you are scanning through each member of a HashTable, then you know you are using the HashTable incorrectly.

I'd say that Using java map for range searches and https://stackoverflow.com/a/11189080/1261844 are better solutions, but a HashTable is simply not the way to go about this.

like image 28
aaronburro Avatar answered Nov 15 '22 12:11

aaronburro