Class GraphCollector


  • public final class GraphCollector
    extends Object

    Final Class, which provides a static function, that provides a collector to create a graph

    Since:
    1.0
    Author:
    Andreas Pointner
    • Method Detail

      • toGraph

        public static <V,​E> Collector<Vertex<V,​E>,​?,​Graph<V,​E>> toGraph​(BiPredicate<Vertex<V,​E>,​Vertex<V,​E>> predicate)

        Creates a collector, that allows to collect a stream of vertices and convert it into a graph. The given predicate decides, if an edge is created. the edge is created directional.


        Usage example:
        
           Stream<Vertex<V>> someStream = ...;
           Graph<V> myGraph = someStream.collect(GraphCollector.toGraph((v1, v2) -> {
              // Some check if a edge from v1 to v2 should be created
              return true / false;
           });
         

        Another use case is to transform a graph into another graph
        
           Graph<V>; myFirstGraph = ...;
           Graph<S>; myResult Graph = myFirstGraph
             .stream()
             .map(...) // Some mapping
             .xyz(...) // Some other streaming operations.
             .collect(GraphCollector.toGraph(myBiPredicate));
         
        Type Parameters:
        V - the type of the decorated vertex value
        E - the type of the decorated edge value
        Parameters:
        predicate - test if an edge from first element to the second element should be created
        Returns:
        a collector, that can be used in streams.
      • toSubGraph

        public static <V,​E> Collector<Vertex<V,​E>,​?,​Graph<V,​E>> toSubGraph()

        Creates a subgraph for a given set of vertices, where the edge between the vertices in the stream are used.

        Example:

        Given Graph:
                   (C)
                    ↑
             (A) → (B)
              ↑     ↓
            (BC) ← (AB)
         
        Selected Vertices: [A, B, C] Resulting Graph:
                  (C)
                   ↑
            (A) → (B)
         

        So all edges, that leads to a vertex, that is again in the list of vertices, will be added, all other edges are removed.

        Usage example (for the above example):

        
            Graph<String, Void> graph = GraphBuilderImpl.<String, Void>create()
                           .from("A").to("B")
                           .from("B").to("C")
                           .from("B").to("AB")
                           .from("AB").to("BC")
                           .from("BC").to("A")
                        .toGraph();
        
            Graph<String, Void> collect = graph.stream()
                              .filter(x -> x.getElement().length() == 1)
                              .collect(GraphCollector.toSubGraph());
         
        Type Parameters:
        V - the type of the decorated vertex value
        E - the type of the decorated edge value
        Returns:
        A collector which allows collecting for a vertex stream