0

How can I join list and compare with another list

List list = new ArrayList();
List mList = getGroupById(date, id); // have 2 data
List tList = getSummary(date, id); // have 5 data
tList.addAll(create(tlog));

for (Rev m : (List<Rev>) mList) {
    Detail x = new Detail();
    for (Object[] s : (List<Object[]>) tList) {
        if (s[1].toString().equals(m.getMid())) {
            System.out.println("dddd " + s[1]);
        }
    }
}

Query

public List create(List<Alog> tlog) {
    List list = new ArrayList<>();
    ...

    Detail x = new Detail();
    ...
    x.setAbc(abc);
    list.add(x);

    return list;
}

public List getSummary(String date, String id)
{
    StringBuilder bf = new StringBuilder();
    bf.append("SELECT ");
    bf.append("'ABC', ");
    ...
    return em.createQuery(bf.toString())
            .setParameter("date", date, TemporalType.DATE)
            .setParameter("id", id)
            .getResultList();
}

Error

 cannot be cast to [Ljava.lang.Object;
    at com.rh.app.service.reader.SummaryReader.read(SummaryReader.java:182)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) cannot be cast to [Ljava.lang.Object;
    at com.rh.app.service.reader.SummaryReader.read(SummaryReader.java:182)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)

pointed to

for (Object[] s : (List<Object[]>) tList) 
Michael
  • 41,989
  • 11
  • 82
  • 128
John Joe
  • 12,412
  • 16
  • 70
  • 135

1 Answers1

0

The Ljava.lang.Object is the name for Object[].class, the java.lang.Class representing the class of array of Object.

I.e you are trying to cast to Object[] that is not compatible with the real Object you have.

Obviously the result of your query is a List as usual, so you should try with iterators.

Instead of for (Object[] s : (List<Object[]>) tList) use:

Iterator itr = tList.iterator();
while(itr.hasNext()){
   Object[] s= (Object[]) itr.next();

   //now you have one array of Object for each row
   if (String.valueOf(s[1]).equals(m.getMid())) {
       System.out.println("dddd " + String.valueOf(s[1]));
   }
}
Michael
  • 41,989
  • 11
  • 82
  • 128
TiyebM
  • 2,684
  • 3
  • 40
  • 66