0

I am trying to compare two Integer lists in Java, using the method retainAll, but the return is not what I expected and I don't seem to find the problem here. In the following code, within a servlet:

// some data recieved via JSon
int id_questao = Integer.valueOf(String.valueOf(resp.get("id_questao")));
ArrayList<Integer> idsAlt = (ArrayList<Integer>) resp.get("alt_selecionadas");
System.out.println(idsAlt);
// some database data
ArrayList<Integer> gabarito = new Ead_AlternativasDAO().getAlternativasCorretasPorQuestao(id_questao);
System.out.println(gabarito);

// comparing lists
ArrayList<Integer> auxiliar = new ArrayList<Integer>();
auxiliar.addAll(idsAlt);
System.out.println("auxiliar");
System.out.println(auxiliar);
auxiliar.retainAll(gabarito);
System.out.println("after retainAll: ");
System.out.println(auxiliar);

I got the following printed in debug:

idsAlt
[64, 65]
gabarito
[64, 65]
auxiliar
[64, 65]
after retainAll: 
[]

It should be [64,65] also, shouldn't it? What may I be doing wrong?

2
  • 2
    I agree, that should be the expected result. This seems odd... I can only imagine someone was able to fill the idsAlt/gabarito lists with proxied Integer (I wouldn't know how to do that) ... I would iterate those lists and print the getClass() for their elements ... something there is not what it looks like Commented Apr 13, 2020 at 18:57
  • Are you sure that the cast to ArrayList<Integer> is correct (and not ArrayList<Long>, for example)? Commented Apr 13, 2020 at 21:20

2 Answers 2

1

I managed to reproduce your debug with the following snippet:

    public static void main(String...args) {
        ArrayList<Integer> idsAlt = (ArrayList<Integer>)getAltSelecionadas();
        System.out.println(idsAlt);
        // some database data
        List<Integer> gabarito = getGabaritoFromDao();
        System.out.println(gabarito);

        // comparing lists
        ArrayList<Integer> auxiliar = new ArrayList<Integer>();
        auxiliar.addAll(idsAlt);
        System.out.println("auxiliar");
        System.out.println(auxiliar);
        boolean retained = auxiliar.retainAll(gabarito);
        System.out.println(retained + " after retainAll: ");
        System.out.println(auxiliar);
    }

    private static ArrayList<Integer> getGabaritoFromDao() {
        return new ArrayList<>(Arrays.asList(64, 65));
    }

    private static ArrayList getAltSelecionadas() {
        return new ArrayList(List.of(64L, 65L));
    }

So you need to verify the types of the data returned by resp.get("alt_selecionadas"); (why it had to be cast to ArrayList?) and/or getAlternativasCorretasPorQuestao(Integer id_questao) in Ead_AlternativasDAO

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you are storing instances of your own class in the lists and not integers

retainAll compares the content using the equals method. If you are storing instances of your own class and that class do not override equals it will compare references

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.