Home > OS >  Unchecked assignment: 'java.util.List' to 'java.util.Collection<? extends manu.app
Unchecked assignment: 'java.util.List' to 'java.util.Collection<? extends manu.app

Time:11-02

I am having an adapter where i have two lists one list is for InvestorsList where it comes with the list of investors and the other list is called investorListFull which is used to filter results when searching.

Below is how i have declared the lists

 List<Investor> investorList;
 List<Investor> investorListFull;

Below is how the lists are assigned in my recyclerview adapter constructor


public InvestorsAdapter(Context context, List<Investor> investorList) {

        this.context = context;
        this.investorList = investorList;

        investorListFull = new ArrayList<>(investorList);

    }

Below is how i am filtering results in investors list

public Filter getInvestorFilter() {
        return investorFilter;
    }

    private final Filter investorFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            List<Investor> filteredList = new ArrayList<>();

            if (constraint == null || constraint.length() == 0) {
                filteredList.addAll(investorListFull);
            } else {
                String filterPattern = constraint.toString().toLowerCase().trim();

                for (Investor investor : investorListFull) {

                    if (investor.getUsername().toLowerCase().contains(filterPattern)) {
                        filteredList.add(investor);
                    }

                    if (investor.getDateJoined().toLowerCase().contains(filterPattern)) {
                        filteredList.add(investor);
                    }

                }
            }

            FilterResults filterResults = new FilterResults();
            filterResults.values = filteredList;

            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults filterResults) {
            investorList.clear();
            investorList.addAll((List) filterResults.values);
            notifyDataSetChanged();
        }
    };

I am getting Unchecked assignment error in publish results investorList.addAll((List) filterResults.values);

CodePudding user response:

I am getting Unchecked cast error in publish results investorList.addAll((List) filterResults.values);

That's because you're doing an unchecked cast. Actually, you're doing both a checked and an unchecked cast there.

(List) filterResults.values is a checked cast. This means that the Java compiler will insert a checkcast instruction into the bytecode to ensure that filterResults.values (an Object) really is an instance of List.

However, investorList.addAll expects a List<Investor>, not a List. List is a raw type. You can pass a raw-typed List to a method expecting a List<Something>; but this is flagged as unsafe because the compiler can't guarantee that it really is a List<Something> - there is nothing about the List that makes it a "list of Something", because of type erasure. The compiler can insert a checkcast instruction to ensure it's a List; but not one to ensure it's a List<Something>: this fact is unchecked.

What it's saying with the warning is "there may be something wrong here; I just can't prove it's wrong". If you know for sure - by construction - that filterResults.values really is a List<Investor>, casting it to List<Investor> is safe.

You should write the line as:

investorList.addAll((List<Investor>) filterResults.values);

Note that this will still give you an unchecked cast warning, because it's still an unchecked cast - you just avoid the use of raw types as well.

If you feel confident in suppressing the warning, declare a variable, so you can suppress the warning specifically on that variable, rather than having to add the suppression to the method or class; and document why it's safe to suppress there:

@SuppressWarnings("unchecked") // Safe because <reason>
List<Investor> listOfInvestors = (List<Investor>) filterResults.values;
investorList.addAll(listOfInvestors);
  • Related