Class GraphBuilder<N>

  • Type Parameters:
    N - The most general node type this builder will support. This is normally Object unless it is constrained by using a method like nodeOrder(com.google.common.graph.ElementOrder<N1>), or the builder is constructed based on an existing Graph using from(Graph).

    @Beta
    @DoNotMock
    public final class GraphBuilder<N>
    extends java.lang.Object
    A builder for constructing instances of MutableGraph or ImmutableGraph with user-defined properties.

    A Graph built by this class has the following default properties:

    • does not allow self-loops
    • orders Graph.nodes() in the order in which the elements were added (insertion order)

    Graphs built by this class also guarantee that each collection-returning accessor returns a (live) unmodifiable view; see the external documentation for details.

    Examples of use:

    
     // Building a mutable graph
     MutableGraph<String> graph = GraphBuilder.undirected().allowsSelfLoops(true).build();
     graph.putEdge("bread", "bread");
     graph.putEdge("chocolate", "peanut butter");
     graph.putEdge("peanut butter", "jelly");
    
     // Building an immutable graph
     ImmutableGraph<String> immutableGraph =
         GraphBuilder.undirected()
             .allowsSelfLoops(true)
             .<String>immutable()
             .putEdge("bread", "bread")
             .putEdge("chocolate", "peanut butter")
             .putEdge("peanut butter", "jelly")
             .build();
     
    Since:
    20.0
    Author:
    James Sexton, Joshua O'Madadhain