Compile errors are resolved by "new" TMap definition, NOT, the possible duplication reference. I tried to make that clear. I am a Java Newbee and this is my first post.
Problem is, as it was then, cannot access my TMap from different classes.
Having said that, my Puts are lost because of the subsequent Get with "new". I cannot seem to access the TMap without re-creating it over and over. Sequence of events: Define/init TMap=>Put/Get to TMap=>Get TMap with "new".
I found some similar and good ideas within Stackoverflow question: "Accessing a HashMap from a different class".
Create and Init my treemap "DataStorage" and Put/Get "PutDataStorage" classes works fine (successfully loaded and able to get those records).
I try a Get with "new" DataStorage(because I have not yet figured how to access the table otherwise) and fail cause I created a new Tmap.
I am missing something from those examples. Maybe a complete example would help me instead of pieces. I like the pieces concepts but, am too new to appreciate the individual pieces at this time and have spent way too much time on this already (off and on for a couple of weeks).
Yes, I am that new ;). I'll fix it at some point, somehow.
Summary:
Ideally what I am trying to accomplish is create and load my TreeMap in one class. Access that same TreeMap from one or more separate / additional / other classes.
What I have now: Lost initial loaded data and map. Inability to access my Treemap from a different class.
In the meantime, I am still trying different ideas, play with packages, etc.
Define/Init Treemap: DataStorage
import java.util.TreeMap;
public class DataStorage {
public static TreeMap<String, Integer> people = new TreeMap<String, Integer>();
}
And Put: PutDataStorage
import java.util.TreeMap;
public class PutDataStorage {
/**
* Run one time "put"
*/
public static void main(String[] args) {
DataStorage storage = new DataStorage();
TreeMap<String, Integer> people = storage.people;
people.put("bob", 2);
people.put("susan", 5);
System.out.println("PutData whole Entry " +people.entrySet());
System.out.println("PutData First Entry " +people.firstEntry());
System.out.println("PutData susan Value " +people.get("susan"));
}
}
Get Treemap: GetDataStorage Wipes out my previous put records cause I have the "new" in there. The only record I have coming out of GetDataStorage is the solo popeye=3 record. I need to find a way to access the TMap without having to recreate it over and over.
import java.util.TreeMap;
public class GetDataStorage {
public static void main(String[] args) { //System.out.println("main");
DataStorage storage = new DataStorage();
TreeMap<String, Integer> people = storage.people;
// TreeMap<String, Integer> people = new TreeMap<String, Integer>();
people.get("bob");
people.get("susan");
people.put("popeye", 3);
System.out.println("GetData 1stEntry " +people.firstEntry());
System.out.println("GetData bobValue " +people.get("bob"));
System.out.println("GetData popValue " +people.get("popeye"));
System.out.println("GetData lowerKey " +people.lowerEntry("popeye"));
System.out.println("GetData highKey " +people.higherEntry("popeye"));
}
}
Output: From Put
PutData whole Entry [bob=2, susan=5]
PutData First Entry bob=2
PutData susan Value 5
Output: From Get
GetData 1stEntry popeye=3
GetData bobValue null
GetData popValue 3
GetData lowerKey popeye=3
GetData hireKey popeye=3
Expected Get results should be:
GetData 1stEntry bob=2
GetData bobValue 2
GetData popValue 3
GetData lowerKey bob=2
GetData highKey susan=5
Thanking you in Advance.