Represents a range of unique datastore identifiers from getStart().getId() to
getEnd().getId() inclusive. If an instance of this class is the result of a call to
DatastoreService.allocateIds(), the Keys returned by this instance have been
consumed in the datastore's id-space and are guaranteed never to be reused.
This class can be used to construct Entities with Keys that have
specific id values without fear of the datastore creating new records with those same ids at a
later date. This can be helpful as part of a data migration or large bulk upload where you may
need to preserve existing ids and relationships between entities.
This class is threadsafe but the Iterators returned by #iterator() are
not.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["\u003cp\u003e\u003ccode\u003eKeyRange\u003c/code\u003e represents a range of unique datastore identifiers, from a start key to an end key, inclusive.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eKeyRange\u003c/code\u003e guarantees that the returned keys from the \u003ccode\u003eDatastoreService.allocateIds()\u003c/code\u003e method will not be reused in the datastore's ID space.\u003c/p\u003e\n"],["\u003cp\u003eThis class is beneficial when you need to use specific ID values for \u003ccode\u003eEntities\u003c/code\u003e, ensuring that the datastore will not create new records with those same IDs.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eKeyRange\u003c/code\u003e class is thread-safe, but the \u003ccode\u003eIterators\u003c/code\u003e returned by its \u003ccode\u003eiterator()\u003c/code\u003e method are not thread-safe.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eKeyRange\u003c/code\u003e class implements the \u003ccode\u003eIterable<Key>\u003c/code\u003e and \u003ccode\u003eSerializable\u003c/code\u003e interfaces.\u003c/p\u003e\n"]]],[],null,["# Class KeyRange (2.0.0)\n\n public final class KeyRange implements Iterable\u003cKey\u003e, Serializable\n\nRepresents a range of unique datastore identifiers from `getStart().getId()` to `\ngetEnd().getId()` inclusive. If an instance of this class is the result of a call to `\nDatastoreService.allocateIds()`, the [Keys](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) returned by this instance have been\nconsumed in the datastore's id-space and are guaranteed never to be reused. \n\nThis class can be used to construct [Entities](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Entity) with [Keys](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) that have\nspecific id values without fear of the datastore creating new records with those same ids at a\nlater date. This can be helpful as part of a data migration or large bulk upload where you may\nneed to preserve existing ids and relationships between entities. \n\nThis class is threadsafe but the [Iterators](https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html) returned by [#iterator()](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyRange#com_google_appengine_api_datastore_KeyRange_iterator__) are\nnot. \n\nInheritance\n-----------\n\n[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \\\u003e KeyRange \n\nImplements\n----------\n\n[Iterable\\\u003cKey\\\u003e](https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable\u003ccom/google/appengine/api/datastore/Key\u003e.html), [Serializable](https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html) \n\nInherited Members\n-----------------\n\n[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) \n[Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) \n[Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) \n[Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) \n[Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) \n[Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) \n[Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) \n[Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) \n[Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) \n[Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) \n[Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)\n\nConstructors\n------------\n\n### KeyRange(Key parent, String kind, long start, long end)\n\n public KeyRange(Key parent, String kind, long start, long end)\n\nMethods\n-------\n\n### equals(@Nullable Object obj)\n\n public boolean equals(@Nullable Object obj)\n\n**Overrides** \n[Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-)\n\n### getEnd()\n\n public Key getEnd()\n\nReturns the last [Key](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) in the range.\n\n### getSize()\n\n public long getSize()\n\nReturns the size of the range.\n\n### getStart()\n\n public Key getStart()\n\nReturns the first [Key](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) in the range.\n\n### hashCode()\n\n public int hashCode()\n\n**Overrides** \n[Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--)\n\n### iterator()\n\n public Iterator\u003cKey\u003e iterator()"]]