Suppose I have this array:
double[][] Q = new double[n1][n2];
I could index the values of Q by using int indexes, such as Q[2][1]. But in my case, n1 is a byte[], not an int. I still know the possible values of n1 (e.g, all possible combinations of the array values). What collection should I use instead of an array?
HashMap<byte[], Double>[] Q = new HashMap[n2];
This was my solution, but I'm not sure it is adequate. To index, I can do
byte[] n1 = {1,0,6,1,4,2,5,1};
Q[1].get(n1);
Is there a better way to do this? Something that is more performant? I think having an array of HashMaps is not ideal, but can I add that int to my key? How?
As stated in the comments,
The problem with using an array as a hashmap key (aside from the fact that it's mutable) is that arrays don't calculate their hashcode based on their contents; so you couldn't actually look up a value in the map unless you have the actual key instance.
So how do I use an array as an indexing key? A stupid solution would be to always convert it to String before using it, but I'm sure there are better and proper solutions.
I think the best way to go for your problem is to define a complex key class, which basically consists of your byte[] and int, write reliable hashCode and equals-Methods for this class, and use it as a key to the HashMap.
This way, you can use the runtime and memory efficiency of a hashmap and encapsulate the complexity of the key in a separate class.
Code to illustrate:
public static class MyKey {
private int i;
private byte[] b;
private MyKey(int i, byte[] b) {
this.i = i;
this.b = b;
}
public static MyKey of(int i, byte ... b) {
return new MyKey(i, b);
}
#Override
public int hashCode() { // Autogenerated from eclipse
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(b);
result = prime * result + i;
return result;
}
#Override
public boolean equals(Object obj) { // Autogenerated from eclipse
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
MyKey other = (MyKey) obj;
if(!Arrays.equals(b, other.b))
return false;
if(i != other.i)
return false;
return true;
}
}
public static void main(String[] args) {
HashMap<MyKey, Double> valueMap = new HashMap<>();
valueMap.put(MyKey.of(1, (byte)2, (byte)4, (byte)7), 0.1);
valueMap.put(MyKey.of(1, new byte[] { 3, 8, 14 }), 0.1);
}
Related
Post Details
In a data structures course, I was given Java source code for a "quadratic probing hash table" class and asked to implement a generic map (with get and put methods) and store the key/definition pairs in a hash table. I understand the material when reading the book but find it difficult to implement in a programming language (Java). I think part of the problem is understanding exactly what the question requires and part is deficiency in Java programming experience. I'm hoping to receive some suggestions for how I can approach problems like this and fill in whatever Java knowledge I'm missing.
Some questions I've had
What is the function of the hash table class in relation to the generic map I'm supposed to create? The hash table has several methods including get, insert, remove, rehash, etc... Is the purpose of the hash table to generate a hash value to use as a key in the map class? Are keys and definitions stored in the hash table or will they be stored in the map? What's the point of making a map if the hash table already does all of this?
Can someone help me understand how to approach problems like this? What are some references that might help me, either specifically with this question or with understanding how to effectively and methodically complete this type of exercise?
I appreciate whatever help I can get. I'm including code from the book to help illustrate the problem.
Quadratic Probing Hash Table Code From Textbook
public class QuadraticProbingHashTable<AnyType> {
public QuadraticProbingHashTable() {
this(DEFAULT_TABLE_SIZE);
}
public QuadraticProbingHashTable(int size) {
allocateArray(size);
doClear();
}
public boolean insert(AnyType x) {
int currentPos = findPos(x);
if(isActive(currentPos)) return false;
array[currentPos] = new HashEntry<>(x, true);
theSize++;
if(++occupied > array.length / 2) rehash();
return true;
}
private void rehash() {
HashEntry<AnyType>[] oldArray = array;
allocateArray(2 * oldArray.length);
occupied = 0;
theSize = 0;
for(HashEntry<AnyType> entry : oldArray)
if(entry != null && entry.isActive) insert(entry.element);
}
private int findPos(AnyType x) {
int offset = 1;
int currentPos = myhash(x);
while(array[currentPos] != null && !array[currentPos].element.equals(x)) {
currentPos += offset;
offset += 2;
if(currentPos >= array.length) currentPos -= array.length;
}
return currentPos;
}
public boolean remove(AnyType x) {
int currentPos = findPos(x);
if(isActive(currentPos)) {
array[currentPos].isActive = false;
theSize--;
return true;
} else return false;
}
public int size() {
return theSize;
}
public int capacity() {
return array.length;
}
public boolean contains(AnyType x) {
int currentPos = findPos(x);
return isActive(currentPos);
}
public AnyType get(AnyType x) {
int currentPos = findPos(x);
if(isActive(currentPos)) return array[currentPos].element;
else return null;
}
private boolean isActive(int currentPos) {
return array[currentPos] != null && array[currentPos].isActive;
}
public void makeEmpty() {
doClear( );
}
private void doClear() {
occupied = 0;
for(int i = 0; i < array.length; i++) array[i] = null;
}
private int myhash(AnyType x) {
int hashVal = x.hashCode();
hashVal %= array.length;
if(hashVal < 0) hashVal += array.length;
return hashVal;
}
private static class HashEntry<AnyType> {
public AnyType element;
public boolean isActive;
public HashEntry(AnyType e) {
this(e, true);
}
public HashEntry(AnyType e, boolean i) {
element = e;
isActive = i;
}
}
private static final int DEFAULT_TABLE_SIZE = 101;
private HashEntry<AnyType>[] array;
private int occupied;
private int theSize;
private void allocateArray(int arraySize) {
array = new HashEntry[nextPrime(arraySize)];
}
private static int nextPrime(int n) {
if(n % 2 == 0) n++;
for(; !isPrime(n); n += 2) ;
return n;
}
private static boolean isPrime( int n ) {
if(n == 2 || n == 3) return true;
if(n == 1 || n % 2 == 0) return false;
for(int i = 3; i * i <= n; i += 2)
if(n % i == 0) return false;
return true;
}
}
Map Skeleton From Textbook
class Map<KeyType,ValueType> {
public Map()
public void put(KeyType key, ValueType val)
public ValueType get(KeyType key)
public boolean isEmpty()
public void makeEmpty()
private QuadraticProbingHashTable<Entry<KeyType,ValueType>> items;
private static class Entry<KeyType,ValueType> {
KeyType key;
ValueType value;
}
}
Generally, what you're facing is a problem of implementing a given interface. The Map is the interface - the HashTable is a means of implementing it, the underlying data structure.
However, I understand your confusion as the definition of the HashTable that you were provided seems ill-suited for the job as it does not seem to have an option to use a custom key (instead always relying on the object's hash code for calculating the hash) nor does it have an option to have a custom HashEntry. As the question is specified, I would say the answer is "you can't". Generally, implementing a Map on a HashTable comes down to handling collisions - one approach, which is not very effective but usually works, is that whenever you find a collision (a case where you have differing keys but the same hashes), you rehash the entire table until the collision is no longer there. The more commonly adopted answer is having a multi-level hashtable, which basically recursively stores a hashtable (calculating a different hash function) on each level. Another method is having a hashtable of arrays - where the arrays themselves store lists of elements with the same hash - and rehashing if the number of collisions is too large. Unfortunately, neither of those solutions is directly implementable with the sample class that you were provided. Without further context, I cannot really say more, but it just seems like a badly designed exercise (this is coming from someone who does occasionally torture students with similar things).
An way of hacking this within your framework is creating a Pair type whose hashCode function just calculates key.hashCode(). This way, as a value you could store an array (and then use the array approach I mentioned above) or you could store a single element (and then use the rehash approach). In either solution, solving the collision handling is the most difficult element (you have to handle cases where the HashTable contains() your Pair, but the value part of the pair doesn't equals() the element that you want to insert.
I am trying to implement the linear probing method. Right now, I have come until this point:
public class LinearProbing<Key, Value> {
private int size = 300001;
private Value[] value = (Value[]) new Object[size];
private Key[] key = (Key[]) new Object[size];
public Value put(Key thiskey, Value thisval) {
int hash = thiskey.hashCode();
for (int i = hash; key[i] != null; i = (i + 1) % size) {
if(key[i] == hash)
break;
key[i] = thiskey;
value[i] = thisval;
}
}
}
I am bit confused to proceed after this. Here are my doubts:
When I check the equality of this.key == hash, I get an error saying I can't compare Key and int.So I decided to return the Object from the hashcode method and compare it with the this.key[i].equals(hashObject). However doing this is against as hashcode method returns the int in the Javadoc and I want to keep it that way. How do I solve this?
Please let me know if I wasn't clear.
How to add String[] into Set avoiding exception.
I put array's size to 100, it doesn't need to be fixed size array but
I need to have access to array's members like x[] so i can insert array values.
Set<String[]> s = new TreeSet<String[]>();
String[] x = new String[100];
int i=0;
x[i++] = ...
x[i++] = ...
s.add(x);
Ljava.lang.String; cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(TreeMap.java:1188)
at java.util.TreeMap.put(TreeMap.java:531)
at java.util.TreeSet.add(TreeSet.java:255)
Arrays don't have a "natural order", that's why you got this exception.
One way is to provide a custom comparator when constructing your TreeSet.
Set<String[]> s = new TreeSet<String[]>(new Comparator<String[]>() {
#Override
public int compare(String[] o1, String[] o2) {
//your logic here
}
});
But I don't think you should use a TreeSet for that sort of things because that will be hard to tell how to define an order for comparing your arrays.
IMO your best option is to create a wrapper class, overriding hashcode and equals and put those in an HashSet.
class WrapperStringArray {
private String[] arr;
//constructors, getters, setter and additional methods
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(arr);
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
WrapperStringArray other = (WrapperStringArray) obj;
return Arrays.equals(arr, other.arr);
}
}
I have developed a garbage collector friendly String cache for my Android game. Its purpose is to handle Strings for ints. I made a silly mistake implementing it but the bug never disclosed itself in desktop. In Android, however, the cache started returning funny Strings at once:
class IntStringCache {
private final Map<IntStringCache.IntCacheKey, String> cachedStrings = new HashMap<IntStringCache.IntCacheKey, String>();
private final IntCacheKey tempIntCacheKey = new IntCacheKey(0);
public String getStringFor(int i) {
tempIntCacheKey.setIntValue(i);
String stringValue = cachedStrings.get(tempIntCacheKey);
if (stringValue == null) {
stringValue = String.valueOf(i);
// ERROR - putting the same object instead of new IntCachKey(i)
cachedStrings.put(tempIntCacheKey, stringValue);
}
return stringValue;
}
public int getSize() {
return cachedStrings.size();
}
private class IntCacheKey {
private int intValue;
private IntCacheKey(int intValue) {
this.intValue = intValue;
}
private void setIntValue(int intValue) {
this.intValue = intValue;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + intValue;
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
IntCacheKey other = (IntCacheKey) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (intValue != other.intValue)
return false;
return true;
}
private IntStringCache getOuterType() {
return IntStringCache.this;
}
}
And the tests all of which pass:
public class IntStringCacheTest {
private IntStringCache intStringCache = new IntStringCache();
#Test
public void shouldCacheString() {
// given
int i = 1;
// when
String s1 = intStringCache.getStringFor(i);
String s2 = intStringCache.getStringFor(i);
// then
assertThat(s1).isNotNull();
assertThat(s1).isEqualTo(String.valueOf(i));
assertThat(s1).isSameAs(s2);
}
#Test
public void shouldCacheTwoValues() {
// given
int i1 = 1;
int i2 = 2;
int expectedCacheSize = 2;
// when
String s1 = intStringCache.getStringFor(i1);
String s2 = intStringCache.getStringFor(i2);
// then
assertThat(intStringCache.getSize()).isEqualTo(expectedCacheSize);
assertThat(s1).isSameAs(intStringCache.getStringFor(i1));
assertThat(s2).isSameAs(intStringCache.getStringFor(i2));
}
}
Note:
assertThat(String.valueOf(1)).isSameAs(String.valueOf(1));
fails.
The fact that the second test passes is interesting as, with the bug, there should be one key in the map that gets updated. This may be explained with hashCode() that could make the same key go into two different buckets inside HashMap. But how is it possible that the same key (even if in two buckets) returns the same two Stings? It seems that even though there is a bug in the code the HashMap does the job correctly.
My Android Java implementation, on the other hand, returns wrong number Strings with this bug at once.
You should consider replacing this entire class with SparseArray or its Support Library equivalent SparseArrayCompat (if you need it on <3.0 devices) as they are specifically designed to map integers to objects in a memory efficient way.
this is my first question here, but i hope i will provide all the needed information.
If NOT, please let me know!
My Problem:
I tried to improve my backtracking-algorithm by adding a HashMap to store the already processed results. For this i created a own class for the key of that HashMap. In there i overwrote the .equals()- and .hashCode()- methods.
But if i try to put this key and it's value into the map, it is taking much time, so that the algorithm becomes even less efficient then the backtrack-algorithm without the map.
To solve that problem, i changed the HashMap-Key to String and addded a .toString()-method to my key-class. This works very fine and it is quite fast. (Strange thing: .toString().hashCode() produces a lot of negative values, but seems to work)
Now my Question:
Is it always slowing down that much, if you create your own key?
I tried to find a answer to that question on my own and the only thin i found was to change .hashCode() or playing with the parameters of the HashMap-Constructor.
I tried both and i exported the produced HashCodes for my test-environment and i did not find any duplicates, though i know, it isn't a "good" method for hash-codes!
Here is a copy of my HashKey-Class (names of variables and methods changed):
public class HashKey {
private final int int0, int1, int2;
public HashKey(int int0, int int1, int int2) {
this.int0 = int0;
this.int1 = int1;
this.int2 = int2;
}
public int getInt0() {
return this.int0;
}
public int getInt1() {
return this.int1;
}
public int getInt2() {
return this.int2;
}
#Override
public int hashCode() {
final int prime1 = 107;
final int prime2 = 227;
final int prime3 = 499;
int result = 1;
result = prime1 * result + this.int2;
result = prime2 * result + this.int1;
result = prime3 * result + this.int0;
return result;
}
#Override
public String toString() {
return "Int0: " + this.int0 + " Int1: " + int1 + " Int2: " + int2;
}
#Override
public boolean equals(Object obj) {
if (obj instanceof HashKey) {
boolean eq0, eq1, eq2;
eq0 = this.int0 == ((HashKey) obj).getInt0();
eq1 = this.int1 == ((HashKey) obj).getInt1();
eq2 = this.int2 == ((HashKey) obj).getInt2();
if (eq0 && eq1 && eq2) {
return true;
}
}
return false;
}
}
And in my main-Class i use this:
HashMap<HashKey, List<Object>> storedResults = new HashMap<HashKey, List<Object>>();
int x1,x2,x3;
Object obj;
// later in a method:
storedResults.put(new HashKey(x1,x2,x3), obj);
If i change the Type of the Key to String and put that String into the Map, it works fine! So the HashKey.hashCode()-method and the rest of the algorithm works fine and is quite fast.
Does anybody know, what i can do to use this HashKey? For this algorithm it is not that important, but i want to know it for future algorithms!
If there are any questions or critics: they are VERY welcome!
Thanks in advance!
Klumbe
Try this:
Simplyfy your equals(..)-method and do not calculate the hashCode more than once.
public final class HashKey {
private final int int0, int1, int2;
private final int hashCode;
public HashKey(int int0, int int1, int int2) {
this.int0 = int0;
this.int1 = int1;
this.int2 = int2;
hashCode=107*int0+227*int1+499*int2;
}
#Override
public final int hashCode() {
return hashCode;
}
#Override
public final boolean equals( finalObject obj) {
if (!obj instanceof HashKey)
retun false;
HashKey other = (HashKey)obj;
return int0 == other.int0 && int1 == other.int1 && int2 == other.int2;
}
}
Referring to the comment of fge I changed the code.
Your class is nearly immutable: all your instance members are final, just the class itself would also need to be.
But as all your instance members are final, you could as well calculate the hash code at build time:
// Add as member:
private final int hashCode;
public HashKey(int int0, int int1, int int2) {
this.int0 = int0;
this.int1 = int1;
this.int2 = int2;
hashCode = // calculate hash code here
}
public int hashCode()
{
return hashCode;
}
By the way, negative hash codes are nothing to worry about. .hashCode() returns an int, after all.