14.2.1.2.2 Session-Bound Maps

You can directly create maps in the session. But, you cannot use any graph-related data type as the map key or value type. Session-bound maps can be further passed as parameters to graph algorithms or used like any other map object. They are managed by PgxSession and PgxMaps APIs.

Scalar collections contain simple data types like Integer, Long, Float, Double and Boolean. They can be managed by the PgxSession APIs.

Creation of a Session-bound Map

You can use createMap() method and its overloads to create a session-bound map.

intToDouble = session.createMap(PropertyType.INTEGER, PropertyType.DOUBLE, "intToDouble")
intToTime = session.createMap(PropertyType.INTEGER, PropertyType.TIME)  // A name will be automatically generated.
println intToTime.getName()
println intToTime.getSessionId()
println intToTime.getGraph()                                           // `null`: Not bound to a graph.
println intToTime.getKeyType()
println intToTime.getValueType()
import java.time.LocalTime;
import oracle.pgx.api.*;
import oracle.pgx.common.types.*;
...
PgxMap<Integer, Double> intToDouble = session.createMap(PropertyType.INTEGER, PropertyType.DOUBLE, "intToDouble");
PgxMap<Integer, LocalTime> intToTime = session.createSequence(PropertyType.INTEGER, PropertyType.TIME);
System.out.println(intToTime.getName());
System.out.println(intToTime.getSessionId());
System.out.println(intToTime.getGraph());  // `null`: Not bound to a graph.
System.out.println(intToTime.getKeyType());
System.out.println(intToTime.getValueType());

Run Operations on a Session-bound Map

You can run important operations such as setting, removing and checking existence of entries on a session-bound map as shown in the following code:

intToDouble.put(0, 0.314)
intToDouble.put(1, 3.14)
intToDouble.put(2, 31.4)
intToDouble.put(3, 314)

println intToDouble.size()           // 4
println intToDouble.get(1)
println intToDouble.get(3)
println intToDouble.get(10)          // null

println intToDouble.containsKey(0)   // `true`
intToDouble.remove(0)
println intToDouble.containsKey(0)   // `false`
println intToDouble.containsKey(10)  // `false`
intToDouble.remove(10)
println intToDouble.containsKey(10)  // `false`

println intToDouble.put(1, 999)      //  previous mapped value (`3.14`) is replaced by `999`
intToDouble.destroy()
import java.util.Arrays;
import oracle.pgx.api.*;

...

intToDouble.put(0, 0.314);
intToDouble.put(1, 3.14);
intToDouble.put(2, 31.4);
intToDouble.put(3, 314);

System.out.println(inToDouble.size());            // 4
System.out.println(intToDouble.get(1));
System.out.println(intToDouble.get(3));
System.out.println(intToDouble.get(10));          // null

System.out.println(intToDouble.containsKey(0));   // `true`
intToDouble.remove(0);
System.out.println(intToDouble.containsKey(0));   // `false`
System.out.println(intToDouble.containsKey(10));  // `false`
intToDouble.remove(10);
System.out.println(intToDouble.containsKey(10));  // `false`

System.out.println(intToDouble.put(1, 999));      //  previous mapped value (`3.14`) is replaced by `999`
intToDouble.destroy();

Traversal of a Session-bound Map

You can traverse a session-bound map, using entries() method to get an iterable of map entries and keys() method to get an iterable of map keys.

intToDouble.entries().forEach {it -> println (it)}
intToDouble.keys().forEach {it -> println (it)}
import java.util.Iterable;
import java.util.stream.Stream;
import oracle.pgx.api.*;
...
Iterable<Map.Entry> entries = intToDouble.entries();
entries.forEach(System.out::println);
Iterable<Map.Entry> keys = intToDouble.keys();
keys.forEach(System.out::println);