Using a Hashmap to keep track of original count or not

John Anderson :

I am scanning the MediaStore on app start. I iterate over it for audio tracks and store them. I also store other aspects like albums. I store the tracks in an ArrayList and that also goes for albums (different ArrayList) This works fine but I have started to consider the performance.

I want to keep track of tracks count, for each album, while scanning. So, I originally used a HashMap of album id -> tracks count. With each track I get during scanning, I check if the HashMap (associated with the album id) exists or not. I create it when it does not exist and update it with the count otherwise. Then, after the scanning, I would iterate over the albums ArrayList and update the entries with the information from HashMap by using the HashMap lookup.

I am starting to wonder if it is worth it to use HashMap here for performance. Will skipping the HashMap and looking up the album I want to update, using a for loop, then updating it, be better in performance?

Thanks.

Edit: since I cannot post the code because it is a large chunk, so I will simplify it. Original code:

private void scanTracks()
{
    HashMap<Long, Integer> albumHashMap= new HashMap<>();

    // Iterate over tracks in MediaStore Block Start

    // Get albumId.

    if (!albumHashMap.containsKey(albumId))
        albumHashMap.put(albumId, 1);
    else
        albumHashMap.put(albumId, albumHashMap.get(albumId) + 1);

    // Iterate over tracks in MediaStore Block End

    for(Album album : mAlbums)
    {
        album.setCount(albumHashMap.get(album.getId()));
    }
}

Here is the alternative implementation I am suggesting.

private void scanTracks()
{
    // Iterate over tracks in MediaStore Block Start

    // Get albumId.

    for (Album album : mAlbums)
    {
        if (album.getId() == albumId)
        {
            album.secCount(album.getCount()+1);
            break;
        }
    }

    // Iterate over tracks in MediaStore Block End
}
Hung Thai :

I am scanning the MediaStore on app start. I iterate over it for audio tracks and store them. I also store other aspects like albums. I store the tracks in an ArrayList and that also goes for albums (different ArrayList) This works fine but I have started to consider the performance.

With this description, I understand that you have a list of all the tracks, which is not grouped by album. Then, you have another list of all the albums.

If that's the case, with the use of HashMap, the time complexity will be O(n + m), where n and m is the count of tracks and albums, respectively. Haven't evaluated the performance of the other way, I can say that this is a sign of good performance.

Now, if you go through each album in the album list, and then iterate the tracks to count, the time complexity would be O(m*n), which is far worse than the above O(m + n).

So in recap, use HashMap.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Keep track of original variable name after rename using notes stata

Git keep the original file and not track any changes to it

Using Handler to keep track of files

Flow programming: subscriber and publisher to keep track of count?

How to keep track of the count of instances of a type?

How to *keep track* of object count in CoreData

How to keep track of children count in Firebase

Selenium how to count and keep track of the number of products

Find, in linear time, duplicate in array of length N, numbers 1 to N-1 without using additional obj/arr to keep track of a count

Using the Oracle SCN keep track of modified rows

How to keep track of established connections using WebSockets

using retain to keep track of maximum value

Using metaclass to keep track of instances in python

Using Array Key => Value to track count of items

Keep track of variables within class when using method multiple times

Keep track of answers in Parallelized loops using pool() - Python

Read a file using Stream and keep track of the current index

How to keep track of dynamically added nodes using MutationObserver?

How to keep track of webpages opened in web-browser using Python?

what is the logic of using an array to keep track of multiple setTimeouts

Python, using dictionaries and sets to keep track of all occurences of a word

How do I keep track of nodes in a tree using hash table?

Using xsl:accumulator to keep track of text nodes between two PIs

Using closure to keep track of the state of previous function call

How to keep track of mouse events and position using selenium and javascript in python?

MySQL : How to keep track of change in value using its trend?

How do you keep track of changes in registry using powershell?

How to keep track of the user searched items by using cookies

JProgressBar to keep track of a Native(C++) application using JNI