I have a List of Objects
which I want to convert to a List of Integers
List<Object> list = new ArrayList<>();
list.add("StringTest");
list.add(10);
list.add(5.9);
list.add(2.5f);
list.add(12L);
... // different datatypes
List<Integer> result = new ArrayList<>();
// convert items from list and add to result
What could be a good custom implementation to add values from different datatypes of List<Object>
to a List<Integer>
- Strings may be appended based on their length.
- Floating numbers needs to be rounded
I know standard implmentation on How to cast an Object to an int in java? but I am looking for a good structure to write a generic one.