2017-04-19 61 views
1

我有如下表獲取不同的對象從列表中的Java

question_text_id question_id language_id question_text 
2     7   1   english_text_1 
3     7   2   spanish_text_1 
4     8   2   spanish_text_2 
5     8   1   english_text_2 

現在我要爲每個不同的question_id

創建列表我用下面的代碼

List<QuestionText> questionTextList = questionManager.getQuestionsTextByQuestionId(Long.parseLong(questions.getQuestionId().toString())); 
for (QuestionText questionText : questionTextList) { 
    questionMap.put("questionId", questionText.getQuestionId()); 
    questionMap.put("language", questionText.getLanguageId()); 
    if(questionText.getLanguageId().longValue() == 1){ 
     questionMap.put("englishQuestionText",questionText.getQuestionText()); 
    } else { 
     questionMap.put("spanishQuestionText",questionText.getQuestionText()); 
    } 
    questionListMap.add(questionMap); 
}    
adminCollectionBookendModel.put("questionListMap",questionListMap);  

[{questionId = 1,language=1, englishQuestionText = english_text_1,spanishQuestionText=spanish_text_1},{questionId = 1,language=2, englishQuestionText = english_text_1,spanishQuestionText=spanish_text_1}] 

但是,這給予我重複相同的數據對象,如果我有西班牙文和英文問題文本如上所示。如何獲得唯一的列表?

如何同時獲取每個question_id的西班牙文本和英文文本以及language_id並訪問它?

請幫我在這

+1

你'questionListMap'列表類型只包含相同的對象多次。即使您在每次迭代中創建新地圖,也不會得到預期結果。你需要做的是在每次迭代中查看列表,看看當前問題是否已經有地圖,並設置其英文或西班牙文本。 – Titus

+1

要添加到以前的評論中,您將無法爲地圖中的'language'屬性添加不同的值,因爲它將被覆蓋。如果您希望languageIds適用於兩種語言及其文本,則可能必須考慮以不同的方式來存儲此屬性。 – user2004685

+0

@ user2004685其他的工作也很好,請告訴我如何達到我的要求 – allDroid

回答

0

的第一步是創建一個POJO類就是這樣,

public class QuestionDetails { 

    private int questionId; 
    private int englishLanguageId; 
    private int spanishLanguageId; 
    private String englishLanguageText; 
    private String spanishLanguageText; 

    public int getQuestionId() { 
     return questionId; 
    } 

    public void setQuestionId(int questionId) { 
     this.questionId = questionId; 
    } 

    public int getEnglishLanguageId() { 
     return englishLanguageId; 
    } 

    public void setEnglishLanguageId(int englishLanguageId) { 
     this.englishLanguageId = englishLanguageId; 
    } 

    public int getSpanishLanguageId() { 
     return spanishLanguageId; 
    } 

    public void setSpanishLanguageId(int spanishLanguageId) { 
     this.spanishLanguageId = spanishLanguageId; 
    } 

    public String getEnglishLanguageText() { 
     return englishLanguageText; 
    } 

    public void setEnglishLanguageText(String englishLanguageText) { 
     this.englishLanguageText = englishLanguageText; 
    } 

    public String getSpanishLanguageText() { 
     return spanishLanguageText; 
    } 

    public void setSpanishLanguageText(String spanishLanguageText) { 
     this.spanishLanguageText = spanishLanguageText; 
    } 

    @Override 
    public String toString() { 
     return new StringBuilder().append("questionId: ").append(questionId) 
       .append(" ,englishLanguageId: ").append(englishLanguageId) 
       .append(" ,englishLanguageText: ").append(englishLanguageText) 
       .append(" ,spanishLanguageId: ").append(spanishLanguageId) 
       .append(" ,spanishLanguageText: ").append(spanishLanguageText) 
       .toString(); 
    } 
} 

下一步將會改變你的代碼片段是這樣的:

List<QuestionDetails> questionsList = new ArrayList<>(); 
List<QuestionText> questionTextList = questionManager 
     .getQuestionsTextByQuestionId(Long.parseLong(questions 
       .getQuestionId().toString())); 
for (QuestionText questionText : questionTextList) { 
    /* Get QuestionDetails Object */ 
    QuestionDetails qd = getQuestionDetails(
      questionText.getQuestionId(), questionsList); 
    /* Check Null */ 
    if(null == qd) { 
     /* Get New Object */ 
     qd = new QuestionDetails(); 
     /* Add Object To List */ 
     questionsList.add(qd); 
    } 
    /* Set Question ID */ 
    qd.setQuestionId(questionText.getQuestionId()); 
    /* Set Language ID & Text */ 
    if (questionText.getLanguageId().longValue() == 1) { 
     qd.setEnglishLanguageId(questionText.getLanguageId() 
       .longValue()); 
     qd.setEnglishLanguageText(questionText.getQuestionText()); 
    } else { 
     qd.setSpanishLanguageId(questionText.getLanguageId() 
       .longValue()); 
     qd.setSpanishLanguageText(questionText.getQuestionText()); 
    } 
} 
adminCollectionBookendModel.put("questionListMap", questionsList); 

最後,這裏是執行getQuestionDetails函數:

private QuestionDetails getQuestionDetails(int questionId, 
     List<QuestionDetails> questionsList) { 
    /* Search Existing Object */ 
    for (QuestionDetails qd : questionsList) { 
     /* Match Found */ 
     if (qd.getQuestionId() == questionId) { 
      return qd; 
     } 
    } 

    /* No Object Found */ 
    return null; 
} 
+0

這是給我同樣的對象重複5次 – allDroid

+0

@allDroid更新瞭解決方案。它應該現在工作正常。 – user2004685

+0

它的工作表示感謝 – allDroid

0

我建議做以下幾點:

爲了區分QuestionText,您需要使用question_text_id覆蓋equals方法。否則兩個相同的問題question_id,但不同的語言文本將是平等的。

然後爲每種語言創建兩個單獨的地圖。然後只需遍歷所有問題,並通過question_id將每個問題放在相應的地圖中。您可以通過它question_id retrive一個問題對象,並從中獲得所有必要的領域,包括西班牙文/英文文本

List<QuestionText> questionTextList = questionManager.getQuestionsTextByQuestionId(Long.parseLong(questions.getQuestionTextId().toString())); 
for (QuestionText questionText : questionTextList) { 

    if(questionText.getLanguageId().longValue() == 1){ 
     englishQuestionMap.put("question_id",questionText); 
    } else { 
     spanishQuestionMap.put("question_id",questionText); 
    } 
    questionListMap.add(questionMap); 
}    

所以,你的地圖將有Map<Long, QuestionText>