0

I have list List myList, that contains Long values which is returned from a database operation. I am checking if the list contains a specific Long value. I am trying to do myList.contains(value), but unfortunately it returns false even though the List contains the specific value! Why it is happening? How to resolve it?

Repository class

    @Repository
    public interface CarsRepository extends JpaRepository<Cars, Long> {
        @Query(value = "select id from Cars WHERE colour = ?1", nativeQuery=true)
        public List<Long> fetchCarsWithColour(String colour);
    }

Calling method

    Long specificCarId = 3456L;
    List<Long> carIds = carsRepository.fetchCarsWithColour("red");
    System.out.println(carIds);         // [3456, 3457]
    System.out.println(specificCarId);  // 3456
    if(carIds.contains(specificCarId)){
       System.out.println("Inside if");
       return true;
    }  else {
       System.out.println("Inside else");
       return false;
    }

Console

[3456, 3457]
3456
Inside else

The calling method returns false even though it has the value 3456L.

UPDATE

I have tried printing List carIds as below. It throws exception java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

for(Long value : carIds){
    System.out.println(value);
}

So somehow, the repository call returns List instead of List but because of autoboxing, it perfectly fits into List

User_1940878
  • 321
  • 1
  • 6
  • 25
  • 3
    Does the List actually contain that value? Have you verified it? This is also a segment of a method. What else is going on there? Perhaps you should print just above the return statements. – WJS Apr 22 '21 at 18:00
  • @XavierBrassoud this obviously isn't an async call. those return Futures. – rzwitserloot Apr 22 '21 at 18:00
  • 4
    Your code as pasted is fine; if it isn't returning the result you expected, you'd have to paste more / look elsewhere. For example, this isn't a case-insensitive scan; perhaps in your DB it's got colour 'RED', not 'red'. Or there's a bug in your code (you did not paste your code, you pasted some sort of pseudocode, given that there are obvious errors in there such as missing semicolons). – rzwitserloot Apr 22 '21 at 18:02
  • I have verified many times; the Id 3456 is there in the List. – User_1940878 Apr 22 '21 at 18:04
  • Can you log the ids received in carIds list and paste the output in question? – Durlabh Sharma Apr 22 '21 at 18:32
  • 2
    _"I have verified many times"_ - SHOW us how you verified this. We've all made the mistake: "verifying" something and believing it to be true, but then someone looks over your shoulder and points out the error. – Jim Garrison Apr 22 '21 at 18:56
  • Added System.out.println(carIds) – User_1940878 Apr 22 '21 at 18:57
  • Can you put an informative print statement with the value right before returning true and false? e.g `System.out.println("returning true with -" + specificCarId);` The other issue is that we don't see where you're making the call and checking the return value. The `contains` may actually be working fine. This is where a [mre] would be useful instead of the code fragments. – WJS Apr 22 '21 at 19:14
  • Ok, are you certain this is the **exact** code? I just noticed you're missing a semi-colon at the end of the `fetchCarsWithColour("red")` statement. So this wouldn't even compile. – WJS Apr 22 '21 at 19:26
  • 2
    https://stackoverflow.com/questions/7113715/how-to-check-that-listlong-contains-a-value/7113778 Found similar question here! – User_1940878 Apr 22 '21 at 19:39
  • You should try, " for(Long carId: carIds){ System.out.println(carId.equals(specificCarId));}" that should give you a class cast exception if your "objects" aren't Longs. – matt Apr 22 '21 at 19:55
  • Your library is returning a BigInteger, not a Long. It should be "List carIds" – matt Apr 24 '21 at 10:09
  • yes @matt I have changed it to BigInteger and changed the comparison logic to BigInteger comparison. Now it is working fine. Is there a way to forcefully return Long other than BigInteger from Repository? – User_1940878 Apr 25 '21 at 18:20
  • 1
    @User_1940878 That comes down to your database and config. You might actually try to make a question out of that. – matt Apr 25 '21 at 18:27

0 Answers0