Luis Afonso Teixeira
Luis Afonso Teixeira

Reputation: 211

How to pass and get values to TreeMap on another class?

I am trying to simulate a production system and now I am having trouble getting and passing values to a TreeMap located on a different class.

To explain what I intend to do briefly, I will to create a Panel where I will have some textpanels to save values (for the number of parts to be added to the system) and a table where the number and parameters of the work stations on the system will be set. When I run it, those values should be stored for further processing.

On a previous question I was recommended using TreeMaps to store those values, something like:

Station[num][type][avg_time][posx][posy][state]
Part[num][type][state]

This is what I've coded so far:

L.java

import java.awt.*;
import javax.swing.*;    

public class L extends JFrame {

    public static void main(String[] args) {


      SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            L l = new L();

            TMap t = new TMap();
            t.Station("num", 127);
            t.Station("type", 3);
            //System.out.println("Entryset: " + t.keySet());
            //System.out.println("Entryset: " + t.Station() + "\n");
         }
      });

    }

 } 

TMap.java

import java.util.*;

public class TMap {
    //public TreeMap <String, Integer>St = new TreeMap<String, Integer>();
    public int num_atrib = 6;
    public static TreeMap<String, Integer> Station(String s,int i) {
        TreeMap <String, Integer>St = new TreeMap<String, Integer>();
        St.put(s,i);
        System.out.println("Now the tree map Keys: " + St.keySet());
        System.out.println("Now the tree map contain: " + St.values());
        return St;
    }
} 

This is outputing:

Now the tree map Keys: [num]
Now the tree map contain: [127]
Now the tree map Keys: [type]
Now the tree map contain: [3]

I have two problems, first, is this the right way to do it, because for what I see the map outputed should be [num, type] and the keys [127, 3] right?

And secondly, how can I later on get parameters from TMap on the L class, since t.keySet() for instance won't retrieve anything so far!

Hope I made myself clear, thanks in advance for your help.

Upvotes: 1

Views: 3108

Answers (1)

Dennis Meng
Dennis Meng

Reputation: 5187

First off, you're creating a new TreeMap every time you call TMap.Station. Try putting the TreeMap as a field and initializing it in a constructor instead. That should get you the map with two key/value pairs.

Answering your second question, is there any reason why you cannot make the TMap a field and just create methods to access and set? If you only instantiate it in a function, it'll disappear as soon as that function exits (plus its scope would only be in that function).

EDIT: In response to the comment...what about

EDIT EDIT: Adding rough outlines for getters. If you wanted something like a put(), it would work in a similar manner.

import java.awt.*;
import javax.swing.*;
import java.util.Set;

public class L extends JFrame {
    private TMap t;

    public L() {
        t = new TMap();
    }

    public Set<String> getKeySet() {
        return t.getKeySet();
    }

    public Integer get(String s) {
        return t.get(s);
    }

    // your main method as before
}

and

import java.util.*;

public class TMap {
    private TreeMap<String, Integer> St;
    private int num_atrib = 6;

    public TMap() {
        St = new TreeMap<String, Integer>();
    }

    public Set<String> getKeySet() {
        return St.keySet();
    }

    public Integer get(String s) {
        return St.get(s);
    }

    public static TreeMap<String, Integer> Station(String s,int i) {
        St.put(s,i);
        System.out.println("Now the tree map Keys: " + St.keySet());
        System.out.println("Now the tree map contain: " + St.values());
        return St;
    }
}

Upvotes: 1

Related Questions