简体   繁体   中英

Choosing a suitable data structure

Whenever I start my application I need to iterate over all entries in a matrix, and for each entry construct a variable amount of objects.

Later on when given a matrix, I need to iterate through the new matrix, and for all non-zero entries i need to retrieve all the objects calculated earlier for that particular entry and iterate through them.

The focus is on getting retrieved and iterated through the objects. The construction time is less important.

I was thinking of some kind of map structure, where i map the entry to a linked list. Is this suitable? Or can you suggest something other?

As a side note, I am implementing this in Java, so if you know a concrete implementation to solve this problem I would be happy to know!

Regards Jesper

I was thinking of some kind of map structure, where i map the entry to a linked list. Is 
this suitable? Or can you suggest something other?

This sounds like a great application to use the Multimap API that is part of google commons. You can also get it as part of the guava libraries.

If you don't want to add a dependency on that, however, maintaining a Map<Object1, List<Object2>> would probably be the cleanest way to do it.

For the matrices, you could just use arrays, and probably make a class Matrix to abstract away what the matrix is actually made of. For the storage of the objects, use a matrix of List<WhateverKindOfObjectYouAreConstructing> s. You can then quickly find the list for a given entry, and then quickly iterate through it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM