1

I'm trying to find if multiple HashMaps are empty.

To give some context. I have a hashmap declared here.

static Map<Integer, College> tblColleges = new HashMap<Integer, College>();

For each college object:

Map<Integer, Department> tblDepartments = new HashMap<Integer, Department>();

I'm trying to add a major. Majors can only exist as an attribute of Department.

Here's what I have right now.

int numberofColleges = Databases.tblColleges.size();
int emptyColleges = 0;
for(int key: Databases.tblColleges.keySet()) {
        if(Databases.getTblColleges(key).tblDepartments.isEmpty()) {
                emptyColleges++;
            }
        }
    if(numberofColleges == emptyColleges) {
        System.out.println("Invalid. Requires at least 1 department.");
    }

I should only be able to create a Major if at least 1 college has a department.

Essentially for each college object that exists in the tblColleges, I'm checking to see if it's department hashmap is empty. If it is empty, then I increment the number of empty colleges.

Afterward, I compare the number of college objects with empty college objects found, if they are equal then I print an error.

I was wondering if there was a better more efficient way to do this, maybe with some function that exists that I'm not familiar with rather than using variables.

2
  • 2
    Are you looking for tblColleges.isEmpty()? Commented May 5, 2019 at 3:11
  • Not necessarily, I already have that above what I've shown. The issue with just having that is there can be a scenario in which I could have multiple colleges which exist but no departments yet. Commented May 5, 2019 at 4:14

1 Answer 1

2

Q: Can you do the check "more efficiently"?

A: You could optimize it a bit:

boolean nonEmptyColleges = false;

for (int key: Databases.tblColleges.keySet()) {
    if (!Databases.getTblColleges(key).tblDepartments.isEmpty()) {
        nonEmptyColleges = true;
        break;
    }
}

The above short circuits as soon as it finds a College with a Department. That will be a substantial improvement in a lot of cases.

Then, assuming that Databases.tblColleges is a Map:

boolean nonEmptyColleges = false;

for (int college: Databases.tblColleges.values()) {
    if (!college.tblDepartments.isEmpty()) {
        nonEmptyColleges = true;
        break;
    }
}

Q: Can you do the check with less code?

A: Using Java 8 streams you could write the last as:

boolean nonEmptyColleges = Databases.tblColleges.values().stream()
           .anyMatch(c -> !c.tblDepartments.isEmpty());

(I think ...)


Q: But is this the right approach?

A: IMO, no.

It seems that you intend to do this check each time you add a major. That's not necessary.

Majors can only exist as an attribute of Department.

The key thing that you need to check is that the Department you want to add the major for exists.

  • If the Department doesn't exist you can't add the major to it.
  • If the Department does exist you can the major to it, whether or not it is currently a department of a college1.

The bigger point here is that any data model is going to have a variety of data integrity rules / constraints on it. But that does mean that you need to explicitly check all of them each time the model is changed. You only need to check the preconditions for the change (e.g. that the Department exists) and any constraints that could be invalidated by the change.


1 - The "not" case assumes that there may be some other way of finding a Department. It could be a separate table of Department objects, or it could be that you are in the process of creating and building a new Department and haven't added it to its College yet.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the advice. When you say that I should only check if the department I'm trying to add the major in exists, my issue here is that I only know which department after the user has selected it. I want to be able to print the error out and avoid letting the user select a department in the first place if none exists yet.
Ummm ... If a department doesn't exist, how can the user select it?
Sorry I should have explained that more clearly. The way my program is set up is that it allows user to select what they want to create (College, Department, Major). If they select to create a major, the system than asks to select a college and a following department. I want to avoid giving them the option to select a college and department if it's not possible to create a major.
That makes even less sense. The logic should be: 1) if there are no colleges, only a college can be created, 2) if there are colleges and no departments, then colleges or departments may be created, 3) otherwise colleges, departments and majors can all be created. You don't need to know if there are only colleges with no departments. If a department exists, then there must be a college with a department. Unless you allow creation of departments that are not (yet) in a college ... in which case there is no reason to stop creation of a major in a department that isn't in a college!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.