Hello,
I recently tried to use HashMap as follows: there is a register consisting of 2 bytes that is the address and the contents of this register of 2 bytes.
For example, at the address 0xF1 0x15 (low, high byte) the register contains data 0x12 0x45 (low, high byte).
Because the address consists of two bytes, then just maybe 256 * 256 = 65536 registers. Using HashMap create a database of the following form:
HashMap<String,Data2BytesCell> mDataBase = new HashMap<>(); mDataBase.put( Byte.toString( (byte) 0xF1 ) + Byte.toString( (byte) 0x15 ), new data2BytesCell((byte) 0x12, (byte) 0x45 ); I create the key as a string of two bytes, here in particular "F115". Using the same key, I similarly extract the register value.
As a result, I created 65536 elements, but when I started checking these elements for collisions, there were a lot of them. Those. the register must contain one value at the given address, and it displays a completely different one.
What is the point of using such an unreliable repository as a HashMap when such a high probability of collisions? Maybe I do not use it either? Or use for other purposes?
Thank you in advance for all the answers.
For specifics, I will give a more detailed code with methods:
// в классе создается объект базы данных HashMap<String,Data2BytesCell> mDataBase = new HashMap<>(); // Описание метода записи данных по адресу. // Данные заносятся в базу по данному адресу // (является ключом). public void setData(byte lBAddress, byte hBAddress, byte lBData, byte hBData){ mDataBase.put(Byte.toString(lBAddress) + Byte.toString(hBAddress), new Data2BytesCell( lBData, hBData ) ); } // Данные считываются с определенного адреса (ключа). // Возвращает массив из 2 байт (данные) public byte[] getData(byte lBAddress, byte hBAddress){ Data2BytesCell data2BytesCell = mDataBase.get( Byte.toString(lBAddress) + Byte.toString(hBAddress) ); return data2BytesCell.getData(); } I will also give a method for checking on collisions:
public void validateCollisions(){ // Здесь в данные data2BytesCell в 2 байта записываются значение // адреса. Т.е. например в регистр с адресом 0х01 0х02 (ключ "0102") // записывается значение 0х01 и 0х02. // Теперь когда я захочу считать значение по адресу 0х01 0х02 // (ключ "0102") я должен на выходе получить массив из двух элементов // 0х01 0х02. Аналогично заполняется регистры для остальных 256х256 // адресов. for (int i = 0; i < 256; i++){ for (int j = 0; j < 256; j++){ Data2BytesCell data2BytesCell = new Data2BytesCell( (byte) i, (byte) j ); mDataBase.put( Byte.toString( (byte) i ) + Byte.toString( (byte) j ), data2BytesCell); } } // Здесь идет проверка берем регистр с адресом 0х01 0х02 // (ключ "0102") извлекаем с помощью get извлекаем массив из двух // байт если первый элемент равен 0х01, а второй 0х02, то все норм, // если нет, то выводим ошибку коллизия. for (int i = 0; i < 256; i++){ for (int j = 0; j < 256; j++){ Data2BytesCell data2BytesCell = mDataBase.get(Byte.toString( (byte) i ) + Byte.toString( (byte) j)); byte lbHb[] = data2BytesCell.getData(); if (lbHb[0] != (byte)i || lbHb[1] != (byte)j ){ Log.e(TAG, "Collision in: i = " + Integer.toString(i) + " j = " + Integer.toString(j) + "/n" + "lbHb[0] = " + Byte.toString(lbHb[0]) + "lbHb[1] = " + Byte.toString(lbHb[1]) ); } } }
Stringkey, and Integer has all the rules. I will have to train my attention. Thank you again very much for your help. - foxis