Speed/expensive of SQLite query vs. List.contains() for "in-set" icon on list rows
<p>An application I'm developing requires that the app main a local list of things, let's say books, in a local "library." Users can access their local library of books and search for books using a remote web service. The app will be aware of other users of the app through this web service, and users can browse other users' lists of books in their library. Each book is identified by a unique <code>bookId</code> (represented as an <code>int</code>).</p>

<p>When viewing books returned through a search result or when viewing another user's book library, the individual list row cells need to visually represent if the book is in the user's local library or not. A user can have at most 5,000 books in the library, stored in SQLite on the device (and synchronized with the remote web service).</p>

<p>My question is, to determine if the book shown in the list row is in the user's library, would it be better to directly ask SQLite (via <code>SELECT COUNT(*)...</code>) or to maintain, in-memory, a <code>List</code> or <code>int[]</code> array of some sort containing the unique <code>bookId</code>s.</p>

<p>So, on each row display do I query SQLite or check if the <code>List</code> or <code>int[]</code> array contains the unique <code>bookId</code>? Because the user can have at most 5,000 books, each <code>bookId</code> occupies 4 bytes so at most this would use ~ 20kB.</p>

<p>In thinking about this, and in typing this out, it seems obvious to me that it would be far better for performance if I maintained a list or int[] array of in-library <code>bookId</code>s vs. querying SQLite (the only caveat to maintaining an int[] array is that if books are added or removed I'll need to grow or shrink the array by hand, so with this option I'll most likely use an <code>ArrayList</code> or <code>Vector</code>, though I'm not sure of the additional memory overhead of using <code>Integer</code> objects as opposed to primitives).</p>

<p>Opinions, thoughts, suggestions?</p>
