I’m working in Java. I have the requirement that I must essentially compare two database queries. To do this, I take each row of the result set and assign it to a HashTable with the field name as the ‘key’ and the data in the field as the ‘value’. I then group the entire result set of HashTables into a single Vector just as a container. So essentially to compare two queries I’m really iterating through two Vectors of HashTables.
I’ve come to find that this approach works really well for me but requires a lot of memory. Because of other design requirements, I have to do this comparison via a Vector-HashTable-like structure, and not some DB side procedure.
Does anyone have any suggestions for optimization? The optimal solution would be one that is somewhat similar to what I am doing now as most of the code is already designed around it.
Thanks
Have you looked at the Flyweight Pattern? Do you have lots of equal objects?
Perhaps this pattern might be appropriate for your ‘Key’, as I imagine the field names are going to be repeated for each row? If they’re Strings, you can call
intern()so that they’ll share the same memory location with other equal Strings, as Strings are immutable.Another possible optimization – not memory but speed – if concurrency is not an issue would be to use an
ArrayListrather than aVector– as they are not synchronized so accesses should be a little faster. Similarly,HashMapisn’t synchronized andHashtableis, so using the former might be faster too.