Java find class fields with specified data type

摘要: Some Java reflection API examples.

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

上一篇: MongoDB group, count and sort example
下一篇: Java 8 flatMap example
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号