16

I have list List<Long> list, that contains: [160774, 7212775] and Long id = 7212775. I need to check if the list contains an element with value of id. How to do that? Unfortunately list.contains(id) returns false in my case.

I'm using it that way:

@RequestMapping("/case/{id}")
public String openCase(@PathVariable("id") Long id) {

    log.debug(caseDAO.findAllCasesId());    // [160774, 7212775]
    log.debug(id); // 7212775

    if(caseDAO.findAllCasesId().contains(id)) {
        return "case";
    } else {
        return "404";
    }
}

Piece of DAO (Hibernate, but native sql here):

public List<Long> findAllCasesId() {
    String sql = "select id from cases";
    SQLQuery query = getSession().createSQLQuery(sql);
    return query.list();
}

SOLVED

The problem was with caseDAO.findAllCasesId(), that return list of Object, not list of Long. I corrected this by:

SQLQuery query = getSession().createSQLQuery(sql).addScalar("id", Hibernate.LONG);

Big thanks to: Nayuki Minase

Community
  • 1
  • 1
marioosh
  • 27,328
  • 49
  • 143
  • 192
  • I am not seeing the problem. Please post your code? – Nayuki Aug 18 '11 at 20:34
  • 2
    Are you absolutely sure that `caseDAO.findAllCasesId()` has type `List`? Note that `List.contains(Object)` takes an `Object`, so it's easy to mistakenly test for something that cannot possibly exist. – Nayuki Aug 18 '11 at 20:39
  • It's possible that the particular implementation of `List` does not satisfy the contract for `contains()`. Try to check using a `for`-loop and `equals()` to see if it's a true bug. – Nayuki Aug 18 '11 at 21:28
  • @Nayuki You have right. I check `instanceof` element of list and it is not Long! It is Object, hence the problem was. Very big thanks. – marioosh Aug 19 '11 at 07:51
  • Okay, glad to hear it's solved. I think on Stack Overflow you should post an answer yourself and then accept it, heh, so that future readers can benefit. – Nayuki Aug 19 '11 at 13:19

4 Answers4

8

When autoboxing, you need to make sure you postfix the literal with an L i.e. Long id = 7212775L for this to work.

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
  • 1
    To not confuse people: We're obviously talking about a *post*fix literal here ;) – Voo Aug 18 '11 at 20:39
6

Running the code below on eclipse helios:

public static void main(String[] args) {
    List<Long> list = new ArrayList<Long>();
    list.add(160774L);
    list.add(7212775L);
    System.out.println(list.contains(7212775L);
}

Output:

true

What you're doing wrong is

System.out.println(list.contains(7212775));

The problem is that your list takes Long objects and you are searching for a literal.

Dhruv Gairola
  • 9,102
  • 5
  • 39
  • 43
3
List<Long> list = new ArrayList<Long>(Arrays.asList(160774L, 7212775L));
Long id = 7212775L;
System.out.println(list.contains(id)); // prints true
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
0

Hum, I believe List list should return true in your case. The following piece of code returns true.

List<Long> listOfLongs = new java.util.ArrayList<Long>();
    listOfLongs.add(160774L);
    listOfLongs.add(7212775L);
    return listOfLongs.contains(7212775L);

Same with

List<Long> listOfLongs = new java.util.ArrayList<Long>();
    listOfLongs.add(Long.valueOf(160774L));
    listOfLongs.add(Long.valueOf(7212775L));
    return listOfLongs.contains(Long.valueOf(7212775L));

If you could show us the code where this ain't working, it would help.

Alexis
  • 1