如何用java迭代每n个元素?

如何用java迭代每n个元素?,java,java-8,Java,Java 8,我有一个名为calculate的方法,它需要很长时间才能完成。因此,我决定将我的信息列表对象部分发送到此方法。如何迭代每n个元素 public static void main(String [] args){ Map<String, Long> info....; //my info Map //I want to call method like for(int i = 0; i<info.size(); i+=5) calcul

我有一个名为calculate的方法,它需要很长时间才能完成。因此,我决定将我的信息列表对象部分发送到此方法。如何迭代每n个元素

public static void main(String [] args){
    Map<String, Long> info....;  //my info Map

    //I want to call method like 
    for(int i = 0; i<info.size(); i+=5)
       calculate(info.submap(i,i+5)); 
}


public static boolean calculate(Map<String, Long> info){
    //Some calculations

}
publicstaticvoidmain(字符串[]args){
地图信息….;//我的信息地图
//我想调用这样的方法

对于(inti=0;i您可以使用以下代码

class SomeClass {

    private final int BUFFER_SIZE = 5;    

    public static void main(String[] args) {
        Map<String, Long> info = new HashMap<>();

        LongStream.range(0, 30).boxed().forEach(i -> info.put("key" + i, i)); // for test

        IntStream.range(0, info.size() / BUFFER_SIZE)
                .boxed()
                .parallel()
                .map(i -> Arrays.copyOfRange(info.keySet().toArray(), BUFFER_SIZE * i, BUFFER_SIZE * (i + 1)))
                .map(Arrays::asList)
                .map(keys -> info.entrySet().stream()
                        .filter(x -> keys.contains(x.getKey()))
                        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)))
                .forEach(SomeClass::calculate);
    }

    public static boolean calculate(Map<String, Long> info) {
        System.out.println("calculation for " + info.toString());
        return true;
    }
}
class-SomeClass{
专用最终整数缓冲区大小=5;
公共静态void main(字符串[]args){
Map info=newhashmap();
LongStream.range(0,30).boxed().forEach(i->info.put(“key”+i,i));//用于测试
IntStream.range(0,info.size()/BUFFER\u size)
.boxed()
.parallel()
.map(i->Arrays.copyOfRange(info.keySet().toArray(),BUFFER_SIZE*i,BUFFER_SIZE*(i+1)))
.map(数组::asList)
.map(key->info.entrySet().stream())
.filter(x->keys.contains(x.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue)))
.forEach(SomeClass::calculate);
}
公共静态布尔计算(地图信息){
System.out.println(“计算”+info.toString());
返回true;
}
}

您可以使用以下代码

class SomeClass {

    private final int BUFFER_SIZE = 5;    

    public static void main(String[] args) {
        Map<String, Long> info = new HashMap<>();

        LongStream.range(0, 30).boxed().forEach(i -> info.put("key" + i, i)); // for test

        IntStream.range(0, info.size() / BUFFER_SIZE)
                .boxed()
                .parallel()
                .map(i -> Arrays.copyOfRange(info.keySet().toArray(), BUFFER_SIZE * i, BUFFER_SIZE * (i + 1)))
                .map(Arrays::asList)
                .map(keys -> info.entrySet().stream()
                        .filter(x -> keys.contains(x.getKey()))
                        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)))
                .forEach(SomeClass::calculate);
    }

    public static boolean calculate(Map<String, Long> info) {
        System.out.println("calculation for " + info.toString());
        return true;
    }
}
class-SomeClass{
专用最终整数缓冲区大小=5;
公共静态void main(字符串[]args){
Map info=newhashmap();
LongStream.range(0,30).boxed().forEach(i->info.put(“key”+i,i));//用于测试
IntStream.range(0,info.size()/BUFFER\u size)
.boxed()
.parallel()
.map(i->Arrays.copyOfRange(info.keySet().toArray(),BUFFER_SIZE*i,BUFFER_SIZE*(i+1)))
.map(数组::asList)
.map(key->info.entrySet().stream())
.filter(x->keys.contains(x.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue)))
.forEach(SomeClass::calculate);
}
公共静态布尔计算(地图信息){
System.out.println(“计算”+info.toString());
返回true;
}
}

听起来你想做的是为你的
地图信息
实例所代表的数据实现一种排序。然后你可以为这些批创建一个
流:这在某种程度上与方法家族相反,但具有讽刺意味的是,因此你可能不得不自己创建一个命令式的批ner-例如:

private static <T> Stream<Stream<T>> createBatchStreams(final Iterator<T> iter, final int maxBatchSize) {
    final Stream.Builder<Stream<T>> resultBuilder = Stream.builder();
    {
        // NOTE: This logic could also be encapsulated in a Collector class
        // in order to make it less imperative style
        Stream.Builder<T> currentBatchBuilder = Stream.builder();
        int currentBatchSize = 0;

        while (iter.hasNext()) {
            final T next = iter.next();
            if (currentBatchSize == maxBatchSize) {
                resultBuilder.add(currentBatchBuilder.build());
                // Start building a new batch
                currentBatchBuilder = Stream.builder();
                currentBatchSize = 0;
            }
            currentBatchBuilder.add(next);
            currentBatchSize++;
        }
        // Check if there is a non-empty Stream to add (e.g. if there was a
        // final batch which was smaller than the others)
        if (currentBatchSize > 0) {
            resultBuilder.add(currentBatchBuilder.build());
        }
    }
    return resultBuilder.build();
}

听起来你想做的是为你的
映射信息
实例所代表的数据实现一种排序。然后你可以为这些批创建一个
:这在某种程度上与方法家族相反,但讽刺的是,因此你可能必须自己以命令式的方式创建批-例如:

private static <T> Stream<Stream<T>> createBatchStreams(final Iterator<T> iter, final int maxBatchSize) {
    final Stream.Builder<Stream<T>> resultBuilder = Stream.builder();
    {
        // NOTE: This logic could also be encapsulated in a Collector class
        // in order to make it less imperative style
        Stream.Builder<T> currentBatchBuilder = Stream.builder();
        int currentBatchSize = 0;

        while (iter.hasNext()) {
            final T next = iter.next();
            if (currentBatchSize == maxBatchSize) {
                resultBuilder.add(currentBatchBuilder.build());
                // Start building a new batch
                currentBatchBuilder = Stream.builder();
                currentBatchSize = 0;
            }
            currentBatchBuilder.add(next);
            currentBatchSize++;
        }
        // Check if there is a non-empty Stream to add (e.g. if there was a
        // final batch which was smaller than the others)
        if (currentBatchSize > 0) {
            resultBuilder.add(currentBatchBuilder.build());
        }
    }
    return resultBuilder.build();
}

您只需添加和删除stackframe
info.size()/5次。我只想发送信息对象5乘5来计算方法。我不明白你的意思。在java8中,你可以使用并行stream@hellzone:我的意思是,以这种方式部分发送列表,在时间性能方面没有任何好处。@Azodious calculate方法将此列表发送到web服务,但这有一个问题webservice。当我发送100条信息时,它几乎在1小时后响应,当我发送5条信息时,它在1秒内响应。您只是添加和删除stackframe
info.size()/5次。我只想发送信息对象5乘5来计算方法。我不明白你的意思。在java8中,你可以使用并行stream@hellzone:我的意思是,以这种方式部分发送列表,在时间性能方面没有任何好处。@Azodious calculate方法将此列表发送到web服务,但这有一个问题当我发送100条信息时,它会在1小时后回复,当我发送5条信息时,它会在1秒内回复。