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?
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 ...
});
}
List<SomeClassWithGetValueMethod>to make it work. You need to see how generic works.