How to extract the properties of a class instance from a LinkedHashMap value

40 Views Asked by At

I am using a LinkedHashMap for the first time.

My LinkedHashMap is defined to contain a key String and a class value.

I defined the following classes:

// --- Geometric Element Class ---------
    class GeoElement
    {
        String elemType = "Generic";         // Element type
    }

// --- Point Element Class ---------
    class PointElement extends GeoElement
        {
            final String elemType = "Point";
            String pointName = "";                                // Point name
            double  pointCoord[] = {0.0, 0.0, 0.0};        // Point coordinates

            public PointElement(String pName, double px, double py, double pz)     // Overloaded Constructor
            {
                this.pointName = pName;
                this.pointCoord[0] = px;
                this.pointCoord[1] = py;
                this.pointCoord[2] = pz;
            }
        }   

// --- Line Element Class ---------
         class LineElement extends GeoElement
        {
            final String elemType = "Line";
            
            String lineName = "";                   // Line name
            String startPoint = "";                     // Start-Point or Anchor-Point
            String endPoint = "";                       // End-Point used only for Two-Point lines
            String angle = "";                              // Angle degrees
            String oldLineName = "";                // Base-Line name   
            String startPointMethod = "";       // Start-Point method
            String startParam = "";                  // Start-Parameter
            String endPointMethod = "";        // end-Point method
            String endParam = "";                   // End-Parameter
            String groupName = "";                 // Group name

// --------------------------------------------- Overloaded Constructor --------------------------------------------------------------------------------------------------
            LineElement(String lName, String strPoint, String endPoint, String ang, String bLine, 
                                            String strPtMet, String strPar, String endPtMet, String endPar, String gName )  
            {
                this.lineName = lName;
                this.startPoint = strPoint;    
                this.endPoint = endPoint;
                this.angle = ang;  
                this.oldLineName = bLine;
                this.startPointMethod = strPtMet;  
                this.startParam = strPar;   
                this.endPointMethod = endPtMet; 
                this.endParam = endPar;
                this.groupName = gName;                       
            }
        }   

My LinkedHashMap is defined as follows:

   public static  LinkedHashMap<String, GeoElement> geoElemMap = new LinkedHashMap<String, GeoElement>();     // LinkedHashMap for data transfer to/from GUI and Cliff's code

An item added to geoElemMap can be a <point-name,point-coordinates> or it can be a <line-name, line-properties>. For debugging purposes I use the following method to print out the content of my geoElemMap any time it is accessed:

    public void linkedHashMapTest (LinkedHashMap<String, MyNewGUI.GeoElement> linkedHashMap)
    {
        System.out.print("\n =================================================== \n ");
         for (Entry<String, MyNewGUI.GeoElement> entry : linkedHashMap.entrySet()) 
        {
            System.out.println("Key: " + entry.getKey()
                               + ", Value: "
                               + entry.getValue().toString());
        }
        System.out.print(" =================================================== \n "); 
// Create List  that store values 
        List<GeoElement> list = new ArrayList< GeoElement>(linkedHashMap.values()); 
// display List 
        System.out.println("List  - " + list); 
    }

Unluckily I haven't been able to unfold the content of geoElemMap value portion. In the following there is an example of geoElemMap print out:

*=================================================== Key: ZERO, Value: MyNewGUI$PointElement@5ce1e0d4 Key: ONE, Value: MyNewGUI$PointElement@59dd4912 Key: TWO, Value: MyNewGUI$PointElement@2bd30507 Key: THREE, Value: MyNewGUI$PointElement@76979715 Key: FOUR, Value: MyNewGUI$PointElement@25a33e53

List - [MyNewGUI$PointElement@5ce1e0d4, MyNewGUI$PointElement@59dd4912, MyNewGUI$PointElement@2bd30507, MyNewGUI$PointElement@76979715, MyNewGUI$PointElement@25a33e53]*

I would like to extract and print out the geometric element properties that are encoded in the value portion of geoElemMap<key, value>. The value content seems to be mapped to a memory address that follows the "@" character. My question for Java experts is: ** How can I get the value content? **

2

There are 2 best solutions below

0
Robert Kock On

You should override the toString() method within each class that derives from GeoElement.
For instance in the PointElement:

@Override
public String toString()
{
  return (elemType + " " + pointName + ....); // fill the dots with a string representation of the coordinates.
}
0
Saturn 1401 On

Your class has not overridden the toString() method, so your call to object.toString() which returns an address in memory. You should override the toString() method

class GeoElement{
String elemType = "Generic";         // Element type

 @Override
 public String toString(){
 return elemType;
 }
}

or add a getter

class GeoElement{
String elemType = "Generic";         // Element type

 public String getElemType(){
 return elemType;
 }
}  

and use him

entry.getValue().getElemType()