26.2.1.2.2 セッションバインド・マップ
セッション内でマップを直接作成できます。ただし、グラフ関連のデータ型をマップ・キーまたは値の型として使用することはできません。セッションバインド・マップは、さらに、グラフ・アルゴリズムにパラメータとして渡すことや、他のマップ・オブジェクトと同様に使用することができます。これらは、PgxSession
およびPgxMaps
APIによって管理されます。
スカラー・コレクションには、Integer
、Long
、Float
、Double
、Boolean
などの単純なデータ型が含まれます。これらは、PgxSession
APIによって管理できます。
セッションバインド・マップの作成
createMap()
メソッドとそのオーバーロードを使用して、セッションバインド・マップを作成できます。
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());
セッションバインド・マップでの操作の実行
次のコードに示すように、エントリの設定や削除、エントリの存在のチェックなど、重要な操作をセッションバインド・マップで実行できます。
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();
セッションバインド・マップのトラバース
entries()
メソッドを使用してマップ・エントリのIterableを取得し、keys()
メソッドを使用してマップ・キーのIterableを取得することにより、セッションバインド・マップをトラバースできます。
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);
親トピック: マップ・データ型