1

I have a HashMap which looks like this:

HashMap<QualifiedProduct,List<Date>

so my java code looks like this:

@Name("testController")
@AutoCreate
public class TestController {
public HashMap<QualifiedProduct, List<Date>> createAndReturnHashmap(){

    HashMap<QualifiedProduct, List<Date>> hm = new HashMap<QualifiedProduct, List<Date>>();

    QualifiedProduct q1= new QualifiedProduct();
    q1.setAbstractProductId(1);
    q1.setAbstractProductCode("1-1");
    q1.setAbstractProductName("one");
    q1.setAbstractProductTypeName("type1");
    q1.setCustomersCount(1);
    q1.setQualificationDueDate(new Date());
    q1.setQualificationsCount(1);

    QualifiedProduct q2= new QualifiedProduct();
    q2.setAbstractProductId(2);
    q2.setAbstractProductCode("2-2");
    q2.setAbstractProductName("two");
    q2.setAbstractProductTypeName("type2");
    q2.setCustomersCount(2);
    q2.setQualificationDueDate(new Date());
    q2.setQualificationsCount(2);

    QualifiedProduct q3= new QualifiedProduct();
    q3.setAbstractProductId(3);
    q3.setAbstractProductCode("3-3");
    q3.setAbstractProductName("three");
    q3.setAbstractProductTypeName("type3");
    q3.setCustomersCount(3);
    q3.setQualificationDueDate(new Date());
    q3.setQualificationsCount(3);



    List<Date> dt = new ArrayList<Date>();
    Date d1 = new Date();
    Date d2 = new Date();
    Date d3 = new Date();
    dt.add(d1);
    dt.add(d2);
    dt.add(d3);
    hm.put(q1, dt);
    hm.put(q2, dt);
    hm.put(q3, dt);

    return hm;

}

as i read in many other postings on stackoverflow like here,or here the best way to show it in JSF 1.2 is using the good old jsp tag <c:forEach

So i used in my code:

xmlns:c="http://java.sun.com/jstl/core"
.....
<c:forEach var="hm" items="${testController.createAndReturnHashmap()}">
  ${hm.key} <br/>
  and value is:  ${hm.value} <br/><br/>
</c:forEach>

The JSF output is:

com.dw.model.QualifiedProduct@15d97b65[abstractProductId=2,abstractProductCode=2-2,abstractProductName=two,abstractProductTypeName=type2,qualificationsCount=2,customersCount=2,qualificationDueDate=Thu Jun 11 14:47:37 CEST 2015,id=,version=,created=,modified=] and value is:

com.dw.model.QualifiedProduct@2393366a[abstractProductId=1,abstractProductCode=1-1,abstractProductName=one,abstractProductTypeName=type1,qualificationsCount=1,customersCount=1,qualificationDueDate=Thu Jun 11 14:47:37 CEST 2015,id=,version=,created=,modified=] and value is:

com.dw.model.QualifiedProduct@79342f17[abstractProductId=3,abstractProductCode=3-3,abstractProductName=three,abstractProductTypeName=type3,qualificationsCount=3,customersCount=3,qualificationDueDate=Thu Jun 11 14:47:37 CEST 2015,id=,version=,created=,modified=] and value is:

So the value is always empty.

but if i use a HashMap like this HashMap<String,List<Date>> everything works:

public HashMap<String, List<Date>> createAndReturnHashmap(){

    HashMap<String, List<Date>> hm = new HashMap<String, List<Date>>();

    List<Date> dt = new ArrayList<Date>();
    Date d1 = new Date();
    Date d2 = new Date();
    Date d3 = new Date();
    dt.add(d1);
    dt.add(d2);
    dt.add(d3);
    hm.put("one", dt);
    hm.put("two", dt);
    hm.put("three",dt);

    return hm;

}

and the result is:

two and value is: [Thu Jun 11 14:54:22 CEST 2015, Thu Jun 11 14:54:22 CEST 2015, Thu Jun 11 14:54:22 CEST 2015]

one and value is: [Thu Jun 11 14:54:22 CEST 2015, Thu Jun 11 14:54:22 CEST 2015, Thu Jun 11 14:54:22 CEST 2015]

three and value is: [Thu Jun 11 14:54:22 CEST 2015, Thu Jun 11 14:54:22 CEST 2015, Thu Jun 11 14:54:22 CEST 2015]

Where is the differenct in using a non Standard object as a key???

Community
  • 1
  • 1
Joergi
  • 1,527
  • 3
  • 39
  • 82

2 Answers2

1

Updating the answer as you changed the question.

If you want to use custom object as key in HashMap, you have to override hashCode() and equals() method in your custom object. here is the example of how to do that .


Previous answer for checking size of list in jsp ( original question.)

Try using ${fn:length(myItem.value)} to check list size. For this add <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> in your jsp

EL works on getters and setter. If you are doing list.size, it looks for a method list.getSize() on List object, which is not there. It has list.size() instead. So we have to use this jstl function to retrieve size of list.

Amit.rk3
  • 2,417
  • 2
  • 10
  • 16
  • size is 0 - that's why i don't get a value. but java tells me, there is a value (see the for each in java above) – Joergi Jun 11 '15 at 11:04
  • are you getting `0` printed in jsp ? I doubt that – Amit.rk3 Jun 11 '15 at 11:06
  • yes, i do! if there wouldn't be a 0, a i would have been able to get the value with my jsp code too – Joergi Jun 11 '15 at 11:08
  • are you getting same output using the above solution which I gave ? – Amit.rk3 Jun 11 '15 at 11:09
  • yes, as i wrote... i also added some more output to my original question – Joergi Jun 11 '15 at 11:12
  • if i use: list.size i get nothing - with your fn: length(myitem.value) it get a 0 printed – Joergi Jun 11 '15 at 11:15
  • 1
    As I explained, list.size is not valid, so you can skip that. now it seems to be some logic issue in your code. Also ` ${myItem} ` using this what output you are getting. I think you are printing list object directly here – Amit.rk3 Jun 11 '15 at 11:23
  • i want the list values in my oiriginal posting!.... the problem is, that with my original explanation, the list seems to be empty - that's why i showed in a differnt way, that it its not! – Joergi Jun 11 '15 at 12:03
  • hey @Amit.rk3 - I totally edited the question, to make it much more clearer... the problem is not the value, the problem is the non standard object key – Joergi Jun 11 '15 at 12:59
0

as far as i recognized, the forEach is not working with a non java-standard object. the best is to make a wrapper class

public class QualificationProductWithListOfOrderingMailingDate {

    private HashMap<QualifiedProduct,List<Date>> qualificationProductWithListOfOrderingMailingDate;

    public HashMap<QualifiedProduct,List<Date>> getQualificationProductWithListOfOrderingMailingDate() {
        return qualificationProductWithListOfOrderingMailingDate;
    }

    public void setQualificationProductWithListOfOrderingMailingDate(
            HashMap<QualifiedProduct,List<Date>> qualificationProductWithListOfOrderingMailingDate) {
        this.qualificationProductWithListOfOrderingMailingDate = qualificationProductWithListOfOrderingMailingDate;
    } 

}

than it is no problem at all to loop over every product:

<ui:repeat var="x" value="#{testController.createAndReturnHashmap()}">
    #{x.qualifiedProduct.abstractProductId},#{x.qualifiedProduct.abstractProductCode}

    <ui:repeat var="dt" value="#{x.dates}">
            #{dt}
    </ui:repeat>

</ui:repeat>
Joergi
  • 1,527
  • 3
  • 39
  • 82