Atif Imran
Atif Imran

Reputation: 2049

Find index of an object in a list

I have situation where I have a list(required items) that holds a table column result like:

NAME
ADDRESS
AGE
.
.
etc

In my method I get a User object that contains values for user.getName(), user.getAge() etc. I want to know the best way to ensure that every item in the list is present in the user object. The no of items in the list are variable.

public boolean isUserInfoComplete(User user, ArrayList list){
   //so, if the list has AGE, the user.getAge() must have some value 
}

One way I thought of is maintaining another list that holds values of every user info and checking that against my db list but that is not scalable.

Upvotes: 3

Views: 1193

Answers (4)

atomman
atomman

Reputation: 2505

This seems like a case where you need reflection. This gives you the opportunity to inspect methods and field from your objects at runtime.

If you know your User-objects etc will follow a java bean standard then you will be able to use the getters for checking, though I see now problem in making your fields public final and checking directly on the fields themselves.

Take a look at https://docs.oracle.com/javase/tutorial/reflect/

Upvotes: 1

shmosel
shmosel

Reputation: 50776

It's not possible to dynamically match your method names with the list contents without reflection (which can be expensive and fragile). You may want to consider keeping your User values in a central Map cache. Here's one way to do that:

public class User {

    private enum Field {
        NAME,
        AGE
        //...
    }

    private Map<String, Object> values = new HashMap<>();

    private void putValue(Field field, Object value) {
        values.put(field.name(), value);
    }

    private Object getValue(Field field) {
        return values.get(field.name());
    }

    public void setName(String name) {
        putValue(Field.NAME, name);
    }

    public String getName() {
        return (String)getValue(Field.NAME);
    }

    public void setAge(int age) {
        putValue(Field.AGE, age);
    }

    public Integer getAge() {
        return (Integer)getValue(Field.AGE);
    }

    //...

    public boolean isUserInfoComplete(List<String> fields) {
        return values.keySet().containsAll(fields);
    }
}

Upvotes: 2

Jason
Jason

Reputation: 11822

You could use reflection to solve this problem if the items in the list match the getters in your User object.

For example, if AGE is in the list, you could use reflection to look for the getAge() method on the User class, call it on the object, and then check the result for null (or switch on the method return type to perform other types of checks).

Here's a starting point for you to experiment with (I haven't compiled or tested it):

public boolean isUserInfoComplete(User user, ArrayList list){
    for(String attribute : list) {
        String methodName = "get" + attribute.substring(0, 1).toUpperCase() + attribute.substring(1).toLowerCase();
        Method method = User.class.getMethod(methodName, null);
        if(method != null) {
            Object result = method.invoke(user);
            if(result == null) {
                return false;
            }
        }
    }
    return true;
}

Upvotes: 1

m.aibin
m.aibin

Reputation: 3603

You can check it using contains() while looping. This process will be very resource-consuming. Maybe you can redesign something and simply compare two User objects? Will be faster. You can do it by providing your own implementation of equals and hashcode methods.

Upvotes: 0

Related Questions