Java – find class fields with specified data type

Some Java reflection API examples.

1. Display all fields and data type

A Java reflection example to loop over all the fields declared by a class.

1.1 A POJO.

CompanyA.java

package com.mkyong.test;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class CompanyA {

    String orgName;
    int count;
    List<String> comments;
    Set<String> branches;
    Map<String, String> extra;

    //...
}

1.2 Use Java reflection APIs getDeclaredFields() to loop over and display the field name and data type.

Test.java

package com.mkyong.test;

import java.lang.reflect.Field;
import java.util.List;

public class Test {

    public static void main(String[] args) {

        Field[] fields = CompanyA.class.getDeclaredFields();
        for(Field f : fields){
            Class t = f.getType();
            System.out.println("field name : " + f.getName() + " , type : " + t);
        }

    }

}

Output

field name : orgName , type :class java.lang.String
field name : count , type :int
field name : comments , type :interface java.util.List
field name : branches , type :interface java.util.Set
field name : extra , type :interface java.util.Map

2. Find class fields with specified data type

2.1 This example will find all fields with List data type.

Test.java

package com.mkyong.test;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class TestAbc {

    public static void main(String[] args) {

        List<String> list = getFieldNameWithListDataType();
        for(String data : list){
            System.out.println("List : " + data);
        }


    }

    private static List<String> getFieldNameWithListDataType(){

        List<String> result = new ArrayList<>();

        //CompanyA, refer 1.1
        Field[] fields = CompanyA.class.getDeclaredFields();
        for(Field f : fields){

            // use equals to compare the data type.
            if(f.getType().equals(List.class)){
                result.add(f.getName());
            }

            //for other data type
			
            //Map
            //if(f.getType().equals(Map.class)) 
			
            //Set
            //if(f.getType().equals(Set.class))
			
            //primitive int
            //if(f.getType().equals(int.class))
            //if(f.getType().equals(Integer.TYPE))
			
            //primitive long
            //if(f.getType().equals(long.class))
            //if(f.getType().equals(Long.TYPE))
			
        }

        return result;

    }

}

Output

comments
Note
For primitive types like int, you can compare with int.class or Integer.TYPE

References

  1. Oracle : Java Reflection examples
  2. Oracle : Class – getDeclaredFields Javadoc

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
mkyongAlain Van Hout Recent comment authors
newest oldest most voted
Alain Van Hout
Guest
Alain Van Hout

How about using streams and filter to do this? Something along the lines of:

return Arrays.asList(this.getClass().getDeclaredFields()).stream()
.filter(f -> f.getType().equals(List.class))
.map(Field::getName)
.collect(Collectors.toList());

(not necessarily better, just an alternative approach)

mkyong
Guest
mkyong

Nice Stream example, thanks!