vaibhav
vaibhav

Reputation: 4103

Generics related problem

My code is as below:

private <A extends AbstractDocument> List<A> reorderDocuments(List<A> docs)
    {
        List<A> newdoclist = new ArrayList<A>();
        for (A o : docs) {
            if(//some condition) {
                //TODO::Generic type
                List<A> tempDocs = new ArrayList<A>();
                tempDocs.add( o );
                tempDocs.addAll(o.getAlikeDocuments());
                //sort method called
            } 
          return newdoclist;
    }

have changed the start tag for the type with the function o.getAlikeDocuments() returns List of type Abstract document, but this method is still giving me error on line tempDocs.addAll(o.getAlikeDocuments()); saying The method addAll(Collection<? extends A>) in the type List is not applicable for the arguments (List<AbstractDocument>). Appreciate the help in advance.

Thanks

Vaibhav

Upvotes: 1

Views: 72

Answers (2)

Timo Westk&#228;mper
Timo Westk&#228;mper

Reputation: 22200

Casting to a raw list will do the trick

tempDocs.addAll((List) o.getAlikeDocuments());

The following page might help to memorize the restrictions of extends and super

What is PECS (Producer Extends Consumer Super)?

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533870

The problem you have is that A is a subclass of AbstractDocument and you may not add any AbstractDocument except sub-classes of A

To make it compile, if you know this is not a problem is to use type erasure.

 tempDocs.addAll((List) o.getAlikeDocuments());

Upvotes: 3

Related Questions