Android Hashmap interaction - android

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.

Related

How to retrieve List object stored in TreeMap?

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!

How to Append two different hash map on a particular node without replacing other in Firebase?

I want to append two different hash map pairs on a particular node one with key value both being String and other String and Array List. But problem is Later one is replacing first one in Fire base database. what should i do? I cant use push() all the time.Link of my code. Please help
You can achieve this if you generate a unique random key. This can be done using push() method.
As we know, in the case of HashMap, it replaces the old value with the new one.
Don't forget to use updatechildren() method, and not setValue() method directly on the refrence when you want to update data.
Hope it helps.
Just use 1 Map<String, Object> instead of 2 different ones.
then you put all the data in that single map, string, arraylist

Get JSON Object Keys in a loop with default order android

I want to parse JSONObject and get its keys in the order they do represent to me when i receive. I don't want use JSONObject.keys() as this function giving undefined order of json keys and fully unordered,and this is a huge problem right now because I'm stuck in this position and i have to get json object keys.So is there any way to achieve it? Doing researches i never came across to code which can parse json object keys except JSONObject.keys() which giving reverse order.
There is no way to achieve what you want with a JSONObject because JSONObjects are not ordered by definition and you shouldn't rely on the insertion order. If you need something ordered you should look into JSONArray
I have noticed that although JSONArray gives the proper order, the order won't be kept in HashMap, if you're using it for saving the values.
Using LinkedHashMap solves the order issue.

add arraylist of lat/lng along with string ,string and int(id) of the mapping

I have added all lat/lng to arraylist and .Iwould like to map it to string name and another string along with its id which is an int .Basically I would like to get :
Association : (arraylist)-->Name---->Another Name------>id
how do I do the above association.I am a noob in android and I am using hashmap but it only puts(key,value) which does not satisfy the above condition.
Please let me know how I can implement the above requirement.
I appreciate any help.
Thanks in Advance.
There are a number of ways you could accomplish this. Here are two different ways off the top of my head:
Create an object that will hold the two names and id. Then create a hashmap that maps each lat/long value to the appropriate object. The prototype would be like:
HashMap<Long/Lat, Object>
Or if you don't want to create a new object, then create a hashmap that maps each lat/long value to a hashmap that contains the two names and id. The prototype would be like:
HashMap<Long/Lat, HashMap<String, String>>

Android SparseArray<SparseArray<Object>> initialization

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.

Categories

Resources