How to clear a hashmap in Android [duplicate] - android

This question already has answers here:
Map.clear() vs new Map : Which one will be better? [duplicate]
(7 answers)
Closed 9 years ago.
Below is the reference code
public static Map<String, Long > map1 = new HashMap<String, Long >();
if i call map1.clear() , will all elements of hashmap become eligible for GC or do i need to set every element of the map explicitly to null ?

hashmap.clear() just clears all the data of the HashMap to make it reusable again.Here reusable means the same object can be reused again.So I dont think there is any role of GC here.

Related

How do I add an item to an existing array in a map in Flutter? [duplicate]

This question already has answers here:
Flutter how to add map to existing array on firebase
(1 answer)
How can I update map data that's in a array in firebase? (Flutter)
(2 answers)
Flutter Firestore: update array of object by param
(1 answer)
Closed 4 months ago.
I am trying to develop a stock management application on Flutter. I have integrated Firebase into the app and I can view the data on the app. My collection and documents are as follows:
I want to add a new element to an array in an already existing document wtih this.
Map<String, dynamic> urunData = {
'ad': _adController.text,
'kod': _barcodeController.text,
'seri': _seriController.text,
'raflar': [
{'raf': dropdownValue, 'adet': _rafController.text},
],
'sirket': _sirketController.text,
'tarih': FieldValue.serverTimestamp()
};
sayimRef.doc(_barcodeController.text).update(urunData);
But in this way, the other elements in the 'raflar' array inside document are deleted and it is transformed into a new array with one element. How do I update an existing index or add a new one without deleting other elements?

How to remove all items in table object [duplicate]

This question already has answers here:
Empty an array in Java / processing
(8 answers)
clear method in an array
(6 answers)
The best way to empty array in java
(5 answers)
How to remove all elements in String array in java? [duplicate]
(6 answers)
Closed 5 years ago.
I have a table object 'list_animal':
Animal[] list_animal = new Animal[3];
list_animal[0] = new Animal(...);
list_animal[1] = new Animal(...);
list_animal[2] = new Animal(...);
I want reomve all data in 'list_animal'.
Your question is a bit vague. If I understand correctly, you can do this:
list_animal = new Animal[3];
Or you can assign NULL to each element.
From here How to remove all elements in String array in java?
You can do this
Arrays.fill( list_animal , null );

What is a concrete example of Android app functionality that uses HashMap? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am a beginner in Android, and understand only very basically that HashMap class enables key/value pairs. But how does this translate into actually using this in an Android app? Could someone provide a simple, plain English example of what case you might want to use HashMap in an app? I cannot imagine a case where I might need it. Make up an Android app idea, if needed. Thanks in advance.
I am looking for a "big picture" analysis that will give some examples where you might use HashMap with certain Android functionalities you are trying to implement.
HashMap or Map interface is not new on android, This is Java Collections framework.
Java collection are meant to be used in several cases to hold data and contain 3 interfaces:
List - Basically simple list,or linked list implementations
Set - The same as list but won't hold 2 equal obejcts(You need to implement you own equals and hashcode)
Map - as you said key value pair.
Uses:
List - For anything, just to hold data
Set - For list of data that we want that all of the items will be unique.
Map - Key value and the most common example is the use for DB items, or something with ids.. for example:
bookId, Book.. I that case you can take the object by id.. This is the most common
I attached link for Java collection tutorial.. It is very important framework that you have to know if you are going to develop java/android
http://tutorials.jenkov.com/java-collections/index.html
Hope that helps
We could use HashMap to keep a list of employess together with their respective salaries.
We can do:
HashMap<String, Float> emplMap = new HashMap<String, Float>();
emplMap.put("fred", 1.000);
for(String name : emplMap.keySet()) {
System.out.print(name + "'s salary is" + emplMap.get(name));
}
Should print
"fred's salary 1.000"

Storing and retrieving values from hashmap [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
i'm a beginner to Android development .... can u please tell me if this is this the correct way to declare an hashmap and add it to the arraylist?
Button createagendaButton = (Button) dialog.findViewById(R.id.button2);
createagendaButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("agendaTitle", edit_agendaTitle.getText().toString());
map.put("presenterName", edit_presenterName.getText().toString());
mylist.add(map);
}
});
It seems you are new to android
For starters why dont you go through samples in android sdk
sdk-path/samples/api-version/
and this link
To cheer you up try these that will solve your problems
1. get value from edittext edittext.getText();
2. store values in hashmap as hashmap.put(key,value)
3. instead of going for hashmaps, reconsider the type of data you are storing and try SparseArray if it suits your needs. Sparse arrays are well optimized and good at performance though they are very different in comparison
4. sqlite can store only some types of data. For sqlite on android try this
5. For ensuring proper functioning of your app across devices and resolutions, refer best practices and life cycles of different components used in your app. Also since you are using sqlite, prefer singleton pattern
The following code will allow you to store the values into the hashmap by using the key => value convention.
Map mMap = new HashMap();
mMap.put("FirstName", firstNameInput.getText().toString());
mMap.put("Surname", surnameInput.getText().toString());
mMap.put("Gender", genderInput.getText().toString());
Note, you will need to change the variable name before .getText() to that of your EditText variable names
http://developer.android.com/reference/java/util/HashMap.html might be of some use!

How to dynamically define name of R.drawable [duplicate]

This question already has answers here:
How to get the image from drawable folder in android?
(4 answers)
Closed 9 years ago.
Is there a way to generate R.drawable.... name on the fly? I want to set a background image with a dynamically generated name ( which is already usable as R.drawable.example_graphic)
So i want to find a way to assign a String to $ABC -> btn.setBackgroundImage(R.drawable.$ABC);
I don't want to create a new drawable, i want to use the existing one.
Yes. Drawables are constants at compile time: you know exactly what exists. So, in your Application object, create a map:
public static final Map<String, Integer> NAMED_RESOURCES;
static {
Map<String, Integer> m = new HashMap<String, Integer>();
m.put(KEY1, R.drawable.ABC);
m.put(KEY2, R.drawable.DEF);
// ...
NAMED_RESOURCES = Collections.unmodifiableMap(m);
}
now you can:
btn.setBackgroundImage(Application.NAMED_RESOURCES.get($ABC));
i have found the solution, as #CommonsWare has shared.
getContext().getResources().getIdentifier("flags_"+country, "drawable","mypackage")
However, it may require cacheing as it uses reflection.
So, creating a static HashMap to keep the ResourceName and it's result from getIdentifier function (integer) seems to be a good idea; further usages for the same ResourceName will just get the value from HashMap instead of using the reflection again.

Categories

Resources