1

This is my code in which I have to append a string so that I can output like this name,email,phoneNumber, These are the two ways I am thinking of

String matchedFields = "";
        DuplicateApplicantPojo duplicateApplicantPojo = new DuplicateApplicantPojo();
        if (applicant.getApplicantName().equals(detectionPojo.getName())) {
        matchedFields = DuplicateSettingsConstants.LABEL_NAME;
        }
        if (applicant.getApplicantEmail1().equals(detectionPojo.getEmail1())) {
        matchedFields = ", " + DuplicateSettingsConstants.LABEL_EMAIL;
        }
        if (applicant.getApplicantCellPhone().equals(detectionPojo.getCellPhone())) {
        matchedFields = ", " + DuplicateSettingsConstants.LABEL_PHONE;
        }

And another way is to

 String matchedFields[] = new String[3];
        int i=0;
        DuplicateApplicantPojo duplicateApplicantPojo = new DuplicateApplicantPojo();
        if (applicant.getApplicantName().equals(detectionPojo.getName())) {
        matchedFields[i] = DuplicateSettingsConstants.LABEL_NAME;
        i++;
        }
        if (applicant.getApplicantEmail1().equals(detectionPojo.getEmail1())) {
        matchedFields[i] = DuplicateSettingsConstants.LABEL_EMAIL;
        i++;
        }
        if (applicant.getApplicantCellPhone().equals(detectionPojo.getCellPhone())) {
        matchedFields[i] =   DuplicateSettingsConstants.LABEL_PHONE;
        }
        String matched=matchedFields[0];
        for(int j=1;j<matchedFields.length;j++)
        {
        matched=", "+matchedFields[i];
        }

Which way should I prefer ? or is there any other way through which I can proceed

Vipul Jain
  • 1,395
  • 1
  • 14
  • 28
  • 1
    technically it would be better to post this to code review though as you haven't really flagged this for errors. – Daemedeor Sep 04 '15 at 04:21
  • Consider making this a bit more readable. Perhaps rename your instances to applicant, and something like expected isntead of detectionPojo – John Sep 04 '15 at 04:49
  • You can make use of this http://stackoverflow.com/a/26195047/1326537 answer to implement this feature – Arun Prakash Sep 04 '15 at 05:40
  • What would make you "prefer" any one solution over another? Faster? Less RAM used? Fewer edge cases? Simpler code? Smaller bytecode? Compiles without warnings in a Java 1.1 environment? ...? We can't tell you which bus to take if we don't know your destination. – Kevin J. Chase Sep 04 '15 at 05:40

4 Answers4

1

Your code doesn't need to do extra stuff. First approach is enough for it.

Time complexity and space complexity both will be larger for your second approach. (though the difference is very small)

afzalex
  • 8,598
  • 2
  • 34
  • 61
  • Thanks for the info, but actually I wanted a better solution for this as I don't want to use as many if else – Vipul Jain Sep 04 '15 at 04:26
  • In your case what you were doing was best approach for it. nothing could be made better. If you come across situation where you will have to write so many if-else statements and you want to avoid it, you should have appropriate data structure to just linearly check everything. Here in your sec approach you are actually doing the same thing twice, one with if-else and then with for. – afzalex Sep 04 '15 at 04:39
0

well neither of them would work because you are overwriting the values from before, instead of just appending the string at the end. I would just do it in the the if test because then you wouldn't be allocating memory to the array and then keeping track of an extra int. It would be even better to use string builder if you did go using the for loop way see: when to use StringBuilder in java

Community
  • 1
  • 1
Daemedeor
  • 1,013
  • 10
  • 22
0

The first one should do, except that you can use StringBuilder for appending of the "matchedFields"

StringBuilder matchedFields = new StringBuilder();
DuplicateApplicantPojo duplicateApplicantPojo = new DuplicateApplicantPojo();
if (applicant.getApplicantName().equals(detectionPojo.getName())) {
   matchedFields.append(DuplicateSettingsConstants.LABEL_NAME);
}if (applicant.getApplicantEmail1().equals(detectionPojo.getEmail1())) {
   matchedFields.append(" , ").append (DuplicateSettingsConstants.LABEL_EMAIL);
}
  .
  .
  .

Get more information about it from API : http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

Karthik R
  • 5,523
  • 2
  • 18
  • 30
0

I suggest add helper method:

private static String getMatched(String prefix, String s1, String s2, String label) {
    String matchedFields = "";
    if (s1.equals(s2)) {
         matchedFields = prefix + label;
    }
    return matchedFields;
}

then you can write (+static import for DuplicateSettingsConstants):

    String matchedFields = "";
    matchedFields += getMatched("", applicant.getApplicantName(), detectionPojo.getName(), LABEL_NAME);
    matchedFields += getMatched(", ", applicant.getApplicantEmail1(), detectionPojo.getEmail1(), LABEL_EMAIL);
    matchedFields += getMatched(", ", applicant.getApplicantCellPhone(), detectionPojo.getCellPhone(), LABEL_PHONE);
sibnick
  • 3,995
  • 20
  • 20