0

I have an array which contains some definitions which are used in the program:

final String[] names = new String[]{"familyNames", "givenNames", "middleNames", "nickNames", "additionalNames", ..., ..., ..., ...}; // Lots of definitions

And now I need to create a new class with these definitions:

public class Person {
  final private String id;
  final private String familyNames;
  final private String givenNames;
  final private String middleNames;
  final private String nickNames;
  final private String additionalNames;
  ...
  ...
  ...
  ...
  ... // Very long list
}

Is it possible to define this class by that array? Something like:

public class Person {
  final private String id;
  final private .... names...
  public int compareTo(...) {...}
}

This Person class should support sort by multi fields, e.g. first by given name, then by family name, etc.

1
  • Are you only storing strings, or will there be other types mixed in, which you need to access as that type (ie, perform math on an integer/float, get the next month from a date, etc)? For sorting, you'll need to implement Comparable, using @Juned's answer. Commented Dec 20, 2013 at 4:29

2 Answers 2

5

To avoid a long list of class member variables, you may use a map of attributes as mentioned here:

Map<String,String> attributesMap = new HashMap<String,String>();

"familyNames", "givenNames", "middleNames", "nickNames", "additionalNames", etc should be used as keys.

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

2 Comments

Probably the better option, for anyone who isn't building a whatever-to-Java compiler... :)
Just updated my question. The requirement is to sort Person objects by multi-fields. Looks like I need to add CompareTo() to the class, but not sure how to do it base on map?
0

You could define it in a class Person by taking the attributesMap in a static factory method.

public class Person {
  final private String id;
  final private Name name;
  ...
  public static Person fromAttributesMap(Map<String, String> attributesMap) {
      Person person = new Person();
      person.id = attributesMap.get("id");
      ...
  }
}

But do keep related attributes together in their own classes. For eg; you could club familyName, givenName, nickName, etc. in a Name class:

class Name {
  final private String familyName;
  final private String givenName;
  final private String middleName;
}

I'm not in full agreement to the map approach. Infact, there is a code smell around the same - it is called "Primitive obsession"

You could also sort a List<Person>, for example by the family name:

public static Comparator<Person> FamilyNameComparator 
                      = new Comparator<Person>() {

    public int compare(Person person1, Person person2) {


      //ascending order
      return person1.getFamilyName().compareTo(person2.getFamilyName());
    }

};

and then calling:

Arrays.sort(persons, Person.FamilyNameComparator);

You could also sort by multiple fields. Take a look at this question on SO.

3 Comments

How is the program supposed to know that fields X, Y, Z... from the array are actually in a sub-object? How is this stuff supposed to be auto-generated as the question seems to be asking?
By implementing an appropriate constructor, or a factory method like "fromAttributeMap()". I'll probably update my answer; but I still stand by my reservations against using a hashmap directly.
@cHao I am not absolutely sure if the OP wanted to model adhoc/random strings in the "names" array. I'll probably update my answer (and talk about using a factory method).

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.