问题
I have a 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 their parameteres are parametrized with different types I cannot overload the first method. I might use different interface, other than Function<T,R>
but don't know which one would suffice as I went through list of them and couldn't find one https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html.
Parameters in those functions are:
flines
- reads the file from given path (String
) and returns list of lines in that file (List<String>
)
join
- concatenates element of given List<String>
and returns a String
collectInts
- parses the given String
and returns List of integers found in that String
.
sum
- adds elements from List<Integers>
and returns the sum
Questions:
Can I overload the first mehod by the second one?
What other existing functional interface I might use besides function? I think none, as the types of argument and result always differ.
回答1:
If you want to create a method which applies multiple functions and is not interested in the intermediate values, you can make it a generic method. The code in your question is strange as it assumes that value
can be a String
and a List<String>
at the same time.
But comparing with your other question, there’s a different picture. While the varargs method there can’t work that way, you can easily provide overloaded methods for the 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 that you fixed your interface types and created functions as described in this answer, you can use it like
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);
来源:https://stackoverflow.com/questions/33256485/how-to-overload-a-method-by-method-with-parameters-list-that-contains-parameters