Using rectangle dimensions to create hash index?

Hi i would like to create a HashTable that adds rectangles to an underlying arrayList, using the x,y,width,height of the rectangle to choose the index of the rectangle in the list.

The reason for this is to speed up the retreival of the rectangle when searching with its dimensions.

How do you use four number( rect dimensions ) to create a hash index?

Thanks alot for any help!

Harley Rana.

[422 byte] By [harleyrana] at [2007-9-27 19:34:27]
# 1

The easiest solution is to transfer your 4 numbers into a string (using e.g. comma as a separator). The question, however, is whether it is what you need. Your specification:

> The reason for this is to speed up the retreival of the rectangle

> when searching with its dimensions.

does not provide much info.

The only keyword is dimension, which can mean:

- Euklideus dimension (which equals 2 for all plane objects)

- dimension of the rectangle = width * height

In this case you actually work with one number only

(1*10 should be "the same" as 5*2, if you are interested in the dimension only)

- anything else?

Pls. advise.

JirMac at 2007-7-6 22:38:35 > top of Java-index,Other Topics,Algorithms...
# 2

The java.awt.Rectangle class is already suitable for use as a key in some hash table [it provides hashCode, equals].

So, an easy solution could be:

Rectangle rect[] = ...;

HashMap rect2index = new HashMap();

for (int r=0; r<rect.length; r++) {

rect2index.put(rect[r], new Integer(r));

}

// if you want to retrieve the index of a specific rectangle

Rectangle rectangle = ...;

Integer bigIndex = (Integer)rect2index.get(rectangle);

if (bigIndex==null) {

// not found

} else {

// the index is bigIndex.intValue();

}

>

remistigri at 2007-7-6 22:38:35 > top of Java-index,Other Topics,Algorithms...