Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #332 List of the keys of a map

Create the list k containing all the keys of the map m

fgl
type
  TList = specialize TFPGList<TKey>;
  TMap = specialize TFPGMap<TKey, TData>;

var
  m: TMap;
  k: TList;

begin
  ....
  k := TList.Create;
  for idx := 0 to m.Count-1 do k.Add(m.Keys[idx]);
  .....
  k.Free;
  ....
final k = m.keys;
import "golang.org/x/exp/maps"
k := maps.Keys(m)
k := make([]K, 0, len(m))
for key := range m {
	k = append(k, key)
}
import java.util.ArrayList;
import java.util.List;
List<K> k = new ArrayList<>(m.keySet());
import static java.util.List.copyOf;
import java.util.List;
List<K> k = copyOf(m.keySet());
@k = keys %m;
k = m.keys()
k = [*m.keys()]
k = list(m.keys())
k = m.keys
use std::collections::HashMap;
m.keys().collect::<Vec<_>>()

New implementation...
< >
programming-idioms.org