0

I have the following method and I need to return two strings:

  1. value of type.getName()
  2. value of field.getName()

What is the best way to do it?

public static void getClassMetaData(List<Object> listClsObj) {
    ....

    for (Field field : declaredFields) {
         Class<?> type = field.getType();

         System.out.println(type.getName());
         System.out.println(field.getName());
      }
}
Stefan Strooves
  • 624
  • 2
  • 10
  • 16
  • 1
    I would return a Field object. It has all the information you need and more. – Peter Lawrey Jan 21 '13 at 10:34
  • possible duplicate of [What is the equivalent of the C++ Pair in Java?](http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java) – Fred Foo Jan 21 '13 at 10:36
  • 1
    Why do you think that iterating twice will be bad for your code? These kind of things should really not be bothered, until it becomes a bottleneck in your code. As far as your current code is concerned, I think, what you want cannot be done without that loop. You are iterating over two different list. – Rohit Jain Jan 21 '13 at 11:32

6 Answers6

4

I would rather return just the Field, since you can get the type and other information from the field itself.

And then at the point of invocation, call the method - field.getType() to get the type.


Seems like you are working with all the declared fields of your class, in which case you can just return the List<Field>: -

public static List<Field> getClassMetaData(List<Object> listClsObj) {

    // Rather than iterating over `declaredField`, just return it
    return declaredFields;
}
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • @StefanStrooves.. Added some more stuff in the answer. – Rohit Jain Jan 21 '13 at 10:44
  • Thanks but the issue here is if I return list of field than the method who call to getClassMetaData need to loop again on the list of fields ...from performance aspects i think it bad. – Stefan Strooves Jan 21 '13 at 10:53
  • @StefanStrooves.. Well, if you are returning the list, then of course you are not iterating over it. So, iteration is still done only once. And don't think about these things too early. Adding one extra iteration will not make too much a difference. – Rohit Jain Jan 21 '13 at 10:55
  • @StefanStrooves.. And if you want to send information about more than one fields, then there is no way out. That is the purpose of `List` and other collections in Java. They allow you to add multiple instances of something in one container. – Rohit Jain Jan 21 '13 at 10:57
2

You can return a String array of course, but I think it would be better to simply return Field

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
2

The more flexible and correct approach to return any number of elements - return List implementation of your data collection.

Evgeniy Fitsner
  • 946
  • 9
  • 14
1

You can return an array of size 2 with both the fields.

return new String[]{type.getName(),field.getName()};
MoveFast
  • 3,011
  • 2
  • 27
  • 53
1

You can just return the Field instance instead of try to return 2 String. Otherwise, you can return a object containing your 2 String like a Pair object or a custom object.

class Pair<F, S> {

    private final F first;
    private final S second;

    public Pair(F first, S second) {
        this.first = first;
        this.second = second;
    }
    // getter for first and second
}
Patouche
  • 149
  • 1
  • 5
1

You should try and return a list as you are eventually using the attributes of field object in your other code.

Plus it would make the method more maintainable. The consumer of the method could decide what data it needs from the Field object.

Anugoonj
  • 575
  • 3
  • 9