使用hashmap表示加权图

时间:2015-01-28 23:18:38

标签: java hashmap

我的输入文字文件:

  122334 
  45   
  67

输出应为:

0-{1=2,2=3,3=4} //node 0 is connected to 1 with weight 2,to 2 with weight 3,etc

1-{4=5}

2-{6=7}

以下是我的计划:

public class BFS {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        HashMap<Integer, HashMap<Integer, Integer>>hash1=new HashMap<Integer,     HashMap<Integer,Integer>>();
        HashMap<Integer, Integer>hash2=new HashMap<Integer, Integer>();
        HashMap<Integer, Integer>temp1=new HashMap<Integer, Integer>();
    BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Translab\\workspace\\Algorithms\\inputfile.txt"));
        int remainder,remainder2,j=0;
        int line=1;
        String str;
        while ((str =in.readLine()) != null) {
            int foo = Integer.parseInt(str);
          while(foo>0){
                 remainder=foo % 10;
                   foo = foo / 10;
                 remainder2=foo%10;
                 foo=foo/10;
                 temp1.put(remainder2, remainder);
                 hash2.putAll(temp1);

        }
        temp1.clear();
        hash1.put(j, hash2);

        j++;


        }
        for (Entry<Integer, HashMap<Integer, Integer>> entry  : hash1.entrySet()) {
            System.out.println(entry.getKey()+"-"+entry.getValue() );

        }
        in.close();
    }
}

有了这个:

0-{1=2, 2=3, 3=4, 4=5, 6=7}

1-{1=2, 2=3, 3=4, 4=5, 6=7}

2-{1=2, 2=3, 3=4, 4=5, 6=7}

有人可以帮助我 - 我是hashmap的新手。

2 个答案:

答案 0 :(得分:3)

您不必要地重载了Map和Integer类。更好的方法是将图形节点和边缘封装在类中 - 一旦开始对图形执行操作,这将使事情变得更加容易:

class Node {
    private int id;
    private List<Edge> edges;
    public boolean hasId(int id) {
        return id == this.id;
    }
    public addEdge(Node node, int weight) {
        edges.add(new Edge(node, weight));
    }
}

class Edge {
    private int weight;
    private Node destination;
}

class Graph {
    private List<Node> nodes;
    public Node getNodeById(int id) {
        return nodes.stream()
            .filter(node -> node.hasId(id))
            .findFirst().orElse(null);
    }
}

我还建议您在转换为id和weight之前将输入行分为标记。这可以在Graph类中完成:

public void addEdge(int fromNodeID, String edgeData) {
    Node node = getNodeByID(fromNodeID);
    for (int pos = 0; pos < edgeData.length(); pos += 2) {
        int destNodeID = Integer.parseInt(edgeData.substring(pos, pos+1));
        int weight = Integer.parseInt(edgeData.substring(pos + 1, pos + 2);
        node.addEdge(getNodeByID(destNodeID), weight);
    }
}

然后处理输入相当简单:

int id = 0;
Map<Integer, String> edgeData = new HashMap<>();
while((line = in.readLine()) != null) {
    graph.addNode(new Node(id));
    put(id, line);
}
edgeData.entrySet().stream()
    .forEach(entry -> graph.addEdge(entry.getKey(), entry.getValue());

我遗漏了很多错误检查,简单的构造函数等等。但希望你能得到这个想法。

答案 1 :(得分:0)

这里我假设你的解析逻辑是正确的,这就是你想要的。 您需要在第一个hash2循环中初始化while,而您根本不需要temp1

int j=0;    
while ((str =in.readLine()) != null) {
            int foo = Integer.parseInt(str);
          HashMap<Integer, Integer> hash2=new HashMap<Integer, Integer>();
          while(foo>0){
                 remainder=foo % 10;
                   foo = foo / 10;
                 remainder2=foo%10;
                 foo=foo/10;
                 hash2.put(remainder2, remainder);
          }
          hash1.put(j, hash2);
          j++;
    }