The way you describe is typafe (actually it is not, but it will throw a ClassCastException if you're wrong), but probably the slowest and surest way to do this (although this is pretty clear). The above example, which simply distinguishes ArrayList, is the fastest, but, as the poster indicates, is not typical, and, of course, you probably want to copy it to another list. If you are ready to give up the need to copy it into an ArrayList and are happy with the list instead (which should be yours), simply do:
List<ProductListBean> productList = Arrays.asList(((List<ProductListBean>)getProductsList()).toArray(new ProductListBean[] {}));
This is fast because basic copying is done using System.arraycopy, which is a native method, and it types types (well, not exactly, but it is safe, as your example), because System.arraycopy will throw an ArrayStoreException if you try to add something that is not of the same type.
source share