簡體   English   中英

使用 java 8 將一段時間更改為 IntStream

[英]Changing a while to IntStream using java 8

我正在玩 Java 8 並且我試圖將下面的代碼轉換為之前有 for 和 while 循環的代碼,但不幸的是我無法將 while 轉換為 IntStream。

有人可以幫我弄這個嗎。 另外,如果有人可以提出更好,更有效的方法。 謝謝 !

import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

public class NestedStreams {

    public static void main(String[] args) {

        ArrayList<String> a1 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
                null, "00", null, "00", "01", "90", null, "77", "00001", "AB").collect(Collectors.toList());
        ArrayList<String> a2 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
                null, "00", null, "00", "02", "90", null, "77", "00001", "AB").collect(Collectors.toList());
        ArrayList<String> a3 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
                null, "00", null, "00", "03", "90", null, "77", "00001", "AB").collect(Collectors.toList());
        ArrayList<String> a4 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
                null, "00", null, "00", "04", "90", null, "77", "00001", "AB").collect(Collectors.toList());
        ArrayList<String> a5 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
                null, "00", null, "00", "05", "90", null, "77", "00001", "AB").collect(Collectors.toList());
        ArrayList<String> a6 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
                null, "00", null, "00", "06", "90", null, "77", "00001", "AB").collect(Collectors.toList());
        ArrayList<String> a7 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
                null, "00", null, "00", "06", "90", null, "77", "00001", "AB").collect(Collectors.toList());
        ArrayList<String> a8 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
                null, "00", null, "00", "06", "90", null, "77", "00001", "AB").collect(Collectors.toList());
        ArrayList<String> a9 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
                null, "00", null, "00", "06", "90", null, "77", "00001", "AB").collect(Collectors.toList());
        ArrayList<String> a10 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
                null, "00", null, "00", "06", "90", null, "77", "00001", "AB").collect(Collectors.toList());
        ArrayList<String> a11 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
                null, "00", null, "00", "06", "90", null, "77", "00001", "AB").collect(Collectors.toList());
        ArrayList<String> a12 = (ArrayList<String>) Stream.of("ABC", "CFR", "DDR", "01", "M", null, "001", null, "00",
                null, "00", null, "00", "06", "90", null, "77", "00001", "AB").collect(Collectors.toList());

        ArrayList<ArrayList<String>> someList = new ArrayList<ArrayList<String>>();
        someList.addAll(Arrays.asList(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12));

        NestedStreams ns = new NestedStreams();
        String status = ns.testMethod(someList);
        if (status.equalsIgnoreCase("working")) {
            System.out.println("we can now do the processing");
        } else {
            System.out.println("failure");
        }

    }

    public String testMethod(ArrayList<ArrayList<String>> someList) {
        try {
            IntStream.range(0, someList.size()).forEach(i -> {
                ArrayList<String> someOtherValues = (ArrayList<String>) someList.get(i);
                someOtherValues.replaceAll(t -> Objects.isNull(t) ? "" : t);
                AtomicInteger count = new AtomicInteger(4);
                AtomicInteger counter = new AtomicInteger(5);
                if (!someOtherValues.get(0).toString().equals("")) {
                    // while (count.intValue() < (someOtherValues.size())) { //Line 62
                    IntStream.range(count.intValue(), someOtherValues.size()).forEach(value -> { //Line 63
                        IntStream.range(0, 3).forEach(k -> {
                            String avalue = someOtherValues.get(count.intValue()).toString();//count is getting increased more than the length of arrayList //Line 65
                            System.out.println(avalue);
                            counter.incrementAndGet();
                        });
                        count.set(counter.intValue());
                        counter.incrementAndGet();
                        System.out.println("The variable value :-" + value);
                        System.out.println("The variable counter :-" + counter);
                        System.out.println("The variable count :-" + count);
                    });
                }
            });
            return "working";
        } catch (Exception e) {
            e.printStackTrace();
            return "failed";
        }
    }
}

代碼的唯一問題是在更改為 IntStream 后,我無法檢查條件 count < someOtherValues.size() 由於第 65 行導致錯誤,因為計數增加超過 ArrayList 大小。

注意:上面的代碼導致 java.lang.IndexOutOfBoundsException: Index: 20, Size: 19 您可以取消注釋第 62 行的 while 循環並在第 63 行注釋 IntStream 以獲得完整的工作代碼。

IntStream只返回一個順序和有序的 stream。 您不能期望它像 while 循環那樣對每次迭代進行條目檢查。 如果您想獲得 while 循環提供的相同結果,請包含一個if語句來檢查條件。 我在我的機器上運行了它,它提供了相同的結果。

      IntStream.range(count.intValue(), someOtherValues.size()).forEach(value -> { //Line 63

         // If condition here acts as the condition statement in a loop 
         if(count.intValue() < someOtherValues.size()) {
                  IntStream.range(0, 3).forEach(k -> {
                            
                   String avalue = someOtherValues.get(count.intValue()).toString();//count is getting increased more than the length of arrayList //Line 65
                   System.out.println(avalue);
                   counter.incrementAndGet();
                        });
                        count.set(counter.intValue());
                        counter.incrementAndGet();
                        System.out.println("The variable value :-" + value);
                        System.out.println("The variable counter :-" + counter);
                        System.out.println("The variable count :-" + count);
                   }  
            });

需要注意的幾點:

  1. 目前尚不清楚您通過將while更改為IntStream來達到什么目的。 但是,我將把它留給用例依賴項。 由於您要求更好的方法,我建議堅持使用 while 循環,因為它以更簡單的方式完成工作。
  2. IntStream.range(start, end)提供包含起始和不包含結束的值
  3. 我進行了更改以匹配 while 循環生成的 output。 同樣,由於您在這里沒有提到您的目標,因此您應該注意正確性或預期的 output。

干杯!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM