Graph Implementation in Java using Set and Map There is a Graph implementation. I'm using Set to store unique Edges and Map to store Vertices and their associated edges. Assume Vertex and Edge class has been defined somewhere else implementing hashcode() and equals() working fine. 

public class Graph
{
    static class Vertex {}

    static class Edge {}

    private Map&lt;Vertex, Set&lt;Edge&gt;&gt; vertexSetMap;
    private Set&lt;Edge&gt; edgeSet;

    public Graph()
    {
        edgeSet = new LinkedHashSet&lt;&gt;();
        vertexSetMap = new LinkedHashMap&lt;&gt;();
    }

    public boolean addVertex(Vertex vertex)
    {
        if(!vertexSetMap.containsKey(vertex))
        {
            vertexSetMap.put(vertex, new LinkedHashSet &lt;&gt;());
            return true;
        }
        return false;
    }

    public boolean addEdge(Vertex v1, Vertex v2, Edge edge)
    {
        if(edgeSet.add(edge))
        {
            vertexSetMap.get(v1).add(edge);
            vertexSetMap.get(v2).add(edge);
            return true;
        }
        return false;
    }

    public Set&lt;Edge&gt; getEdges(Vertex vertex)
    {
        return vertexSetMap.get(vertex);
    }

    public Set&lt;Vertex&gt; getNeighborsOf(Vertex vertex)
    {
        Set&lt;Vertex&gt; neighbors = new LinkedHashSet &lt;&gt;();
        for(Edge edge : vertexSetMap.get(vertex))
        {
            Vertex v1 = edge.getVertex1();
            Vertex v2 = edge.getVertex2();
            if(v1.equals(vertex)) neighbors.add(v2);
            else neighbors.add(v1);
        }
        return neighbors;
    }

    public void removeVertex(Vertex vertex)
    {
        Set&lt;Edge&gt; deleteEdges = getEdges(vertex);
        vertexSetMap.remove(vertex);
        edgeSet.removeAll(deleteEdges);
        vertexSetMap.keySet().forEach(vertex1 -&gt; 
        vertexSetMap.get(vertex1).removeAll(deleteEdges));
    }

    public void removeEdge(Edge edge)
    {
        edgeSet.remove(edge);
        Vertex v1 = edge.getVertex1();
        Vertex v2 = edge.getVertex2();
        vertexSetMap.get(v1).remove(edge);
        vertexSetMap.get(v2).remove(edge);
    }

    public boolean containsVertex(Vertex vertex)
    {
        return vertexSetMap.containsKey(vertex);
    }

    public boolean containsEdge(Vertex v1, Vertex v2, Edge edge)
    {
        return (edgeSet.contains(edge) &amp;&amp; vertexSetMap.get(v1).contains(edge)
            &amp;&amp; vertexSetMap.get(v2).contains(edge));
    }

    public boolean containsEdge(Vertex v1, Vertex v2)
    {
        for(Edge e : vertexSetMap.get(v1)) 
            if(vertexSetMap.get(v2).contains(e)) return true;
        return false;
    }

    public int totalVertices()
    {
        return vertexSetMap.keySet().size();
    }

    public int totalEdges()
    {
        return edgeSet.size();
    }

    public Set&lt;Vertex&gt; getAllVertices()
    {
        return new LinkedHashSet &lt;&gt;(vertexSetMap.keySet());
    }

    public Set&lt;Edge&gt; getAllEdges()
    {
        return edgeSet;
    }

    public void clear()
    {
        edgeSet = null;
        edgeSet = new LinkedHashSet &lt;&gt;();
        vertexSetMap = null;
        vertexSetMap = new LinkedHashMap &lt;&gt;();
    }
}
As of now, you only use LinkedHashSet and LinkedHashMap, thus declaring edgeSet and vertexMap as HashSet and HashMap is pointless. You could deploy Inversion of Control in order to demand some Set and Map at construction.



Method getEdges(...) could possibly return null, so you may want to wrap the return value in an Optional. Using Optional, you can also unify the behaviour of Map#get(Object) (which may or may not throw a NPE if a null is passed as argument):

public Optional&lt;Set&lt;Edge&gt;&gt; getEdges(Vertex vertex)
{
    Optional&lt;Set&lt;Edge&gt;&gt; result = Optional.empty();
    try 
    {
        result = Optional.ofNullable(vertex);
    }
    catch (NullPointerException e)
    {
        // No action needed, we return an empty Optional
    }
    return result;
}




Your remove...(...)-methods throw a NPE if the Vertex/Edge provided is not contained within the graph or is null. As with Map#get(Object), Set#remove(Object) may or may not throw a NPE. You can rewrite them to not throw:

public void removeVertex(Vertex vertex)
{
    Optional&lt;Set&lt;Edge&gt;&gt; deleteEdges = getEdges(vertex);
    if (deleteEdges.isPresent())
    {
        vertexSetMap.remove(vertex);
        edgeSet.removeAll(deleteEdges.get());
        vertexSetMap.keySet().forEach(vertex1 -&gt;
                vertexSetMap.get(vertex1).removeAll(deleteEdges.get()));
    }
}

public void removeEdge(Edge edge)
{
    try
    {
        if (edgeSet.remove(edge))
        {
            Vertex v1 = edge.getVertex1();
            Vertex v2 = edge.getVertex2();
            vertexSetMap.get(v1).remove(edge);
            vertexSetMap.get(v2).remove(edge);
        }
    }
    catch (NullPoionterException e)
    {
        // No action needed, there is nothing to remove.
    }
}




When you add methods

public boolean containsVertex(Vertex vertex)
public boolean getOther(Vertex vertex)


To your Edge-class, you can rewrite getNeighborsOf(...) to use Streams

public Set&lt;Vertex&gt; getNeighborsOf(Vertex vertex) {
    return vertexSetMap.get(vertex).stream()
          .filter(edge -&gt; edge.contains(vertex))
          .map(edge -&gt; edge.getOther(vertex))
          .collect(Collectors.toSet());
}




What is missing is a Set&lt;Vertex&gt; getVertices() method (you have one for Edges, so I would expect one for Vertexs as well). This would probably require creating an additional field Set&lt;Vertex&gt; vertices in your Graph class.