-1

I have a list of objects that I needed to sort by category and also alphabetically. My idea was to take my ArrayList, convert it to a HashMap so that I could use a key to organize my data by category, and then createe a treemap to naturally alphabetize the data. It works, and I am not certain of a better/more efficient way of doing this. My TreeMap has the following structure:

TreeMap<String, ArrayList<CustomModel>>

How do I access the list of values from this treemap? I understand that I can get a specific value, if it were a String for example like the following:

for (Map.Entry<String, String> entry : treemap.entrySet()) {
            Log.i(TAG, "key= " + entry.getKey());
            Log.i(TAG, "value= " + entry.getValue());
        }

However, what if you have a list of values? How do I retrieve everything within that list? Seems like I need another conversion.

portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136

2 Answers2

1

It seems like you already have this solved. Entry.value() will return the ArrayList, instead of the String type. Try enumerating this and then logging each value

  • You are right, more or less. It does not return a list, it returns an object reference id. You have to use toArray() to get back the list. I included more details, but the work seems to be done and I accomplished what I wanted to accomplish which was sorting and alphabetizing – portfoliobuilder Apr 11 '18 at 21:39
0

I got the answer. There are a couple of ways of doing this. You can retrieve an object from your list, or the entire list object.

1) Object From List

You need a key to do this option. Get the key using keySet()

Object key = treemap.keySet().toArray()[index];
Object value = treemap.get(key);

2) Entire List Object

ArrayList<CustomModel> alCustomModel = (ArrayList<CustomModel>) treemap.values().toArray()[index];

Some notes about this, is that you are retrieving the list of CustomModels per position. Use whichever methodology makes sense to you. Cheers!

portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136