nilesh
nilesh

Reputation: 1336

Java get class name of arrayList of object and type cast the array list without loop

I have List of objects and need to type cast from class name. e.g

public void foo(List configs){
    //configs type cast code
    configs.forEach(config -> {
        String value = config.getValue();
    });
}

Is there any way to do this?

Upvotes: 0

Views: 666

Answers (1)

davidxxx
davidxxx

Reputation: 131396

Use a generic type : List<Config>.
The compiler handles the generic of raw Collections as Object. So it is like if you had declared : List<Object>.

Supposing you have a List of Config instances, you could write :

public void foo(List<Config> configs){
    configs.forEach(config -> {
                String value = config.getValue();
                // use the value ...
              });    
}

Upvotes: 1

Related Questions