I know your question was about Java, but the Gremlin Groovy sessions below demonstrate the approach. One way would be to use the dedup
step.
http://gremlindocs.com/#filter/dedup
That will ensure that U1
exists only once after that step as shown here:
gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]
gremlin> g.V.out.in
==>v[4]
==>v[6]
==>v[1]
==>v[1]
==>v[1]
==>v[4]
==>v[6]
==>v[1]
==>v[4]
==>v[4]
==>v[6]
==>v[1]
gremlin> g.V.out.in.dedup
==>v[4]
==>v[6]
==>v[1]
Alternatively you could write all users to a Set
:
gremlin> x = [] as Set
gremlin> g.V.out.in.fill(x);null
==>null
gremlin> x
==>v[4]
==>v[6]
==>v[1]