简体   繁体   中英

HashMap Java Cannot Display Multiple Values

Let me start by saying I have a file. For more info, you check it here .

I have already added the edges on the hashmap with multiple values. I have check it by adding this piece of code:

        map.put(nodes.get(i), edges);
        System.out.println(nodes.get(i) + " " +  map.get(nodes.get(i)));

After adding each entry to map, I check it right away if it was successfully added. Yes, it worked.

Here's the output:

a0 [acodijkstra.Edge@b1c5fa]
a1 [acodijkstra.Edge@13caecd, acodijkstra.Edge@f84386, acodijkstra.Edge@1194a4e]
a2 [acodijkstra.Edge@15d56d5, acodijkstra.Edge@efd552, acodijkstra.Edge@19dfbff]
a3 [acodijkstra.Edge@10b4b2f, acodijkstra.Edge@750159, acodijkstra.Edge@1abab88]

But when creating another method to display the content of the map, I found out that the map is empty. Here's the code:

public void viewFile() {
    for(int i=0; i<nodes.size();i++) {
        System.out.println(nodes.get(i) + " " + this.map.get(nodes.get(i)));
    }
}

The output of the above code is this:

a0 []
a1 []
a2 []
a3 []

What could be the possible reason for this? I am really confused why this happened.

For the code, here's the simplified version (if complied, this will result to errors since I edited some parts deemed unnecessary out):

class Reader {

    HashMap<String, Vertex> vertexList;
    Map<String, ArrayList<Edge>> map;
    ArrayList<String> nodes;
    ArrayList<Edge> edges;


public Reader(String fileName) {
    vertexList = new HashMap<String, Vertex>();
    map = new HashMap<String, ArrayList<Edge>>();
    nodes = new ArrayList<String>();
    edges = new ArrayList<Edge>();
    readFile(fileName);
}

private void readFile(String fileName) {

    try{
        FileReader file = new FileReader(fileName);
        Scanner sc = new Scanner(file);

        int i = 0;
        while (sc.hasNextLine()) {
          input.add(sc.nextLine());
          i++;
        }

        setNodes();
        setVertices();
        System.out.println();
        file.close();
    } catch(Exception e){
        System.out.println(e);
    }
}
public void setNodes() {

    System.out.println();

    for(int i=0; i<input.size(); i++) {
        line = this.input.get(i);
        nodes.add(line.substring(0,line.indexOf("-")).trim());
        adjLine.add(line.substring(line.indexOf("-")+1).trim());
    }
}

private void setVertices() {

    String[] listEdges;

    for(int i=0; i<nodes.size(); i++) {

        //if vertex does not exist, create it
        if(vertexList.containsKey(nodes.get(i))) {
            vertexList.put(nodes.get(i), new Vertex(nodes.get(i)));
        }

        line = adjLine.get(i);

        //separate adj edges to *
        if(line.contains("*")) {
            listEdges = line.split("\\*");
        } else {
            listEdges = new String[1];
            listEdges[0] = line;
        }

        //add edges to specified vertex
        for(int j=0; j < listEdges.length; j++ ) {
            String[] word = listEdges[j].split(",");
            edges.add(new Edge(vertexList.get(word[0]),Double.parseDouble(word[1])));
        }

        map.put(nodes.get(i), edges);
        System.out.println(nodes.get(i) + " " +  map.get(nodes.get(i)));
        edges.clear();
    }
}

Here:

    map.put(nodes.get(i), edges);
    System.out.println(nodes.get(i) + " " +  map.get(nodes.get(i)));
    edges.clear();

you're storing a reference to edges in the map, and then clearing edges . This is why, while the elements are still in the map, they are blank.

You need to stop reusing the same edges object, and create a new one for every vertex (why is edges even a member of your class, and not a local variable?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM