简体   繁体   中英

Tree Map retrieves value of HashMap instead of the Tree Map Values Java

I have three classes with nested tree maps. I have a method to put values in each of the maps and a method to retrieve those values. The problem that I have is that instead of retrieving the values the output in the console is something like this: project.Verbal@5c647e05

How I input values:

Verbal square = new Verbal();
        square.addVerbal("arbol1", square.addInVerbal("arbol2", square.addInInVerbal("atributo name", "attributeValue")));

How I retrieve values:

System.out.println(square.getVerbal("arbol1").getInVerbal("arbol2").getInInVerbal("atributo name"));

Parent map:

 import java.util.TreeMap;

public class Verbal extends InVerbal {

    private   TreeMap<String, InVerbal> squareRPM =  new TreeMap<String, InVerbal>();

    public TreeMap<String, InVerbal> getSquareRPM() {
        return squareRPM;
    }

    public void setSquareRPM(TreeMap<String, InVerbal> squareRPM) {
        this.squareRPM = squareRPM;
    }


//Put items in the treemap
    public Verbal addVerbal(String key, InVerbal component){

        squareRPM.put(key, component);

        return this;

    }

//Get items in the treemap
    public Verbal getVerbal(String key){

            squareRPM.get(key);

            return this;
    }
}

Inner Map inside Parent

import java.util.TreeMap;

public class InVerbal extends InInVerbal {

    private   TreeMap<String, InInVerbal> component =  new TreeMap<String, InInVerbal>();

    public TreeMap<String, InInVerbal> getComponent() {
        return component;
    }

    public void setComponent(TreeMap<String, InInVerbal> component) {
        this.component = component;
    }

//Put items in the treemap
    public InVerbal addInVerbal(String key, InInVerbal attributes){

        component.put(key, attributes);

        return this;

    }
//Get items in the treemap
    public InVerbal getInVerbal(String key){

        component.get(key);

        return this;
    }
}

Inner Map > Inside Inner Map > Inside Parent

    import java.util.TreeMap;

public class InInVerbal {

    private   TreeMap<String, String> attribute =  new TreeMap<String, String>();

    public TreeMap<String, String> getAttribute() {
        return attribute;
    }

    public void setAttribute(TreeMap<String, String> attribute) {
        this.attribute = attribute;
    }

//Put items in the treemap
    public InInVerbal addInInVerbal(String attributeName, String attributeValue){

    attribute.put(attributeName, attributeValue);

    return this;

    }

//Get items in the treemap
    public InInVerbal getInInVerbal(String attributeName){

    attribute.get(attributeName);

    return this;

    }           
}

Thank you in advance.

You are getting the result of calling the default Object 's toString() method. override it to your desired output.

For example

@Override
public String toString() {
  return this.squareRPM.toString();
}

I think you just need to override the Object class toString() method inside that particular class because it is actually calling the object class toString() while printing the values.

like this:

class Car {
    public String getCarName() {
        return carName;
    }

    public void setCarName(String carName) {
        this.carName = carName;
    }

    public Car(String carName) {
        this.carName = carName;
    }

    @Override
    public String toString() {
        return "Car{" +
                "carName='" + carName + '\'' +
                '}';
    }

    String carName;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM