Reputation: 1336
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
Reputation: 131396
Use a generic type : List<Config>
.
The compiler handles the generic of raw Collection
s 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