-2

In my app I created an ArrayList in some servlet which was assigned using request.setAttribute() as 'testList'. Then request was forwarded to jsp page. Inside jsp page I want to retrieve whole ArrayList and iterate over it.

I used getAttribute(), but after that I cannot retrieve an ArrayList from that object.

An ArrayList contains objects TestObject created by me in another class (which was imported properly). I would like to get access to it by iteration over ArrayList.

Object testList = request.getAttribute("testList");
ArrayList<TestObject> localList = new ArrayList<TestObject>();
localList = testList;
//Type mismatch: cannot convert from Object to ArrayList<TestObject>

What is a best practice to obtain data from Object to ArrayList?

Should I use a cast (IDE warns: Type safety: Unchecked cast from Object to ArrayList)? Or generics? Or... ?

  • Just so you know, using `localList = new ArrayList<>()` and then `localList = testlist` doesn't copy the testlist to the newly created `ArrayList`. It just discards it and assigns a reference to the existing testlist to `localList`, so that `testlist` and `localList` point to the same list. – RealSkeptic Jan 08 '19 at 13:03
  • 1
    What you need is [a cast](https://www.baeldung.com/java-type-casting). – Ole V.V. Jan 08 '19 at 13:20
  • 1
    Possible duplicate of [Java - Type mismatch: cannot convert from element type Object to String](https://stackoverflow.com/questions/23853404/java-type-mismatch-cannot-convert-from-element-type-object-to-string) – Ole V.V. Jan 08 '19 at 13:22
  • You've jumped into the deep end without getting your feet wet first. https://docs.oracle.com/javase/tutorial/java/generics/index.html – chb Jan 08 '19 at 13:41
  • @RealSkeptic Thanks, I'm in the beginning of learning Java, very often I forgot some principals like that. Argh! – artisticImpresion Jan 10 '19 at 20:41
  • @chb Yes, I looks like that. Thanks for a helpful link to generics subject. I have to study it... – artisticImpresion Jan 10 '19 at 20:44

2 Answers2

0

What you need is a cast. The defensive solution checks that the type is correct before casting:

    Object testList = request.getAttribute("testList");
    if (testList instanceof Collection) {
        for (Object obj : (Collection<?>) testList) {
            if (obj instanceof TestObject) {
                TestObject currentTestObject = (TestObject) obj;
                // Do something with currentTestObject
            }
        }
    }

To iterate over the list returned from request.getAttribute you only need to assume it is a Collection, not necessarily ArrayList. Since ArrayList implements the Collection interface the above code will work with ArrayList and with other collection types too. If you only want it to work with List or only ArrayList, just use one of those types instead of Collection in the code.

Consider adding else parts to the if statements and issue a log message and do some other appropriate action if a runtime type wasn’t as expected.

If you are sure you always get an ArrayList<TestObject>, you may omit the checks:

    Collection<TestObject> testList = (Collection<TestObject>) request.getAttribute("testList");
    for (TestObject currentTestObject : testList) {
        // Do something with currentTestObject
    }

This is somewhat simpler.

Link: Object Type Casting in Java

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
-2

You can pass your object to your ArrayList constructor :

Object testList = request.getAttribute("testList");
ArrayList<TestObject> localList = new ArrayList<TestObject>(testList);
  • 1
    It won't work - 'The constructor ArrayList(Object) is undefined'. I think you thought to use 'public ArrayList(Collection extends E> c)' constructor. But in this case 'testList' is still an object not an ArrayList. – artisticImpresion Jan 09 '19 at 21:01