树映射检索HashMap的值而不是树映射值Java

时间:2015-09-16 13:51:12

标签: java tree

我有三个带有嵌套树图的类。我有一个方法在每个映射中放置值和一个方法来检索这些值。我遇到的问题是,不是检索值,而是控制台中的输出是这样的:project.Verbal@5c647e05

我如何输入值:

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

我如何检索值:

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

父地图:

 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;
    }
}

父内部的内部地图

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;
    }
}

内部地图&gt;内部地图内部&gt;内部父母

    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;

    }           
}

提前谢谢。

2 个答案:

答案 0 :(得分:1)

您将获得调用默认Object's toString()方法的结果。将其覆盖到您想要的输出。

例如

CLASS

答案 1 :(得分:1)

我认为你只需要覆盖该特定类中的Object类toString()方法,因为它实际上是在打印值时调用对象类toString()。

像这样:

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;
}