### Thread: Using Strings for .get() and .put() with TreeMap

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;
for(int i=0;i<numVertices;i++) {
}//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) {
}//getEdgeWeight

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

//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();

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

//package access
//Returns the adjacency list for given vertex
TreeMap<String, String> getAdjList(String v) {
}//Graph
2. One thing I notice is that in the method

Code:
```public int getEdgeWeight(String v, String w) {
}//getEdgeWeight```
The output of get will be String, but the return type of the method is int.
3. 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).
4. 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.
5. 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.
6. 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.