Sometimes when I'm working with a class (usually one that represents a database model), I'll have about 15 different properties on it, usually of different types. These are properties that need to be accessed by the users, as well, so each property will typically need some function like

T getPropertyName();
setPropertyName(T value);

But, it's also extremely valuable for me to be able to iterate through each property in a particular order (although, I suppose that "value" may stem from laziness of not having to type each one individually). Is there some logical way to set up this class, so that I could iterate through it in this order? Or am I helpless for organization, and I should just go back to the basics of having 15 private variables, and 15 "getters" and "setters"?

As an aside, currently I'm working with Android and Java, so answers pertaining specifically to that would be the most helpful, but seeing how I encounter this somewhat frequently in other languages, too, any solution would be ideal.

Edit: I've been thinking about using something similar to a Bundle with a list of keys, but I don't know how effective that would really be either.

  • It sounds to me like you are recreating a database from scratch. Why not just use a db ... perhaps an embedded one? – Job Jun 5 '11 at 13:23
  • Hmm, perhaps I see your point here. Basically, what I was doing before was collecting data from the internet, that was "shaped" a certain way. At this point I would put it into the database, but usually through an intermediary object, such as a class. I was then using that class to essentially pass data back and forth between activities. Are you suggesting, however, that I just "eliminate the middle man" and start solely using the database for data? – Andrew Hays Jun 5 '11 at 13:34
  • 1
    I do not really know. You know your design better than anyone else. There is never a one best way to do things. However, the work of creating 15 balanced binary trees or what have you, which work on your data sounds like a huge pain. Databases can do this already with good indexes. I would try to leverage that and see what the performance is. However, if your database server is far away, then an embedded one (as a cache) might help sqlite.org/features.html – Job Jun 5 '11 at 13:40

Use reflection for a given object to discover its getX/setX methods, and then invoke them as needed.

Note that in restrictive environments you may not be allowed to do this.

| improve this answer | |
  • Can you give me an example of how you might do this? I've never actually had to seriously use reflection for anything in Java, because as it stands, what I know about reflection in other languages seems like it would be more complicated than just iterating through a list of properties. – Andrew Hays Jun 5 '11 at 12:16
  • You might find this page interesting: download.oracle.com/javase/tutorial/reflect/class/… – user1249 Jun 5 '11 at 15:10
  • i think this is the best answer – Chani Jun 6 '11 at 4:51
  • Also, do not trust any data received from the internet, including which setter to call. – user1249 Jun 6 '11 at 5:42

If you don't mind a bit of repetition if it means avoiding "magic"... Use a nested class (non-static) that implements java.util.Map<String,Object>. This provides a uniform way for outside code to enumerate a set of properties you choose, while keeping the implementation details (ex. reflection versus a bunch of if/then clauses) hidden.

public class Product {
    private String productName;
    private int productPrice;
    private PropMap props = new PropMap();

    class PropMap implements Map<String,Object>{
    /* Reflection, or if-then statements, etc. 
       Access outer items ex: this.Product.productName

       Throw exceptions if someone tries to store the wrong 
       kind of item.

       keySet() and `iterator()` will need to return implementations 
       that provide things in an appropriate order.

    public PropMap getPropMap(){
        return props;


So external code could go:

Map<String,Object> m = myobj.getPropMap();
for(m.keySet(): String k):
    Object v = m.get(k);

It's also possible to refactor PropMap into its own class, ProductPropMap, but then it wouldn't have access to private items.

| improve this answer | |

The setters and getters could access some kind of (private) collection that you also make accessible. Basically each property would know a key value into the collection to obtain the value it represents. You can do sorting/ordering on the collection.

A reflection based solution is probably more maintainable though.

| improve this answer | |
  • That's similar to the current implementation that I had been trying to work on, but iterating through each one becomes a chore, since I would have to do some casting on it to get it back to its original structure, in the end. I still am somewhat unsure of how a "reflection based solution" would work in java, however. – Andrew Hays Jun 5 '11 at 13:05
  • Can't really get rid of the casting problem. Perhaps you can find some shared interface/abstraction that the ordered collection returns? It somewhat depends on what you use the ordered collection for. Perhaps returning a string representation is enough etc.. – Joppe Jun 5 '11 at 13:43

If you want to iterate through that object why not start looking at Scala on Android? In this I'm being serious. You can use the scala.Product object or any object which implements it to get to the nth item of a list. Then, if you want an ordered iteration, you simply make a list of ints to traverse.

Of course, if that is not for you then there are other solutions. You can reimplement a Tuple within Java or try to find a pre-existing library which contains Tuples.

| improve this answer | |
  • I'm not entirely sure that I like the idea of adding another library just for this one functionality. I think I might go with @Job's comment on the question, and just refactor it so that I don't need the class at all. Or, I guess I'll just iterate. – Andrew Hays Jun 5 '11 at 14:52
private Map property;

Public String getPropertyValue(String Key)
     return (p.get(Key));

Since you know the keys you can order as you like . Hope this will work

| improve this answer | |
  • Unfortunately, no. Since a lot of my values are of different types (I have ints, a couple Dates, etc.), this implementation won't quite work. – Andrew Hays Jun 6 '11 at 0:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.