Compare int[] with once Integer

I have table of int[]. I have for loop with every user and I do:

int[] ids = (values) 
for (User user : listAfterProcessing) {
    if (user.getId().equals(ids)) { ... } 

They didnt work, Although the user id is in this table... Thanks for help :)

2 answers

  • answered 2018-01-13 17:15 B from C

    You are checking whether the result of getId() equals the entire array ids, which is by default a comparison to the array's hashcode.

    According to an answer in this question: How to convert int[] into List<Integer> in Java?

    there is no quick way to convert.

    The solution with Arrays.asList(), which I suggested in the first version of my answer, doesn't work here, sorry.

  • answered 2018-01-13 17:17 O.O.Balance

    Comparing an int array to a single Integer will not get you the result you want. You could go through the whole array and test every cell for the value, or you could keep the array sorted and do a binary search:

    import java.util.Arrays;
    if(Arrays.binarySearch(ids, user.getId().intValue()) >= 0) {}