How to overload a method with a method using a parameter list that contains parameters of the same type, but parameterized with other types

I have methods:

public List<Integer> convertBy(Function<String, List<String>> flines, Function<List<String>, String> join, Function<String, List<Integer>> collectInts) { return collectInts.apply(join.apply(flines.apply((String) value))); }//first method public Integer convertBy(Function<List<String>, String> join, Function<String, List<Integer>> collectInts, Function<List<Integer>, Integer> sum) { return sum.apply(collectInts.apply(join.apply((List<String>) value))); }//second method 

Despite the fact that their parameters are parameterized by different types, I can not overload the first method. I could use a different interface except Function<T,R> , but I don’t know which one is enough, because I looked at their list and could not find it https://docs.oracle.com/javase/8/docs/ api / java / util / function / package-summary.html .

Parameters in these functions:

flines - reads a file from a given path ( String ) and returns a list of strings in this file ( List<String> )

join - joins the element of the specified List<String> and returns a String

collectInts - collectInts given String and returns a list of integers found in this String .

sum - adds elements from List<Integers> and returns the sum

Questions:

  • Is it possible to overload the first mechod with the second?

  • What other functional interface can I use besides the function? I think not, because the types of arguments and result are always different.

+1
source share
1 answer

If you want to create a method that uses several functions and is not interested in intermediate values, you can make it a general method. The code in your question is weird because it assumes that the value can be String and a List<String> at the same time.

But compared to your other question , you have a different picture. Although the varargs method does not work this way, you can easily provide overloaded methods for actual use cases:

 public class InputConverter<T> { private T value; public InputConverter(T value) { this.value = value; } public <R> R convertBy(Function<? super T, ? extends R> f) { return f.apply(value); } public <T1,R> R convertBy( Function<? super T, ? extends T1> f1, Function<? super T1, ? extends R> f2) { return f2.apply(f1.apply(value)); } public <T1,T2,R> R convertBy( Function<? super T, ? extends T1> f1, Function<? super T1, ? extends T2> f2, Function<? super T2, ? extends R> f3) { return f3.apply(f2.apply(f1.apply(value))); } public <T1,T2,T3,R> R convertBy( Function<? super T, ? extends T1> f1, Function<? super T1, ? extends T2> f2, Function<? super T2, ? extends T3> f3, Function<? super T3, ? extends R> f4) { return f4.apply(f3.apply(f2.apply(f1.apply(value)))); } } 

Assuming you fixed your interface types and created the functions described in this answer , you can use it as

 InputConverter<String> fileConv=new InputConverter<>("LamComFile.txt"); List<String> lines = fileConv.convertBy(flines); String text = fileConv.convertBy(flines, join); List<Integer> ints = fileConv.convertBy(flines, join, collectInts); Integer sumints = fileConv.convertBy(flines, join, collectInts, sum); 
+3
source

All Articles