For what you need usually there's TryGetValue.
Currently there's no such implementation but, it should be easy to create.
It would look like this:
program test;
{$mode objfpc}{$H+}
uses
sysutils,
gutil,
gmap;
type
{ TElem }
TElem = class
public
Name: string;
constructor Create(const aName: string);
end;
TElemMapCmp = specialize TLess<integer>;
TElemMap = specialize TMap<integer, TElem, TElemMapCmp>;
{ TElem }
constructor TElem.Create(const aName: string);
begin
self.Name := aName;
end;
var
children: TElemMap;
el: TElem;
i: integer;
it: TElemMap.TIterator;
Value: TElem;
begin
children := TElemMap.Create;
try
for i := 0 to 9 do
begin
el := TElem.Create(IntToStr(i));
children[2*i] := el;
end;
it := children.Min;
if it <> nil then
repeat
writeln(format('%d -> "%s"', [it.key, it.value.name]));
until not it.next;
it.free;
writeln('Test TryGetValue');
for i := 0 to 100 do
if children.TryGetValue(i, Value) then
writeln(format('%d -> "%s"', [i, value.Name])); // key -> value
// ... free elements
finally
children.Free;
end;
readln;
end.
so if tested value in range 0 . . 100 will be absent (gap) TryGetValue return False.