英文:
I need a function that it receive a function by parameter that can be called inside
问题
我想为在间隔内接收的任何函数执行的功能创建一个函数。
为了更好地解释,我将给您一个示例。
我想要使用此函数来调用REST客户端,但是,不是使用包含10个元素的列表进行调用,而是要使用2个元素进行5次调用。
我的问题是我不想在所有需要按间隔调用的方法中管理循环和间隔,我想做类似这样的事情:Util.executeInIntervals(2(间隔),this.restClient.doPost(fullList))
。此方法应按照以下所示的间隔调用此restClient。
我已经能够创建这个示例类,但结果不正确:
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
public class FunctionAsParameter {
public static <T> void executeInIntervals(List<T> list, int interval, Function<T, String> function) {
// 定义间隔的数量
final int INTERVAL = interval;
int cont = 0;
while (list.size() - cont > INTERVAL) {
function.apply(list.subList(cont, cont + INTERVAL).toString());
cont = cont + INTERVAL;
}
// 处理剩余部分
function.apply(list.subList(cont, list.size()));
}
}
class Main {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
FunctionAsParameter.executeInIntervals(list, 2, value -> {
// 这里将调用REST客户端,使用子列表而不是完整列表
System.out.println(value);
// 这个我想要移除,但Java指示函数不能是void
return "";
});
}
}
结果:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
正确的结果应该是:
[1, 2]
[3, 4]
[5, 6]
[7, 8]
[9, 10]
提前感谢。
英文:
I want to do a function for execute any function received by parameter in intervals.
To explain myself better, I will give you an example.
I want to use this function for make calls to a rest client but, instead of do the call with a list with 10 elements, I want to can do 5 calls with 2 elements.
My problem is I don't want have to manage loops and intervals in all methods that I want call in intervals, I would like do something like this: Util.executeInIntervals (2 (interval), this.restClient.doPost(fullList) ); This method should do calls to this restClient in intervals as indicated below.
I have been able to this example class but the result isn't the correct:
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
public class FunctionAsParameter {
public static <T> void executeInIntervals(List<T> list, int interval, Function function) {
// It's defined the num of interval
final int INTERVAL = interval;
int cont = 0;
while(list.size() - cont > INTERVAL) {
function.apply(list.subList(cont,cont + INTERVAL).toString());
cont = cont + INTERVAL;
}
// The rest are processed
function.apply(list.subList(cont,list.size()));
}
}
class Main {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
FunctionAsParameter.executeInIntervals(list, 2, value -> {
// Here it would be the call to rest client with the sublist, not the full list
System.out.println(list);
// This I would like remove but java indicates that the function can't be void
return "";
});
}
}
Result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Correct result would be:
[1, 2]
[3, 4]
[5, 6]
[7, 8]
[9, 10]
Thanks for advance.
答案1
得分: 1
您的回调函数忽略了您传递给它的参数,而且不清楚参数应该是什么(您一次使用String
,一次使用List<T>
)。
您可以尝试像这样修复您的代码:
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
public class FunctionAsParameter {
public static <T> void executeInIntervals(List<T> list, int interval, Function<List<T>, String> function) {
// 定义了间隔的数量
final int INTERVAL = interval;
int cont = 0;
while (list.size() - cont > INTERVAL) {
function.apply(list.subList(cont, cont + INTERVAL));
cont = cont + INTERVAL;
}
// 剩下的部分被处理
function.apply(list.subList(cont, list.size()));
}
}
class Main {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
FunctionAsParameter.executeInIntervals(list, 2, value -> {
System.out.println(value);
return "";
});
}
}
如果您想要删除return "";
(因为您无论如何不使用该函数的返回值),那么Function
是错误的接口 - 您应该使用Consumer
:
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class ConsumerAsParameter {
public static <T> void executeInIntervals(List<T> list, int interval, Consumer<List<T>> consumer) {
// 定义了间隔的数量
final int INTERVAL = interval;
int cont = 0;
while (list.size() - cont > INTERVAL) {
consumer.accept(list.subList(cont, cont + INTERVAL));
cont = cont + INTERVAL;
}
// 剩下的部分被处理
consumer.accept(list.subList(cont, list.size()));
}
}
class Main {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
ConsumerAsParameter.executeInIntervals(list, 2, value -> {
System.out.println(value);
});
}
}
请注意,在这个简单的示例中,您甚至可以进一步替换executeInIntervals()
的调用:
ConsumerAsParameter.executeInIntervals(list, 2, System.out::println);
对于您在评论中提到的示例,代码可能如下所示:
ConsumerAsParameter.executeInIntervals(fullList, 2, this.restClient::doPost);
英文:
Your callback ignores the parameter you pass into it and it is also unclear what the parameter should be (you call it once with a String
and once with a List<T>
).
You could try to fix your code like this:
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
public class FunctionAsParameter {
public static <T> void executeInIntervals(List<T> list, int interval, Function<List<T>, String> function) {
// It's defined the num of interval
final int INTERVAL = interval;
int cont = 0;
while (list.size() - cont > INTERVAL) {
function.apply(list.subList(cont, cont + INTERVAL));
cont = cont + INTERVAL;
}
// The rest are processed
function.apply(list.subList(cont, list.size()));
}
}
class Main {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
FunctionAsParameter.executeInIntervals(list, 2, value -> {
System.out.println(value);
return "";
});
}
}
If you want to remove that return "";
(because you don't use the return value of that function anyway) then Function
is the wrong interface - you should use a Consumer
:
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class ConsumerAsParameter {
public static <T> void executeInIntervals(List<T> list, int interval, Consumer<List<T>> consumer) {
// It's defined the num of interval
final int INTERVAL = interval;
int cont = 0;
while (list.size() - cont > INTERVAL) {
consumer.accept(list.subList(cont, cont + INTERVAL));
cont = cont + INTERVAL;
}
// The rest are processed
consumer.accept(list.subList(cont, list.size()));
}
}
class Main {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
ConsumerAsParameter.executeInIntervals(list, 2, value -> {
System.out.println(value);
});
}
}
Note that in this simple example you could replace the executeInIntervals()
call even further:
ConsumerAsParameter.executeInIntervals(list, 2, System.out::println);
For the example that you mention in a comment, the code could look like this:
ConsumerAsParameter.executeInIntervals(fullList, 2, this.restClient::doPost));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论