javatree

Implement Nested Treemaps in Classes


I need to create a 3 nested Tree maps in classes. In other words, implement my own classes with a clear naming and those classes internally use a Map of other custom classes (that contains a map again).

This would be the structure:

TreeMap<String, TreeMap<String, TreeMap<String, Integer>>> squareRPM = new TreeMap<String, TreeMap<String, TreeMap<String, Integer>>>();

I created this class that would be the outer tree map:

import java.util.TreeMap;

public class Check extends InnerCheck {
private TreeMap<String, InnerCheck> tm; 
        
    public Check (){
        tm = new TreeMap<String, InnerCheck>();
    }
    
    public void addTm1(String coinName, InnerCheck coinValue ){
        tm.put(coinName, coinValue);
    }
    
    public void getT(String coinName){
        System.out.println("My " + tm.get(coinName));
        
    }
    
    public TreeMap<String, InnerCheck> getTm() {
        return tm;
    }

    public void setTm(TreeMap<String, InnerCheck> tm) {
        this.tm = tm;
    }
}

The Inner tree map would be:

import java.util.TreeMap;

public class InnerCheck extends InnerInnerCheck{
    private TreeMap<String, InnerInnerCheck> ttm; 
    
    public InnerCheck (){
        ttm = new TreeMap<String, InnerInnerCheck>();
    }
    public void addTm2(String coinName, InnerInnerCheck coinValue){
        ttm.put(coinName, coinValue);
    }
    public TreeMap<String, InnerInnerCheck> getTttm() {
        return ttm;
    }
    public void setTttm(TreeMap<String, InnerInnerCheck> ttm) {
        this.ttm = ttm;
    }
}

The Inner Inner Tree map would be:

import java.util.TreeMap;

public class InnerInnerCheck {

        private TreeMap<String, Integer> tttm; 
        
        public InnerInnerCheck(){
            tttm = new TreeMap<String, Integer>();
        }
        public void addTm3(String coinName, Integer coinValue){
            tttm.put(coinName, coinValue);
        }
        public TreeMap<String, Integer> getTtm() {
            return tttm;
        }
        public void setTtm(TreeMap<String, Integer> tttm) {
            this.tttm = tttm;
        }
}

In main I'm trying to put items in the nested Treemaps, but I can't because addTm2 has an error which indicates me that I might have implemented those three maps classes in a wrong way.

I'm accessing it:

Check money1 =  new Check();
        
money1.addTm1("dinero2", money1.addTm2("dinero2",(money1.addTm3("dinero1", 3))));`

The error is: The method addTm2(String, InnerInnerCheck) in the type InnerCheck is not applicable for the arguments (String, void).

Is the first time I'm trying to implement nested Tree maps within classes. Please provide feedback, is for an academic project. Thank you in advance.


Solution

  • Calling money1.addTm3("dinero1", 3) returns void (-> nothing)

    Therefore using it as a parameter for addTm2(String coinName, InnerInnerCheck coinValue) cannot work as the method expects an InnerInnerCheck object as parameter. If you want to use your addTm methods in this way you have to make them return the Tree object:

    public InnerInnerCheck addTm2(String coinName, InnerInnerCheck coinValue){
        ttm.put(coinName, coinValue);
        return this;
    }