GondraKkal
GondraKkal

Reputation: 97

Using and printing content of Set<Set<T>> in Java

I have a Java file with this variable, and there should be String content in it:

private Set<Set<MyAxiom>> explanations;

I read that Sets are collections of some type of data (which should be MyAxiom, I guess), but couldn't find how to access them, and I need to stamp.

I tried to retrieve these explanations with a get-return method

public Set<Set<OWLAxiom>> getExpl(int index) {
    return this.explanations(index);
}

Similar methods worked out for other normal variables, but I think a Set needs is own commands, doesn't it? And by the way this is a set of sets. I find it really complex. I would be really glad to know how to handle them, and if not, a documentation link would be appreciated.

EDIT: Maybe adding this is useful for your answers. That explanations variable is in a javabean used by a jsp file. It's within a webapp project I compile with maven. The compilation is fine with this code below, but when I access via browser (with Tomcat) I get the error The method getExpl() in the type BundleQueryManagement is not applicable for the arguments (int)

This is the alternate method I tried:

public Set<Set<OWLAxiom>> getExpl() {
    return this.explanations;
}

Or even

public void getExpl() {
    return this.explanations;
}

Upvotes: 0

Views: 1010

Answers (1)

Raman Sahasi
Raman Sahasi

Reputation: 31891

Your code tells that you have an index value and you want to access corresponding value in your Set. But actually you cannot access Set using index as Sets are unordered collections of objects. So it's not possible.

If you want to access your elements this way, then you should consider using a list instead.

See also: Why doesn't java.util.Set have get(int index)?


EDIT

You can print all elements recursively like this:

public void printExpl(Set<Set<MyAxiom>> explanations) {
    for (Set <MyAxiom> exp: explanations) {
        for (MyAxiom obj: exp) {
            System.out.println(obj.toString());
        }
    }
}

Upvotes: 2

Related Questions