Elopteryx
Elopteryx

Reputation: 285

Foreach loop inside foreach loop (Java)

I have the following code:

Set<TOrganization> organizations = new LinkedHashSet<TOrganization>();
Set<TRole> roles = new LinkedHashSet<TRole>();
StringBuilder message = new StringBuilder("Requested roles: " + "\n");

//I fill them up with names like Test org A, Test Role A 1

for(TOrganization org : organizations) {
    message.append(" - " + org.getName()+ "\n");
    for(TRole role : roles) {
        if(role.getOrganization().equals(org)) {
            message.append("   - " + role.getName()+ "\n");
        }
    }
}

I want to print out the roles in separate categories, something like this:

" - Test org A"
" - Test Role A 1"
" - Test Role A 2"
" - Test org B"
" - Test Role B 1"
" - Test Role B 2"

But my code always appends the organization name before the roles, like this:

" - Test org A"
" - Test Role A 1"
" - Test org A"
" - Test Role A 2"
" - Test org B"
" - Test Role B 1"
" - Test org B"
" - Test Role B 2"

It seems message.append(" - " + org.getName()+ "\n"); gets executed whenever the second loop runs. How is that possible?

Edit: I tested it with printlns but the role names are fine. I didn't touch equals() or hashcode(), I'm simply comparing strings with equals(). The problem is that the org names gets appended into the message for each role belonging to them when they should only appear once.

Upvotes: 0

Views: 4743

Answers (2)

Flo
Flo

Reputation: 411

This is working

class TOrganization {

    private String name;

    public TOrganization(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj instanceof TOrganization) {
            if (((TOrganization) obj).getName().equals(name)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

}

class TRole {

    private TOrganization organization;
    private String name;

    public TRole(TOrganization organization, String name) {
        this.organization = organization;
        this.name = name;
    }

    public TOrganization getOrganization() {
        return organization;
    }

    public String getName() {
        return name;
    }

}

public static void main(String[] args) {
    Set<TOrganization> organizations = new LinkedHashSet<TOrganization>();
    Set<TRole> roles = new LinkedHashSet<TRole>();
    StringBuilder message = new StringBuilder("Requested roles: " + "\n");

    TOrganization orga = new TOrganization("Test org A");
    TOrganization orgb = new TOrganization("Test org B");

    organizations.add(new TOrganization("Test org A"));
    organizations.add(new TOrganization("Test org B"));
    roles.add(new TRole(orga, "Test Role A 1"));
    roles.add(new TRole(orga, "Test Role A 2"));
    roles.add(new TRole(orgb, "Test Role B 1"));
    roles.add(new TRole(orgb, "Test Role B 2"));

    for (TOrganization org : organizations) {
        message.append(" - " + org.getName() + "\n");
        for (TRole role : roles) {
            if (role.getOrganization().equals(org)) {
                message.append("   - " + role.getName() + "\n");
            }
        }
    }
    System.out.println(message.toString());
}

Output

Requested roles: 
 - Test org A
   - Test Role A 1
   - Test Role A 2
 - Test org B
   - Test Role B 1
   - Test Role B 2

Your loops are working, you shold fix your classes

Upvotes: 1

johan d
johan d

Reputation: 2873

You have to assign roles in the outer loop (so that it varies when org varies)

for(TOrganization org : organizations) {
    message.append(" - " + org.getName()+ "\n");

    // like this, you adapt ;)
    roles = org.getRoles();

    for(TRole role : roles) {
        if(role.getOrganization().equals(org)) {
            message.append("   - " + role.getName()+ "\n");
        }
    }
}

edit: you should make Set<TRole> roles = new LinkedHashSet<TRole>(); member of TOrganization IMO

Upvotes: 0

Related Questions