0

I am a little confused on how the Generics of Java work and am hoping someone can help me understand a little better.

I am calling a method from another class.... Here is the method that I am calling.

public List<?> getPagedList() throws Exception;

When I call this method like so

myList = (List<Trade>) getPagedList();

I get a TypeSafety warning saying unchecked cast.

I tried changing the method to this

<T> T getPagedList(Class<T> myClass) throws Exception;

But I cannot seem to get the class object of List like this

getPagedList((List<Trade>).class

Any ideas or direction I can start learning?

EDIT ---- The class

public class Pagination{
    private static final int MAX_PAGE_LENGTH = 20;
    private static final int MAX_PAGES = 5;

    private int currentPage;
    private List list;

    public Pagination(List<?> list, String currentPage){
        this.list = list;

        if(currentPage == null)
            this.currentPage = 1;
        else
            this.currentPage = Integer.parseInt(currentPage);
    }

    public <T> List<T> getPagedList() throws Exception{
        if(currentPage * MAX_PAGE_LENGTH + MAX_PAGE_LENGTH > list.size()){
            return list.subList(currentPage*MAX_PAGE_LENGTH, list.size());
        }else{
            return list.subList(currentPage * MAX_PAGE_LENGTH, currentPage * MAX_PAGE_LENGTH + MAX_PAGE_LENGTH);
        }
    }
}

My Call

    List<Trade> ts = (Some Code to put objects in ts)
    Pagination paging = new Pagination(ts, currentPage);
    List<Trade> ts = paging.getPagedList();
4
  • 1
    What about this? <T> List<T> getPagedList() throws Exception; and calling it with List<Trade> trades = getPagedList() Commented Jun 25, 2015 at 20:39
  • Something here is going to be unsafe unless you actually treat the List<?> as a list whose elements are of some random unknown type. Commented Jun 25, 2015 at 20:39
  • Type safety warning here is actually saying "I am not responsible for guarantee that this casting is going to work", but if you know that you are always going to pass a list object, this should be okay. There are lots of back-end code written prior to Java 1.5 that used such type casting and they are still working even after upgrading to java 1.7. Commented Jun 25, 2015 at 20:39
  • How will you guarantee that the List you return actually contains Trade values? Commented Jun 25, 2015 at 20:46

3 Answers 3

1

No need to pass a parameter:

public class Pagination<T> {

    private static final int MAX_PAGE_LENGTH = 20;
    private static final int MAX_PAGES = 5;

    private int currentPage;
    private List<T> list;

    public Pagination(List<T> list, String currentPage){
        this.list = list;

        if (currentPage == null)
            this.currentPage = 1;
        else
            this.currentPage = Integer.parseInt(currentPage);
    }

    public List<T> getPagedList() throws Exception {
        if (currentPage * MAX_PAGE_LENGTH + MAX_PAGE_LENGTH > list.size()){
            return list.subList(currentPage * MAX_PAGE_LENGTH, list.size());
        }
        return list.subList(currentPage * MAX_PAGE_LENGTH, currentPage * MAX_PAGE_LENGTH + MAX_PAGE_LENGTH);
    }
}

Isn't that what you want? The "magic" here is to have a generic class Pagination where T is the same type parameter throughout the whole class.

And here's how to instantiate it (mind the diamond operator <> which was introduced in Java 7 and helps to reduce redundant information):

Pagination<Trade> p = new Pagination<>(myListOfTrades, null);
Sign up to request clarification or add additional context in comments.

3 Comments

Hmm, well when I did that the class was giving me an warning saying that my declaration of my list wasn't generic. So what I tried is to change the class to a generic class using public class Pagination<T> and then calling the new method with the declaration. But I couldnt get that to work either.
@user3585563 If you post what you've written, maybe I can help. I verified the code I wrote and I didn't get any compiler warnings.
I went ahead and updated the code to show the class and the method call. I just get warning messages and I know there has to be a way to fix them. I just want to be able to write it the right way :)
1

You can do this

<T> List<T> getPagedList(Class<T> myClass) throws Exception;

This means you can pass the type of the element as an argument.

Comments

1

To expand on my comment, you can use this as the signature of the method:

<T> List<T> getPagedList(Class<T> type) throws Exception;

And call it like this:

List<Trade> trades = getPagedList(Trade.class);

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.