-2

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.

  • so in other words, you don't know how to initialize a static variable in Java? try searching that topic – Patrick Parker Mar 23 '19 at 20:20
  • Possible duplicate of [Java: When is a static initialization block useful?](https://stackoverflow.com/questions/9379426/java-when-is-a-static-initialization-block-useful) – Patrick Parker Mar 23 '19 at 20:27
  • No. I have read that what I am trying to do is to remove static. That my variable should be defined not as static. I have examples of where it once was static in the non main class. That did not fix my problem. – 2_where_we_go Mar 23 '19 at 20:32
  • in that case, your question is too unclear to answer. because the way it is written now, you need a static initializer. – Patrick Parker Mar 23 '19 at 20:38
  • I'll re-read static init and try again. – 2_where_we_go Mar 23 '19 at 20:42

1 Answers1

0

Both classes are once again "static".

This works with or without creating a package cause of the public definition. I created and tested both. Comment out the package name in both classes if you want.

Notice that I have my puts in non-ascending order to prove navigation works for my TreeMap.

Don't be alarmed by the number of answer lines as most are print and comments.

PutDS class (creates and loads TreeMap and its records).

//package twowherewego;

import java.util.TreeMap;

public class PutDS {        // Put DS(Data Storage)

public static TreeMap gettMapCashType(){    System.out.println("PutDS - entered " );
    // Create TreeMap=tMapCashType  and "put" two records to tMapCashType
       TreeMap<String, String> tMapCashType = new TreeMap<String, String>();

                tMapCashType.put("C", "Credit Card");  // out of order puts
                tMapCashType.put("A", "All");

System.out.println("PutDS " + tMapCashType);
                return tMapCashType;
            }
    }

The "main" class that executes PutDS and then puts additional TreeMap records then gets a number of records using TreeMap Navigation keywords:

//package twowherewego;
import java.util.TreeMap;

public class GetDS{         // Get DS(Data Storage)

    public static void main(String[] args) {  
        System.out.println("Works with or without 'towherewego' package cause it is public");
        System.out.println("** GetDS main: call PutDS and...");
        // Execute PutDS with method=gettMapCashType() 
       TreeMap<String, String> people = PutDS.gettMapCashType();   //class=PutDS "." method
       System.out.println("** back into GetDS main: after call to PutDS ");
                                            // space or no space ='s no difference
       people.put("D","popeye");            // notice this "put" has no space                                            
       people.put("B", "Bank Card");        // notice this "put" has a space after the ","

        System.out.println("{{{ Navigational helpful keywords, etc }}}" );
        System.out.println("GetData keySet      " +people.keySet());         // key 
        System.out.println("GetData entrySet    " +people.entrySet());       // key and value 
        System.out.println("GetData 1stEntry    " +people.firstEntry());     // 1st entry/key 
        System.out.println("GetData B_BankCard  " +people.get("B"));         // get B value 
        System.out.println("GetData B_lowerKey  " +people.lowerEntry("B"));  // get B lower
        System.out.println("GetData B_highrKey  " +people.higherEntry("B")); // get B higher    
        System.out.println("GetData lastEntry   " +people.lastEntry());      // last entry/key

           System.out.println("**  << End of GetDS >>");
    }
    }         

Produces/displays a trace and navigational records with their keywords. Notice: the puts were done in descending order but, TreeMap (by default) rearranged them in ascending order (see "GetData keySet, ...entrySet" for examples).

Works with or without 'towherewego' package cause it is public
** GetDS main: call PutDS and...
PutDS - entered 
PutDS {A=All, C=Credit Card}
** back into GetDS main: after call to PutDS 
{{{ Navigational helpful keywords, etc }}}
GetData keySet      [A, B, C, D]
GetData entrySet    [A=All, B=Bank Card, C=Credit Card, D=popeye]
GetData 1stEntry    A=All
GetData B_BankCard  Bank Card
GetData B_lowerKey  A=All
GetData B_highrKey  C=Credit Card
GetData lastEntry   D=popeye
**  << End of GetDS >>