[英]How to get two values of string in a single HashMap?
Im new to Android. 我是Android新手。
What i need is?.. Can i get two string values in a single hashmap, its possible. 我需要什么?我可以在单个哈希图中获取两个字符串值,这是可能的。 I have two hashmaps,these two hashmap values should i get in another hashmap.
我有两个哈希图,我应该在另一个哈希图中获得这两个哈希图值。 How to implement this?
如何实现呢? Any help.. Thanks in advance..
任何帮助..预先感谢..
String question = c.getString(TAG_QUES); System.out.println("Checking ::"+question); HashMap<String, String> map = new HashMap<String, String>(); map.put(TAG_QUES,question); System.out.println("questionMap:"+map); ques1.add(question);
String answer = c.getString(TAG_ANSW);
System.out.println("Checking ::"+answer);
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put(TAG_ANSW, answer);
System.out.println("AnswerMap:"+map1);
answ1.add(answer);
I want to get these two hashmap values(TAG_QUESID and TAG_ANSW) in a single hashmap in a single key eg): if i have map3 and i print map3 i should print these two values.. How to do this? 我想在单个键中的单个哈希图中获取这两个哈希图值(TAG_QUESID和TAG_ANSW):如果我有map3并且我打印了map3,我应该打印这两个值。 because based upon the questionid i should get the answers,i want to increment the questionid for next question.
因为我应该根据questionid获得答案,所以我想为下一个问题增加questionid。 plz help me..
请帮助我。
Indeed, this is a java question. 确实,这是一个Java问题。 There are many ways you can do this.
您可以通过多种方式执行此操作。
HashMap<String, String[]> map = new HashMap<String, String[]>();
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
Create a class or enum(preferred) for example TAG, which has two string fields, then Map<String, TAG> map = new HashMap<String, TAG>();
创建一个类或枚举(首选),例如TAG,它具有两个字符串字段,然后
Map<String, TAG> map = new HashMap<String, TAG>();
Since your updated question is a little bit unclear, I tried to answer this based on my own understanding. 由于您更新后的问题有点不清楚,因此我尝试根据自己的理解回答这个问题。 I think what you want to do is continously getting questions and their answers.
我认为您想要做的是不断获得问题及其答案。
So may be you can try another approach: 因此,您可以尝试另一种方法:
class Question{
String questionText;
String answer;
...
}
//map from questionID to a question
Map<String, Question> questionMap = new HashMap<String, Question>();
//TODO: fill up the map with existing questions.
Question question = questionMap.get(questionID);
System.out.println("Question:" + question.questionText + " Answer:" + question.answer);
//same for another question
Use value separated by delimiters and split them later. 使用由定界符分隔的值,然后再将其分割。
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put(TAG_ANSW, ""+answer+":"+"questionid");
Then when you retrieve it just split them: 然后,当您检索它时,只需将它们分开即可:
x= map1.get(TAG_ANSW)
String [] ans_questionId = x.split();
store them to their respective values... 将它们存储为各自的值...
HashMap<String, ArrayList<String>> multiMap =
new HashMap<String, ArrayList<String>>();
void put(String key, String value) {
ArrayList<String> values = multiMap.get(key);
if (values == null) {
values = new ArrayList<String>();
multiMap.put(values);
}
values.add(value);
}
ArrayList<String> get(String key) {
ArrayList<String> value = multiMap.get(key);
return value;
}
// Need to specify also which value to remove
void remove(String key, String value) {
ArrayList<String> values = multiMap.get(key);
if (values != null) {
values.remove(value);
if (values.size() == 0)
multiMap.remove(key);
}
}
If you do not want to keep repetitive values for the same key, use HashSet
instead of ArrayList
. 如果您不想为相同的键保留重复的值,请使用
HashSet
而不是ArrayList
。 In any case you get back a collection of all values you have ever stored under this key. 无论如何,您都将获得此键下曾经存储的所有值的集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.