问题
I am newbie to Android and I am working in an Android project. I want to implement a data model, using that model I want to perform the arithmetic operations using the Hash Map.
But I don't know how to implement it. And also I want to print the value in Text View. The project is like an Calculator.
ModelActivity
Create the getter and setter I don't know how to implement this
MainActivity
Using that Model I have to perform the operation like getting and setting the value using the Hash map.
This is my model class
public class Model {
public Map<String, String> getSmap(String string, String str) {
return Smap;
}
public Map<Integer, Integer> getImap() {
return Imap;
}
public Map<String, Integer> getSImap() {
return SImap;
}
public Map<Integer, String> getISmap(String jeeva, String s) {
return ISmap;
}
private Map<String, String> Smap = new HashMap<>();
private Map<Integer, Integer> Imap = new HashMap();
private Map<String, Integer> SImap = new HashMap<>();
private Map<Integer, String> ISmap = new HashMap<>();
public Map<Integer, String> getISmap(String jeeva) {
return ISmap;
}
}
Using that model I want to perform the operations like add sub.
回答1:
For my understanding you want to know how to populate HashMaps. So first of all you should read this documentation: https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
Code:
Map<String, String> map = new HashMap<>();
// Add things to the map
map.put("key","value");
// Get values from map
String value = map.get("key");
In very generic terms with this you can start doing stuff :) but you should absolutely read the documentation and have a deep understanding regarding how a Map works and then decide which type of Map you need to fulfil your requirements.
来源:https://stackoverflow.com/questions/43742769/how-to-use-the-hashmap-in-datamodel