How easy it is to manipulate a hashmap?

S

Sharp

Hi

I have a HashMap.
Each key is mapped to a value (an ArrayList of Character objects).
How easy is it to compare a column of characters in the HashMap?
Maybe there is a better datastructure to use for my purpose?

For example, if the column contains characters that are the same then keep
score.

123 abcfdgdslfkjkdfj
456 acsajdhkjsahdkj

score: 1 (first column contains 'a')

-----------------
Code.java:
-----------------
Map table = new HashMap();
//lets assume table is populated with data

Collection c = table.values();
for (int i=0; i<valueArray.length; i++) //valueArray = ArrayList of
Character objects
{
Iterator itr = c.iterator();
While (itr.hasNext()) // problem can't refer row by index
{
ArrayList characterList = (ArrayList)itr.next();
characterList.get(i)
...
}
 
J

John C. Bollinger

Sharp said:
I have a HashMap.
Each key is mapped to a value (an ArrayList of Character objects).
How easy is it to compare a column of characters in the HashMap?
Maybe there is a better datastructure to use for my purpose?

What are the keys? Do the values need to be mutable? I'd probably
recommend String, StringBuffer, or char[] values, rather than ArrayList
of Characters. Depending on the relationship between keys and values,
and on what else you want to do with the values, you might not need a
map at all. If you do need a Map, then HashMap is the one I recommend
for most purposes.
 
A

anony

Sharp said:
I have a HashMap.
Each key is mapped to a value (an ArrayList of Character objects).
How easy is it to compare a column of characters in the HashMap?
Maybe there is a better datastructure to use for my purpose?

What are the keys? Do the values need to be mutable? I'd probably
recommend String, StringBuffer, or char[] values, rather than ArrayList
of Characters. Depending on the relationship between keys and values,
and on what else you want to do with the values, you might not need a
map at all. If you do need a Map, then HashMap is the one I recommend
for most purposes.

The keys are strings (mixture of letters and numbers), and are unique.
The values are a string of charcters, and are mutable.
I want to be able to compare columns of characters for similarity.

It seems difficult to retrive a column of characters for comparsion.
The problem lies that you cannot retrieve the rows by index, but
you use the iterator method instead.

Cheers
Sharp
 
J

John C. Bollinger

anony said:
@rainier.uits.indiana.edu:

Sharp wrote:

I have a HashMap.
Each key is mapped to a value (an ArrayList of Character objects).
How easy is it to compare a column of characters in the HashMap?
Maybe there is a better datastructure to use for my purpose?

What are the keys? Do the values need to be mutable? I'd probably
recommend String, StringBuffer, or char[] values, rather than ArrayList
of Characters. Depending on the relationship between keys and values,
and on what else you want to do with the values, you might not need a
map at all. If you do need a Map, then HashMap is the one I recommend
for most purposes.


The keys are strings (mixture of letters and numbers), and are unique.
The values are a string of charcters, and are mutable.

Then use StringBuffers for values if their lengths can change, or char[]
if they have fixed lengths.
I want to be able to compare columns of characters for similarity.

It seems difficult to retrive a column of characters for comparsion.
The problem lies that you cannot retrieve the rows by index, but
you use the iterator method instead.

If you want to be able to retrieve the values by index then you don't
want a Map, you want a List or an array. If you want access both by key
and by index then you need both a Map and a List / array. These can
share the value objects, but you do need to take care to keep them
synchronized. If you mostly want access by index, then it might be
sufficient to have a List of the keys and a List of the values, in
corresponding sequence. Retrieval by key would consist of using
List.indexOf() on the key List, followed by List.get(int) on the value List.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top