String Grouping using Hashmap
I am displaying some List of Questions and Answers in JSP. All the Question and answer are displaying alternatively. There may be same questions with multiple answers. I want 开发者_Python百科to group all the questions if questions are twice, then related answers should display.
For example,Now it is displaying like this:
**Test Question one?
Test Answer one
Test Question two?
Test Answer two
Test Question one?
Test Answer one ( again)**
But i want to display like this:
**Test Question one?
Test Answer one
Test Answer one ( again)
Test Question two?
Test Answer two**
How to do that?
You can use guava's HashMultimap if you want to associate the same key with multiple values:
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
public class HashMultimapTest {
public static void main(String[] args) {
Multimap<String,String> map = HashMultimap.create();
map.put("question 2", "answer 2");
map.put("question 1", "answer 1");
map.put("question 3", "answer 3");
map.put("question 1", "another answer 1");
System.out.println(map);
//{question 1=[answer 1, another answer 1],
// question 2=[answer 2],
//question 3=[answer 3]}
}
}
Make numerical ID for every question, with step 100. Create "again" question adding 1, to base question ID. Than just sort by question ID.
精彩评论