Kaloyan Roussev
Kaloyan Roussev

Reputation: 14711

Can I have a LinkedHashSet of LinkedHashSets?

I have quite a few LinkedHashSets of different types of objects to pass from one java class to another, do I pack them in a bigger object (another linked hash set if this is possible) or do I just pass them in the normal way as parameters?

Upvotes: 0

Views: 86

Answers (2)

TheConstructor
TheConstructor

Reputation: 4465

Both is possible.

If you pack the LinkedHashSets into an other LinkedHashSet you are probably loosing type information as LinkedHashSet<LinkedHashSet<?>> is the only way to collect all kinds of LinkedHashSets in one place. You may also have a look into HashMap as normally you will at some point try to access a specfic sub-LinkedHashSet; using a map this is achieved with ease by defining constant lookup-keys in a common class or interface.

If there are always the same LinkedHashSets to pass between classes, parameters or parameter objects are often a better solution as they provide type information. A parameter object's class could look like this

public class Parameters {
    private LinkedHashSet<String> namesSet = null;
    private LinkedHashSet<Locale> localesSet = null;

    public Parameters(LinkedHashSet<String> namesSet, LinkedHashSet<Locale> localesSet) {
        this.namesSet = namesSet;
        this.localesSet = localesSet;
    }

    public Parameters() {
    }

    public LinkedHashSet<String> getNamesSet() {
        return namesSet;
    }

    public void setNamesSet(LinkedHashSet<String> namesSet) {
        this.namesSet = namesSet;
    }

    public LinkedHashSet<Locale> getLocalesSet() {
        return localesSet;
    }

    public void setLocalesSet(LinkedHashSet<Locale> localesSet) {
        this.localesSet = localesSet;
    }
}

the advantage of parameter-objects is that they keep the method-signature short and can be passed around; just be careful when changing such objects by concurrent threads ;-).

Upvotes: 1

JustinKSU
JustinKSU

Reputation: 4989

Yes. For example:

LinkedHashSet<LinkedHashSet<String>>

Upvotes: 1

Related Questions