Forum Stats

  • 3,770,456 Users
  • 2,253,116 Discussions
  • 7,875,463 Comments

Discussions

How to retrieve all the keys of a particular database?

user12033180
user12033180 Member Posts: 4
edited May 20, 2014 12:58PM in Berkeley DB Java Edition

Hi,

I need to get all the primary keys of database and assign to java list. I found that the PrimaryIndex.keys() provides all the keys and returns an EntityCursor<K>. So, using this method I need to traverse through the entire cursor to collect all the key values. Is there any better/efficient method available to get all the keys which I can assign to a java list?

Any help is highly appreciated.

Thanks,

Bibhu

Answers

  • Greybird-Oracle
    Greybird-Oracle Member Posts: 2,690

    If you need an efficient way to view the keys as a java.util.Collection or Set, use PrimaryIndex.sortedMap().keySet().  This does not copy the keys -- the returned Set is a "view" on the database -- so it is appropriate for large numbers of keys and high performance applications.

    If you must have a List, or you want to make a copy of the keys, then you can pass the key set (PrimaryIndex.sortedMap().keySet()) to the constructor of an ArrayList (for example) to copy it easily.

    --mark

    Greybird-Oracle
This discussion has been closed.