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.
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
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!
Related
I have a Place object, containing an array with objectIds for Tag objects.
An example of an array:
[[{"__type":"Pointer","className":"FavoriteTag","objectId":"Toc5sVlzVd"},{"__type":"Pointer","className":"FavoriteTag","objectId":"cUxcl0IFFv"}]]
My first workflow way too slow:
query each Place object, get the array and parse it to find each objectId
get the corresponding Place with the objectId
The workflow I'm trying:
Find the objectId of each matching Tag object
Put these objectIds in a ArrayList
Get each Place object that contains a matching objectId from the arraylist
I don't know how to do step 3.
I'm trying:
parseQueryFavoritePlaces.whereEqualTo("tags", arrayListTagsObjectIds);
and
parseQueryFavoritePlaces.whereContainedIn("tags", arrayListTagsObjectIds);
but no luck so far.
Am I on the right track here?
Try giving this a shot:
query.whereContainedIn(String key, Collection<? extends Object> values)
According to the Parse documentation this adds a constraint to the query that requires a particular key's value to be contained in the provided list of values.
Here's the ParseQuery documentation if you think that might be useful!
I want to store city names by country in an array.
This is my code
String cities[][]=new String[10][20];
I want to assign all cities of a country one time like this.
cities["USA"]={"NEW YORK","WASHINGTON"}
cities["UK"]={"LONDON","CAMBRIDGE","CARLISLE"}
then I want to use like this
String mycity=cities["UK"][2];
but eclipse shows error for assigning values. how can I use this arrays?
Try like this,
String cities[][]={
{"NEW YORK","WASHINGTON"},
{"LONDON","CAMBRIDGE","CARLISLE"}
};
And
cities[0][0]
will return NEW YORK
May be this helps you.
Better you can use a HashMap - List combination like this
HashMap<String,HashMap<String,List<String>>> cities = new HashMap<>();
Refer following links for more details Storing HashMap inside HashMap,
Storing a HashMap inside another HashMap and improving performance
I have to implement the classification of somehting like Hashmap with two keys and a value, let's say Hashmap<K1, K2, V>, where the two keys are integers and the value is a generic MyObject defined by me.
I read this, this, and this post, and I also know that guava project offers the table interface, but I don't want to use external libraries (if not strictly necessary) to keep my project smaller as possible.
So I decided to use SparseArrays: I thought that this was the better choice because my keys are int and are not necessarily starting from zero and increasing.
I do this initializing:
SparseArray<SparseArray<MyObject>> myObjectSparseArray = new SparseArray<SparseArray<MyObject>>();
Now let's go to the point. Can I do this kind of operation:
MyObject myObject = new MyObject();
myObjectSparseArray.get(3).put(2,myObject);
or should I do something like:
MyObject myObject = new MyObject();
myObjectSparseArray.put(3, new SparseArray<MyObject>());
myObjectSparseArray.get(3).put(2,myObject)
In other words: Do I initialize both SparseArrays with this single line?
SparseArray<SparseArray<MyObject>> myObjectSparseArray = new SparseArray<SparseArray<MyObject>>()
Do you think there are better implementations for my case?
If you have two keys and one value, I would just use a normal HashMap or SparseArray and will combine those two keys into one value.
Let's say you have one key which is String and one which is Long so your map key will be:
(strKey + longKey).hashCode(). You can use it as Integer and save it into SparseArray or as String and use HashMap.
Having tow nested SparseArray is against the purpose of having SparseArray in the first place since you will allocate a new SparseArray for each unique first key in your key pairs.
A good solution for that would be by using hashmap and use a key object that takes two int values (Point for example) or an object that you define that simply holds two keys.
I created a list based on this example:
http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/
In GetView method, need to access information that is stored only in the HashMap.
I can access using "adapter.getItem (position). ToString ()" but this way, keys and values come together in a single string, like this:
{date=2011-07-25 19:30:00, id=1, caption=Test Caption, title=Test Title, bookmark=true}
You can retrieve this data separately? For example, only the value of "bookmark" field
Please include an example. I am new to developing for Android.
remember that you know the data type that is used with the adapter.
instead of using toString, just get the item and use it as you usually do.
for example:
HashMap<String, String> i = (HashMap<String, String>) adapter.getItem(position);
i.get(ITEM_TITLE);
I am attempting to set up a simple Morse encoder using a hashmap in android. Putting values in the hashmap seems pretty straightforward like so:
HashMap<String, String> translate = new HashMap<String, String>();
//initializing translate
translate.put("A",".-");
//same for all letters of alphabet and numbers
However I am having difficulty finding an effective way to utilize the data of the key values for export to another method. I plan to use these values in a string method and simply display it on the phone screen for the user when they type a letter. For example, if they type in "A" the hash map will be queried for "A" and return a ".-". I have never worked with hashmaps before and can't find a suitable example.
Any help on how to access these keys within an android environment will be appreciated!
Use HashMap.get(), so:
translate.get("A"); // returns ".-"
The object returned is exactly the same as the object supplied in the 2nd argument to put(). So if you put a URL in (and the Map is suitably typed) you will get the same URL instance returned from get().
HashMap has keySet and entrySet(). You may start from here. Here is javadoc for complete list of methods in HashMap. Here is an example on how to use those methods.