0

I have a Hypergraph construction like below in HGraph class

Map<HEdge, ArrayList<HVertex>> hGraph = new HashMap<HEdge, ArrayList<HVertex>>();

and would like to print all the (key, value) pairs using the below function. But all I am getting this:

Hypergraph:
e4(2.0)[]
e3(2.0)[]
e1(3.0)[]
e2(2.0)[]
e5(2.0)[]

Nothing inside [], not even "#" sign that I used for debugging !! I've also overrided the toString methods in the HEdge and HVertex classes to print out their labels.

public void printHGraph(HGraph hGraph) {
    Set<HEdge> edges = hGraph.getAllHEdges();
    HEdge edge;
    HVertex vertex;
    ArrayList<HVertex> edge_vertices;

    System.out.println("Hypergraph:");

    Iterator<HEdge> edge_iterator = edges.iterator();
    while(edge_iterator.hasNext()) {
        edge = edge_iterator.next();
        System.out.print(edge.toString());

        System.out.print("[");

        edge_vertices = hGraph.getHVertices(edge);
        Iterator<HVertex> vertex_iterator = edge_vertices.iterator();
        while(vertex_iterator.hasNext()) {
            System.out.print("#"); //@debug
            vertex = vertex_iterator.next();
            System.out.print(vertex.toString()+", ");
        }           

        System.out.print("]");
        System.out.println();
    }
Joarder Kamal
  • 1,387
  • 1
  • 20
  • 28
  • What kind of output you want ??? – MayurB Jul 21 '13 at 13:35
  • Have you overridden `equals` and `hashcode` method in your `HEdge` class? – Rohit Jain Jul 21 '13 at 13:36
  • for example: e1(3.0)[v1, v2, v3] – Joarder Kamal Jul 21 '13 at 13:36
  • @RohitJain, exactly how should I override these two methods? any example kindly? – Joarder Kamal Jul 21 '13 at 13:37
  • It doesn't explain why your code doesn't work but I'd using foreach instead of manual work with iterators – RiaD Jul 21 '13 at 13:38
  • however the while() is working fine with the edge_iterator but not with the vertex_iterator !! I will try with foreach() – Joarder Kamal Jul 21 '13 at 13:40
  • 2
    @JoarderKamal. It won't work with `for each` either. Just try using `hGraph.containsKey(edge)`, you will see that it returns false. You need to override both `equals` and `hashcode` method in your class to make it work correctly as a `HashMap` key. – Rohit Jain Jul 21 '13 at 13:42
  • @JoarderKamal. See [this question for in depth explanation](http://stackoverflow.com/q/27581/1679863) – Rohit Jain Jul 21 '13 at 13:42
  • unfortunately i can't get any of the usual Map functions while typing hGraph. like containsKey or entrySet or anything like this. I am not sure why!! – Joarder Kamal Jul 21 '13 at 13:46
  • While the likely hashcode()/equals() issue needs to be still solved, I'd recommend iterating using hgraph.entrySet() to avoid the unnecessary hash table lookups – kiheru Jul 21 '13 at 13:46
  • Your `hGraph` is of type `HGraph`, so you won't have the map methods unless `HGraph` implements `Map` – kiheru Jul 21 '13 at 13:49
  • The output you get simply means that there's nothing in the lists (values of the map). The problem is in code you don't show: getting the values from the map, or populating the map. – JB Nizet Jul 21 '13 at 13:50
  • I've already checked and verified that the values are set in the HVertex objects correctly by calling the get() method just after using the set() method. I am certain that they are not empty !! – Joarder Kamal Jul 21 '13 at 13:54
  • @kiheru I've generated the overridden codes for hashcode() and equals() using Eclipse Source>Generate method. But still seeing no changes !! How could i able to get the hGraph.entrySet()? I was passing the reference of HGraph type object in the print function, the actual object was created while taking the inputs. – Joarder Kamal Jul 21 '13 at 13:57
  • 2
    @JoarderKamal: if they were not empty, they would be printed as non-empty. What you're printing when calling get() is probably a different list than the one you print in your question. Or it has been cleared in the meantime. Show the code filling the map, and show the code of the methods used in the posted code that get the keys and values from the map. – JB Nizet Jul 21 '13 at 14:04
  • @JoarderKamal To get the entry set, you could add a `getEntries()` method to `HGraph`. – kiheru Jul 21 '13 at 14:07
  • @JBNizet thanks a lot for pointing this out. I've carefully checked my input function and found that I accidentally clear() a vertices list few lines after adding the elements. And that is where the actual problem was. And there was no need to override the hasCode() and equals() in this particular case. Many thanks again everyone. – Joarder Kamal Jul 21 '13 at 15:24

0 Answers0