ToIntFunction<String> i = (x)-> Integer.parseInt(x);
System.out.println(i.applyAsInt("21235"));
Above is the one example for TointFunction where T is Type String.
What other types that I can pass into it as by definition it accepts Generic Type T
.
Any other example for same with other types except String.
The functional interface ToIntFunction<T>
is just an interface without any implementation. The T
type shall be substituted with any type for what you implement this interface. You can't use the only implementation for all the range of objects of the various and unknown types.
This is applicable for the String, as you have defined:
ToIntFunction<String> i = (x) -> Integer.parseInt(x);
You have substituted T
with String
, the function excepts String
with no discussion and returns Integer
. If you want to have the same function for another type, implement it:
ToIntFunction<List<?>> i1 = list -> list.size(); // A primitive example:
i1.applyAsInt(new ArrayList<>()); // gets size of any List
ToIntFunction<MyObject> i2 = myObject -> myObject.getNumber(); // A custom object
i2.applyAsInt(new MyObject());
The point is, any type could be substituted with one simple rule: the implementation must assure the Integer
as a result in any case.