VertexKey compositeVertexKey = null; for (Iterator<VertexType> i = vertexSet.iterator(); i.hasNext(); ) { VertexType v = i.next(); if (compositeVertexKey == null) compositeVertexKey = toolkit.duplicateVertexKey(v.getVertexKey()); else compositeVertexKey = combinator.combineVertexKey(compositeVertexKey, v.getVertexKey()); }   VertexType compositeVertex = g.addVertex(compositeVertexKey, toolkit);
VertexType compositeVertex = combinator.combineVertices(vertexSet);
	public void mergeVertices(Set<VertexType> vertexSet, GraphType g, VertexKeyCombinator<VertexKey> combinator,							   GraphToolkit<GraphType, EdgeKey, VertexKey, EdgeType, VertexType> toolkit) {		// Special case: if the vertex set contains a single vertex		// or is empty, there is nothing to do		if (vertexSet.size() <= 1)			return;		// Get all vertices to which we have outgoing edges		// or from which we have incoming edges, since they'll need		// to be fixed		TreeSet<EdgeType> edgeSet = new TreeSet<EdgeType>();		for (Iterator<EdgeType> i = g.getEdgeIterator(); i.hasNext(); ) {			EdgeType e = i.next();			if (vertexSet.contains(e.getSource()) || vertexSet.contains(e.getTarget()))				edgeSet.add(e);		}		// Combine all of the VertexKey objects into a single composite object		VertexKey compositeVertexKey = null;		for (Iterator<VertexType> i = vertexSet.iterator(); i.hasNext(); ) {			VertexType v = i.next();			if (compositeVertexKey == null)				compositeVertexKey = toolkit.duplicateVertexKey(v.getVertexKey());			else				compositeVertexKey = combinator.combineVertexKey(compositeVertexKey, v.getVertexKey());		}		// Create the new composite vertex		VertexType compositeVertex = g.addVertex(compositeVertexKey, toolkit);		// For each original edge into or out of the vertex set,		// create an equivalent edge referencing the composite		// vertex		for (Iterator<EdgeType> i = edgeSet.iterator(); i.hasNext(); ) {			EdgeType e = i.next();			VertexType source = vertexSet.contains(e.getSource()) ? compositeVertex : e.getSource();			VertexType target = vertexSet.contains(e.getTarget()) ? compositeVertex : e.getTarget();//			  if (source != compositeVertex && target != compositeVertex)//				  System.out.println("BIG OOPS!");			// Don't create a self edge for the composite vertex			// unless one of the vertices in the vertex set			// had a self edge			if (source == compositeVertex && target == compositeVertex &&				 e.getSource() != e.getTarget())				continue;			g.addEdge(toolkit.duplicateVertexKey(source.getVertexKey()),					   toolkit.duplicateVertexKey(target.getVertexKey()),					   toolkit.duplicateEdgeKey(e.getEdgeKey()),					   toolkit);		}		// Remove all of the vertices in the vertex set; this will		// automatically remove the edges into and out of those		// vertices		for (Iterator<VertexType> i = vertexSet.iterator(); i.hasNext(); ) {			g.removeVertex(i.next());		}	}