#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Using Strings for .get() and .put() with TreeMap


    I am currently working on creating a Prim's Minimum Spanning Tree for an undirected, weighted graph that uses String values for the vertices. To create the graph, my teacher said we can use the edge and graph classes from our textbook. However, the book uses Integers for vertices rather than strings. I tried replacing all Integers with Strings, but I received a compiler error for every line that used .get() from the generic TreeMap because it could not find the symbol method get(java.lang.String). After a little work, I discovered that initializing the TreeMap and using .add() works with Strings, but not the .get() or .put() methods. Here is the code exactly the way it is in the book except Integer is replaced with String. How can I make the .get() and .put() methods work with Strings.

    import java.util.*;

    class Graph {
    private int numVertices; //number of vertices in the graph
    private int numEdges; //number of edges in the graph

    private Vector<TreeMap<String, String>> adjList;

    //constructor
    public Graph(int n) {
    numVertices=n;
    numEdges=0;
    adjList=new Vector<TreeMap<String, String>>();
    for(int i=0;i<numVertices;i++) {
    adjList.add(new TreeMap<String, String>());
    }//for
    }//constructor

    //Determines the number of vertices in the graph
    public int getNumVertices() {
    return numVertices;
    }//getNumVertices

    //Determines the number of edges in the graph
    public int getNumEdges() {
    return numEdges;
    }//getNumEdges

    //Determines the weight of the edge between vertices v and w
    public int getEdgeWeight(String v, String w) {
    return adjList.get(v).get(w);
    }//getEdgeWeight

    //Add the edge to both v's and w's adjacency list
    public void addEdge(String v, String w, int wgt) {
    adjList.get(v).put(w,wgt);
    adjList.get(w).put(v,wgt);
    numEdges++;
    }//addEdge

    //Adds an edge to the graph
    public void addEdge(Edge e) {
    //Extract the vertices and weight from the edge e
    String v=e.getV();
    String w=e.getW();
    int weight=e.getWeight();
    addEdge(v, w, weight);
    }//addEdge

    //Finds the edge connecting v and w
    public Edge findEdge(String v,String w) {
    int wgt=adjList.get(v).get(w);
    return new Edge(v, w, wgt);
    }//findEdge

    //package access
    //Returns the adjacency list for given vertex
    TreeMap<String, String> getAdjList(String v) {
    return adjList.get(v);
    }//getAdjList
    }//Graph
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    One thing I notice is that in the method

    Code:
    public int getEdgeWeight(String v, String w) {
    return adjList.get(v).get(w);
    }//getEdgeWeight
    The output of get will be String, but the return type of the method is int.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Your right, I should have changed that when I changed Integers to Strings. Unfortunately, making the return type String still produced the same error, cannot find symbol method get(java.lang.String).
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Originally Posted by CodeCrunch
    Your right, I should have changed that when I changed Integers to Strings. Unfortunately, making the return type String still produced the same error, cannot find symbol method get(java.lang.String).
    For that problem, remember that adjList is a Vector that contains a TreeMap. The get method won't accept a String parameter.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    So is there not a way to obtain the same functionality with get() and put() using Strings without completely recreating my approach.
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Originally Posted by CodeCrunch
    So is there not a way to obtain the same functionality with get() and put() using Strings without completely recreating my approach.
    Instead of a Vector containing a TreeMap, you might consider a TreeMap where the key is a String, and the value is a TreeMap that maps Strings to Strings.

IMN logo majestic logo threadwatch logo seochat tools logo