How to get two values of string in a single HashMa

2019-09-07 16:02发布

问题:

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);

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..

回答1:

Indeed, this is a java question. There are many ways you can do this.

  1. HashMap<String, String[]> map = new HashMap<String, String[]>();

  2. HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();

  3. 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


回答2:

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...



回答3:

 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. In any case you get back a collection of all values you have ever stored under this key.