3

I'm making query using native sql with following code

private <T> List<T> executeNativeQuery(String queryString,
        Map<String, Object> param, Class<T> clazz) {
    Query query = entityManager.createNativeQuery(queryString);
    if (param != null && param.size() > 0) {
        for (Map.Entry<String, Object> entry : param.entrySet()) {
            query.setParameter(entry.getKey(), entry.getValue());
        }
    }
    List<T> resultList = query.getResultList();
    return resultList;
}

And getting following database result.

VendorName | IncidentID | IncidentStatus | IncidentDate
-------------------------------------------------------
XYZ        | 100        |     Open       | 02-JUN-2011    
ABC        | 101        |     Closed     | 03-JUN-2011  
MNP        | 102        |     Open       | 01-JUN-2011  
LPQ        | 103        |     Open       | 01-APR-2011  

To iterate this result, i'm using following approach

 Iterator iter=resultList.iterator();
    while (iter.hasNext()) {
        Object[] result= (Object[]) iter.next();
        System.out.println("Vendor Name-->" +result[0]);
 }

Is there any better way to iterate through the list ?

5
  • This is the best you can get. :) Commented Jun 20, 2013 at 18:59
  • You should use Iterator<Object[]>, not bare Iterator. But see the answers for better ideas. Commented Jun 20, 2013 at 19:12
  • @DavidConrad Thanks for pointing out. But definition for this function is - Iterator<E> iterator(). It wouldn't be applied to an array. How can we handle this ? Commented Jun 20, 2013 at 22:10
  • 1
    @Pankaj Sorry, what is the type T of the List<T> that it is returning? Or, to put it another way, what is the type of resultList? You are already casting iter.next() to Object[], so it must be an Iterator of either Object[] or some other array type, right? Commented Jun 21, 2013 at 16:44
  • @DavidConrad You're right David. I was passing String.class as type which was causing this issue. Thanks for pointing out. Commented Jun 24, 2013 at 21:31

2 Answers 2

7

One "better way" (java 5+ for-each) will actually translate to what you've shown, using an iterator, but the syntax is nicer:

for (Object[] result : resultList) {
    // ...

The other "better way" relies on the fact that you know the list size and you can use a counter in a for loop:

for (int i = 0; i < resultList.size(); i++) {
    Object[] result = resultList.get(i);
    // ... the rest ...

To sum up, there are several ways to iterate through a list, but it's arguable that there are "better" ways: it's a matter of use-case (if you're using that i counter for other things as well or you're using the iterator to also remove some elements from the list) or simply a matter of taste (low-level vs. syntax-sugar).

Sign up to request clarification or add additional context in comments.

Comments

0

In Java 5+ the for each loop is the preferred way

for (Object[] result : resultList) {...}

This could have better performance sometimes because it calculates collection size once. Also, when using a traditional for loop and doing a nested iteration over two collections, you might create a bug. Joshua Bloch mentions it in his book 'Effective Java', item 46. A bit more detailed ofcourse.

http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683/ref=dp_ob_title_bk

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.