Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I want to return a tuple of
double
data types. I am unable to create
Pair
. I have read the
documentation
but unable to make it.
I am doing as
private Pair myNums(double F, double S) {
Pair<F f, S s> p = Pair.create(F, S);
return p;
But it is saying
Unknown class F
–
–
–
Try private <T, S> Pair<T, S> myNums(...) {}. in Java, you must specify the generics before the return type of the method. But you cannot specify double as your argument types. Ideally, do not specify types in your arguments as this will make your method truly generic.
private <T, S> Pair<T, S> myNums(T t, S s) {
// do work to create the Pair and return
Just remember, if you're using generics, then use the power of generics in Java, don't then specify types like double, int or String. It really defeats the very essence and reason why generics are used and their intention.
private Pair myNums() {
Pair<String, Integer> p = Pair.create("Prashant M", 1000);
return p;
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.