Skip to content
Advertisement

How to calculate the count of null field from the model dynamically using java

How to calculate profile completion percentage? Refer to the below Profile model file, Each attribute is another model(EX: BasicInfo has some 15 elements, need to take the count of this and then finally adding the count of this one to Profile total count) . I need to get BasicInfo count dynamically without adding if condition check for all fields. Refer the below code

Logic:

  1. Each attribute has a weight of 1
  2. There are 13 elements in Profile
  3. Each attribute has many elements (assuming BasicInfo has 15 and user has filled 10), then you should return 10/15 = 0.67
  4. Then sum all of the attributes and divide by 13.

// model sample

JavaScript

// What I tried

JavaScript

Advertisement

Answer

Have you tried reflection? You can get all the declared fields of a class. I coded a tiny method to filter nulls as you asked

JavaScript

Here’s the output of this code

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement