如何在单个HashMap中获得两个字符串值? [英] How to get two values of string in a single HashMap?
问题描述
我是Android的新手.
Im new to 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);
我想在单个键中的单个哈希图中获取这两个哈希图值(TAG_QUESID和TAG_ANSW):如果我有map3并且打印map3,我应该打印这两个值. 这该怎么做?因为我应该根据questionid获得答案,所以我想为下一个问题增加questionid.请帮助我.
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? because based upon the questionid i should get the answers,i want to increment the questionid for next question. plz help me..
推荐答案
实际上,这是一个Java问题.您可以通过多种方式执行此操作.
Indeed, this is a java question. 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>>();
创建一个类或枚举(首选),例如TAG,它具有两个字符串字段,然后
Map<String, TAG> map = new HashMap<String, TAG>();
Create a class or enum(preferred) for example TAG, which has two string fields, then
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
这篇关于如何在单个HashMap中获得两个字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!