0

I have two lists of type object with data , the first one is principal entity and the second is dependent entity. In addition I have key table that relate between the principal and depended entity objects. In the first for statement I get one instance of type object and then I go and loop on every instance of the second entity and trying to find Match between them (i think exponential problem…) ,if match is find update the principal entity with the reference object .

The following code is working but I check it from performance perspective and it's not working in efficient way.

Do you have an idea/tips how to improve this code from perforce aspect.

In the JVM monitor I found that EntityDataCreator.getInstanceValue have a problem.

This is the method start

// start with the principal entity
        for (Object principalEntityInstance : principalEntityInstances) {

            List<Object> genObject = null;
            Object refObject = createRefObj(dependentMultiplicity);
            // check entries in dependent entity
            for (Object dependentEntityInstance : toEntityInstances) {
                boolean matches = true;

                for (String[] prop : propertiesMappings) {

                    // Get properties related keys
                    String fromProp = prop[0];
                    String toProp = prop[1];

                    Object fromValue = EntityDataCreator.getInstanceValue(fromProp, principalEntityInstance);
                    Object toValue = EntityDataCreator.getInstanceValue(toProp, dependentEntityInstance);

                    if (fromValue != null && toValue != null) {
                        if (!fromValue.equals(toValue)) {

                            matches = false;
                            break;
                        }
                    }
                }

                if (matches) {
                    // all properties match
                    if (refObject instanceof List) {
                        genObject = (List<Object>) refObject;
                        genObject.add(dependentEntityInstance);
                        refObject = genObject;
                    } else {
                        refObject = dependentEntityInstance;
                        break;
                    }
                }
            }

            if (refObject != null) {

                EntityDataCreator.createMemberValue(principalEntityInstance, navigationPropName, refObject);
            }
        }




public static Object getInstanceValue(String Property, Object EntityInstance) throws NoSuchFieldException,
            IllegalAccessException {

        Class<? extends Object> EntityObj = EntityInstance.getClass();
        Field Field = EntityObj.getDeclaredField(Property);
        Field.setAccessible(true);
        Object Value = Field.get(EntityInstance);
        Field.setAccessible(false);
        return Value;
    }
4

2 回答 2

3

my guess would be your best bet is to go through both lists once, prepare all data that you need in hashtables, then do one iteration. this way, your problem becomes N+M instead of N*M


edit

Map<String,List<Object>> principalMap = new HashMap<String,List<Object>>();

for (Object principalEntityInstance : principalEntityInstances) {
   List<String> keys = getKeysFor(principalEntityInstance);
   for(String key : keys) {
       List<Object> l = principalMap.get(key);
       if(l==null) {
           l = new ArrayList<Object>();
           principalMap.put(key,l);
       }
       l.add(principalEntityInstance);
   }
}

the do the same for dependentEntityInstance - this way, your searches will be much faster.

于 2013-04-22T06:48:16.510 回答
1

我可能误解了您的问题,但我建议为您的实体定义一个 equals 方法并为它们定义一个散列方法,以便您可以利用 java 已经具有的所有优点来搜索和匹配实体。

我认为,当尽可能依赖 Java 的基础架构时,Sun/Oracle 花了很长时间才使它变得非常快。

于 2013-04-22T06:52:39.607 回答