15.2.1.1.2 Session-Bound Collections
You can create and manipulate collections directly in the session without the need for a graph. Session-bound collections can be further passed as parameters to graph algorithms or used like any other collection object. The following sub-sections describe the currently supported types for these collections.
Scalar Collections
Scalar collections contain simple data types like Integer
, Long
, Float
, Double
and Boolean
. They can be managed by the PgxSession
APIs:
Creation of a Scalar Collection
You can use createSet()
and createSequence()
methods to create a scalar collection as shown in the following code:
myIntSet = session.createSet(PropertyType.INTEGER, "myIntSet") myDoubleSequence = session.createSequence(PropertyType.DOUBLE) // A name will be automatically generated if none is provided. println myDoubleSequence.getName() // Display the generated name.
import oracle.pgx.api.*; import oracle.pgx.common.types.*; ... ScalarSet myIntSet = session.createSet(PropertyType.INTEGER, "myIntSet"); ScalarSequence myDoubleSequence = session.createSequence(PropertyType.DOUBLE); System.out.println(myDoubleSequence.getName());
Run Operations on a Scalar Collection
You can run several operations on a scalar collection as shown in the following code:
myIntSet.add(10) myIntSet.addAll([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) myIntSet.addAll([0,1,2]) // Element uniqueness. This operation has no effect on the set. println myIntSet myIntSet.contains(1) // Checks the presence of an element. This code returns `true`. myIntSet.remove(10) myIntSet.removeAll([4, 5, 6, 7, 8, 9]) // Leaves only elements `0, 1, 2, 3`. println myIntSet
import java.util.Arrays; import oracle.pgx.api.*; ... myIntSet.add(10); myIntSet.addAll(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); myIntSet.addAll(Arrays.asList(0, 1, 2)) myIntSet.contains(1); // Returns `true`. myIntSet.remove(10); myIntSet.removeAll(Arrays.asList(4, 5, 6, 7, 8, 9));
Traversal of a Scalar Collection
You can traverse a scalar collection either using an iterator or using the new Stream API. You can add elements of a sequence to a set, traverse a sequence and filter out elements not required, and then add the rest to another scalar collection.
myIntSet.forEach({x -> print x + "\n"}) myIntSet.stream().filter({x -> x % 2 == 0}).forEach({x -> myDoubleSequence.add(x)}) println myDoubleSequence
import java.util.Iterator; import java.util.stream.Stream; import oracle.pgx.api.*; ... myIntSet.forEach(x -> System.out.println(x)) myIntSet.stream().filter(x -> x % 2 == 0).forEach(myDoubleSequence::add)
Parent topic: Collection Data Types