-1

I have some parameters which I get from request.getParameterMap() and I would like to iterate through the Map and take the nth value from each row in the Map. Below is an example of the data

Map contains:

recordNo:1,2,3,4,5,6
dob:19800101,19800201,'',19930101,19940101,19950302
addressLn1: well street, prince street,lewis street,edward street,mills street, #3 rich street

What i have is a Map that contains this data i would like to loop through the data and take nth record from the Map array. This is what i want

first iteration print: 1,19800101,well street

second iteration print: 2,19800201,prince street

Code:

 Map map = request.getParameterMap();

  for(int i = 0; i< map.size();i++){

    //how can i access the values with the array at the nth position            
     System.out.println("Value at " +i+ " " +map.get(i).toString());

   }
Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95
devdar
  • 5,564
  • 28
  • 100
  • 153

1 Answers1

1

Your data structure is conceptually incorrect, hence all your problems.

You should instead keep data in a Java Bean:

public class MyBean {
    private String recordNo; // is it an int?
    private String dob; // is it a Date?
    private String addressLn1;
    // getters/setters omitted
}

then somehow add it to the List<MyBean> and iterate through it:

List<MyBean> list = new ArrayList<MyBean>();
// list gets populated in some way, then
for(MyBean bean : list) {
     bean.getDob();
     ...
}

So it appears that you're dealing with HTTP request and get you parameters map from it. In this case you can't just rely on the order of parameters on that map - the order of recordNo array might (and most probably will) be different from the order of dob array.

What you need to do in your case is to give your inputs in HTML unique names, like recordNo0, recordNo1, dob0, dob1 - so that you can guarantee that bod corresponds to the correct recordNo:

<c:forEach varStatus="status" items="${yourCollection}">
    <input type='text' name='recordNo${status.index}' value='some value'>
    <input type='text' name='dob${status.index}' value='some value'>
</c:forEach>

Then you need to iterate through your parameters in servlet or whatever you've got to construct your MyBeans and collecting them in the array:

List<MyBean> list = new ArrayList<MyBean>();
for(int i=0; ;i++) {
    String recNo = request.getParameter("recordNo"+i);
    if(recNo == null) break;
    MyBean bean = new MyBean();
    bean.setRecordNo(recNo);
    bean.setDob(request.getParameter("dob"+i));
    // etc
}
Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95
  • I am trying this right now i want to pass request.getParameterMap() into the list – devdar Apr 30 '13 at 23:30
  • so you should've started with that - that you've got parameters coming from HTTP request and you need to convert them into a better structure... – Oleg Mikheev Apr 30 '13 at 23:32
  • I cannot pass the request.getParameterMap() into a List or ArrayList – devdar Apr 30 '13 at 23:38
  • I am trying this List studentList = new ArrayList(); studentList = (List) request.getParameterMap(); but its not compling – devdar Apr 30 '13 at 23:41
  • updated my answer - you need to rename your parameters in HTML, you will never be able to make getParameterMap work – Oleg Mikheev Apr 30 '13 at 23:42
  • I have one problem here i can give them unique names but the names will not be able to correspond to that which you have when you add the 'i' to it. What can i do in that case? – devdar Apr 30 '13 at 23:52
  • I can get recordNo1233, recordNo5456546, dob1233, dob5456546 names like that cause i will append the recordNo to the name of the element since the elements are also dynamic in the view – devdar Apr 30 '13 at 23:53
  • All the elements in the view gets created in a tag – devdar Apr 30 '13 at 23:56
  • Thank you very much this will work much appreciated wish i could have given you more points here – devdar May 01 '13 at 00:34